Commander.js
The complete solution for node.js command-line interfaces, inspired by Ruby's commander. API documentation: http://tj.github.com/commander.js/
Installation
Option parsing
Options with commander are defined with the .option()
method, also serving as documentation for the options. The example below parses args and options from process.argv
, leaving remaining args as the program.args
array which were not consumed by options.
Short flags may be passed as a single arg, for example -abc
is equivalent to -a -b -c
. Multi-word options such as "--template-engine" are camel-cased, becoming program.templateEngine
etc.
Variadic arguments
The last argument of a command can be variadic, and only the last argument. To make an argument variadic you have to append ...
to the argument name. Here is an example:
An Array
is used for the value of a variadic argument. This applies to program.args
as well as the argument passed to your action as demonstrated above.
Git-style sub-commands
When .command()
is invoked with a description argument, no .action(callback)
should be called to handle sub-commands, otherwise there will be an error. This tells commander that you're going to use separate executables for sub-commands, much like git(1)
and other popular tools.
The commander will try to find the executable script in current directory with the name scriptBasename-subcommand
, like pm-install
, pm-search
.
Automated --help
The help information is auto-generated based on the information commander already knows about your program, so the following --help
info is for free:
Coercion
Custom help
You can display arbitrary -h, --help
information by listening for "--help". Commander will automatically exit once you are done so that the remainder of your program does not execute causing undesired behaviours, for example in the following executable "stuff" will not output when --help
is used.
Yields the following help output when node script-name.js -h
or node script-name.js --help
are run:
.outputHelp()
Output help information without exiting.
.help()
Output help information and exit immediately.
Links
License
(The MIT License)
Copyright (c) 2011 TJ Holowaychuk <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.