DEV Community

Discussion on: Build an Interactive CLI Using TypeScript

bjesuiter profile image
Benjamin Jesuiter

Thank you for sharing this! At first, I thought, it would not contain all the bits i wanted, but in the end, you had nearly all!
I wanted: command line args, interactive questions, loading and styling.

The only thing I do want additionally is to have a shell like enviornment,
so that someone could start my cli and then insert the command he or she wants to run. But since this was not the topic of your Blogpost, it's not your problem :)

Thank you again!

_christiandimas profile image
Christian Dimas Author

Hey Ben, I just saw your comments on this post. I'm glad that this helps! :)

Can you share more detail about the shell-like environment you're looking for? Maybe I can write a future post on how to do what you need.

I know that it's already 3 months after you've posted the comment, but just in case you still need any help feel free to reply :)

bjesuiter profile image
Benjamin Jesuiter

Yes, thank you for replying!

I mean the following with shell-like Environment:

Imagine the git command. It has multiple subcommands, like 'commit' and 'push'.
Now imagine the nodejs REPL mode.

Now combine both and you get something like this:
$> git (drops you into the interactive git command line environment)

git > commit -m"My Message" (run the command and pass all params to this subommand)

The Idea of this whole thing is:
I want to give my cli to inexperienced users, who do rather follow interactive 'instructions on screen' instead of digging through man-pages or cli --help output.
Also, my cli could be stateful in this manner, it could ask you to log in first, save the login token and then put you in some sort of 'Home' menu with mutliple 'commands' which can be executed in the context of this login.

Have a nice day!

Thread Thread
_christiandimas profile image
Christian Dimas Author

Got it, thanks for the clear explanation!
That's an interesting idea you got.

I'm not quite sure how to build something REPL like (I'll look around for sure!).
But for your use case, I do have an alternative in mind

Because you planned to give your CLI to inexperienced users, what if instead of allowing them to type in the command, you give them a multiple-step prompt just like what I write above with a slightly different approach

Create the CLI as a single type. So it will not have subcommand, simply git (I'll be using git as an example for this)

So when they type:
$ > git

Then they will be presented with the subcommand available (commit, push, etc). Let's say the user chooses commit then the next step you can ask the user to add the message

If they choose push then you can just run the selected action, and not show any next step

Will this be enough for your use case?

I can write a follow-up to this article about the "stateful" CLI, and also publishing the CLI (currently the user needs to have NodeJS installed to get it installed). If you're interested! :)