Minimal toolkit for building CLIs
This package makes creating command line interfaces a breeze.
While there might be many alternatives out there, all ones I used are still based on old best practises. But since I'm not trying to reinvent the wheel here, I've decided to take advantage of minimist for all of the actual parsing.
pizza cheeseexecutes the "pizza-cheese" binary)
Firstly, you need to install the package:
npm install --save args
Once you're done, you can start using it within your binaries:
#!/usr/bin/env nodeimport args from 'args'argsoption'port' 'The port on which the app will be running' 3000option'reload' 'Enable/disable livereloading'command'serve' 'Serve your static site'const flags = argsparseprocessargv
The upper code defines two options called "port" and "reload" for the current binary, as well as a new sub command named "serve". So if you want to check for the value of the "port" option, just do this:
// This also works with "config.p", because the short name of the "port" option is "p"if flagsportconsole.log`I'll be running on port `
In turn, this is how the auto-generated usage information will look like:
Usage: haha [options] [command]Commands:serve Serve your static sitehelp Display helpOptions:-v, --version Output the version number-r, --reload Enable/disable livereloading-h, --help Output usage information-p, --port The port on which the app will be running
Register a new option for the binary in which it's being called.
-p, --port). However, it can also be an array in which the first value defines the short version (
-p) and the second one the long version (
-p, --package <list>.
Register a new sub command. Args requires all binaries to be defined in the style of git's. That means each sub command should be a separate binary called "<parent-command>-<sub-command>".
For example: If your main binary is called "muffin", the binary of the subcommand "muffin list" should be called "muffin-list". And all of them should be defined as such in your package.json.
name // The name of the commandsub // The output of .suboptions // An object containing the options that have been used
Using an initializer is currently only recommended if your command doesn't need special/different options than the binary in which you're defining it. The reason for this is that the "options" argument of the upper function will contain the options registered within the current binary.
This method takes the process' command line arguments (command and options) and uses the internal methods to get their values and assign them to the current instance of args. It needs to be run after all of the
.command calls. If you run it before them, the method calls after it won't take effect.
The methods also returns all options that have been used and their respective values.
process.argv, for example.
This property exposes all sub arguments that have been parsed by minimist. This is useful when trying to get the value after the command, for example:
The upper path can now be loaded by doing:
// Contains "./directory"const path = argssub0
This also works completely fine with sub commands: After you've registered a new command using
.command(), you can easily check the following sub argument within its binary like mentioned above:
pizza eat ./directory
Outputs the usage information based on the options and comments you've registered so far.
By default, the module already registers some default options (e.g. "version" and "help"), as well as a command named "help". These things have been implemented to make creating CLIs easier for beginners. However, they can also be disabled by taking advantage of the following properties:
|help||Automatically render the usage information when running
|version||Outputs the version tag of your package.json||true||Boolean|
|usageFilter||Allows you to specify a filter through which the usage information will be passed before it gets outputted||null||Function|
|value||Suffix for the "Usage" section of the usage information (example)||null||String|
You can pass the configuration object as the second paramater of .parse().
npm link args. Instead of the default one from npm, node will now use your clone of args!