When making a node.js project that is to one extent or another a command line tool, there is often a need to parse options that may be given from the command line when using the tool. In this post i will be breefly covering some options for quickly getting this over with, and continuing with what really matters when making your node.js cli tool.
There are many npm packages that can be installed into a node.js project with npm install to add a dependency for quickly parsing command arguments from the command line, into a object that can be worked with. Many of these projects add additional useful features that allow for defining default values, and what to do for each sub command. In most cases this is best as it helps to save time when making a complex project, however there is also process.argv as well in node.js where the raw array of options are stored.
By far one of the most popular solutions for option parsing commander allows for quick option parsing, and also allows for defining commands.
be sure to check out Yargs as it is a great solution for an option parser. In addition to being a great solution for just parsing what is at process.argv into a workable object for me, it also allows for me to set up commands that are a great way of defining the logic of one or more commands that are to happened when my cli tool is called.
For example I can set up a default command that will log a basic usage example, and then one or more additional commands that actually do something.
One of the first option parsers I have worked with is nopt this is a good choice if you are looking for a more minimal solution for just option parsing, and not much of anything else.