When making node.js command line tools there might be a desire to make a command line tool where I drop into a shell in which I can enter commands to preform certain actions. Some examples of this might be the shell in mongodb where I can call methods, and full scripts from a shell that I can enter when calling the mongodb binary. Another example would be some of these command line text editors that involve entering commands to insert text, delete, and so forth. Once node.js built in module of interest when it comes to this might be the readline module, it allows for me to write an event handler for each time return is entered from the standard input in a command line interface. In this post I will be writing about this module, and give some copy and paste examples.
For a basic example I put together an example where I am just importing the module in with require, creating an interface with the standard input,and output, along with defining what the prompt should be. Once I have that I created an object in which I define what two commands do, and set up a single event hander for what should be done when a command is entered with the retrun key.
When I save this as a file like basic.js, and then call it in node, I drop into a shell with a ‘>’ prompt. From there I can enter ‘pwd’ to have the example log the current working dir to the standard output, or enter ‘close’ to exit the shell.
So if I have my basic.js file saved at my homw folder I might end up doing something like this when calling it.
Of course I can make the script global, add some more useful commands, and so forth. However for a basic hello work type example you should get the idea of how this can be useful for making this kind of cli program with node.js.
For a more advanced example I made a simple cli tool that will give me the point on a cirlce given the centerx, centery, angle, and distance values. I made it so all the current variable sates are displayed in the prompt, and when a change to a value is made the prompt chances. So this is a good example of having a custom prompt that conveys useful information relevant to the shell command. In this example I am also using more commands, and have a way to parse options for those commands.
So for the begining of my angles.js file I start off by importing just the readline module once again, after that I made a conf object that will store the current values of interest for this example.
In addition there is also a getPrompt method that will return the string value of the current prompt format for this shell command example.
So once again the first step with this is to set up the readline interface. The only difference this time is that I am using my getPrompt method that I have made in the conf object shown above. After that I call prompt for the first time.
I will need to update the prompt as needed when changing any of the values via the rl._prompt property.
Now for the commands. This time they require argument data in the form of a text string that will be given after entering the command folowed by a single space. The commands here are used to set the state of the variables, and updating the information displayed in the prompt, as well as a single command for finding the unknown point on a circle.
Finally I am defining what to do for the line event. Here I am parsing the command that is given by splitting it into two parts to find the command, and the text string after it.
Now to see this in action.
The readline module is a very useful tool when it comes to making these kinds of projects. I might take a stab at making a text editor or something to that effect with this kind of interface, but that might prove to be a time consuming project, and there are all ready a lot of great editors out there like that.