commander
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|
Commander.js is a powerful Node.js package providing a complete solution for creating command-line interfaces. It offers an easy way to define required inputs, optional parameters, flags, and commands, thus empowering developers to build sophisticated, user-friendly command-line tools. Commander.js simplifies the process of handling command-line arguments, managing usage errors, and implementing a help system. It also supports multi-command workflows, where each command can have its own action handler or be a stand-alone executable.
To use Commander.js, you'll first need to install it via npm using the command npm install commander
. Once installed, you can require it in your Node.js script and start defining commands, options, and action handlers.
Here is a basic usage example:
const { program } = require('commander');
// Version and option setting
program
.version('1.0.0')
.option('-t, --type <type>', 'Specify type');
// Default help setup
program.parse();
// Access specified options
const options = program.opts();
console.log(`Type: ${options.type}`);
In this example, we define an option '-t' with a long form '--type'. This option expects a value which we can access via program.opts()
. Running the script with -t example
will output Type: example
.
You can also define custom commands with specific action handlers, handle required and optional command-arguments, and validate inputs. Commander.js supports both synchronous and asynchronous action handlers.
The Commander.js documentation, which includes a comprehensive guide on how to use the package, is available within its GitHub repository. From Installation setup and Quick Start guide to advanced usage scenarios, the documentation covers everything you need to effectively leverage the power of Commander.js. The documentation also provides numerous examples that cover a wide array of use cases. The Commander.js GitHub repository is located at https://github.com/tj/commander.js.