Table of contents
- 1. Code samples
- 2. More Features
There are many libraries to build CLI tools for Node.js like optimist, minimist, yargs, Caporal.js and commander to name just a few. In the following article Benny gives you an overview of his favorites.
My favorite CLI builder is commander because it comes with TypeScript definitions:
Biggest lack of commander.js is that it does not fail if you have mandatory arguments but don’t supply any argument at all. For example, it fails when calling
node program.js -c because there is no value for
c but it doesn’t fail when just calling
node program.js. Tested with commander v2.19.0. GitHub issue.
Luckily, Caporal.js addresses this issue:
Should you just care about getting arguments, you will be good with minimist:
- If you want to equip your program with some kind of configuration file (à la
.babelrc) I can recommend cosmiconfig to you. Cosmiconfig searches for and loads configuration files with the extensions
- If you need pattern matching you will probably fall in love with Globs which are implemened by minimatch.
- If you need an interactive command line user interface with prompts, try Inquirer.js.
- For more colorful log messages on the terminal, there is chalk.