3 The complete solution for [node.js](http://nodejs.org) command-line interfaces, inspired by Ruby's [commander](https://github.com/visionmedia/commander).
5 [![Build Status](https://secure.travis-ci.org/visionmedia/commander.js.png)](http://travis-ci.org/visionmedia/commander.js)
9 $ npm install commander
13 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.
19 * Module dependencies.
22 var program = require('commander');
26 .option('-p, --peppers', 'Add peppers')
27 .option('-P, --pineapple', 'Add pineapple')
28 .option('-b, --bbq', 'Add bbq sauce')
29 .option('-c, --cheese [type]', 'Add the specified type of cheese [marble]', 'marble')
32 console.log('you ordered a pizza with:');
33 if (program.peppers) console.log(' - peppers');
34 if (program.pineapple) console.log(' - pineapple');
35 if (program.bbq) console.log(' - bbq');
36 console.log(' - %s cheese', program.cheese);
39 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.
43 The help information is auto-generated based on the information commander already knows about your program, so the following `--help` info is for free:
46 $ ./examples/pizza --help
48 Usage: pizza [options]
52 -V, --version output the version number
53 -p, --peppers Add peppers
54 -P, --pineapple Add pineapple
55 -b, --bbq Add bbq sauce
56 -c, --cheese <type> Add the specified type of cheese [marble]
57 -h, --help output usage information
65 return val.split('..').map(Number);
69 return val.split(',');
74 .usage('[options] <file ...>')
75 .option('-i, --integer <n>', 'An integer argument', parseInt)
76 .option('-f, --float <n>', 'A float argument', parseFloat)
77 .option('-r, --range <a>..<b>', 'A range', range)
78 .option('-l, --list <items>', 'A list', list)
79 .option('-o, --optional [value]', 'An optional value')
82 console.log(' int: %j', program.integer);
83 console.log(' float: %j', program.float);
84 console.log(' optional: %j', program.optional);
85 program.range = program.range || [];
86 console.log(' range: %j..%j', program.range[0], program.range[1]);
87 console.log(' list: %j', program.list);
88 console.log(' args: %j', program.args);
93 You can display arbitrary `-h, --help` information
94 by listening for "--help". Commander will automatically
95 exit once you are done so that the remainder of your program
96 does not execute causing undesired behaviours, for example
97 in the following executable "stuff" will not output when
104 * Module dependencies.
107 var program = require('../');
110 return val.split(',').map(Number);
115 .option('-f, --foo', 'enable some foo')
116 .option('-b, --bar', 'enable some bar')
117 .option('-B, --baz', 'enable some baz');
119 // must be before .parse() since
120 // node's emit() is immediate
122 program.on('--help', function(){
123 console.log(' Examples:');
125 console.log(' $ custom-help --help');
126 console.log(' $ custom-help -h');
130 program.parse(process.argv);
132 console.log('stuff');
135 yielding the following help output:
139 Usage: custom-help [options]
143 -h, --help output usage information
144 -V, --version output the version number
145 -f, --foo enable some foo
146 -b, --bar enable some bar
147 -B, --baz enable some baz
158 Output help information without exiting.
162 Output help information and exit immediately.
166 - [API documentation](http://visionmedia.github.com/commander.js/)
167 - [ascii tables](https://github.com/LearnBoost/cli-table)
168 - [progress bars](https://github.com/visionmedia/node-progress)
169 - [more progress bars](https://github.com/substack/node-multimeter)
170 - [examples](https://github.com/visionmedia/commander.js/tree/master/examples)
176 Copyright (c) 2011 TJ Holowaychuk <tj@vision-media.ca>
178 Permission is hereby granted, free of charge, to any person obtaining
179 a copy of this software and associated documentation files (the
180 'Software'), to deal in the Software without restriction, including
181 without limitation the rights to use, copy, modify, merge, publish,
182 distribute, sublicense, and/or sell copies of the Software, and to
183 permit persons to whom the Software is furnished to do so, subject to
184 the following conditions:
186 The above copyright notice and this permission notice shall be
187 included in all copies or substantial portions of the Software.
189 THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
190 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
191 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
192 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
193 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
194 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
195 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.