You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

261 lines
6.6 KiB

  1. # Commander.js
  2. The complete solution for [node.js](http://nodejs.org) command-line interfaces, inspired by Ruby's [commander](https://github.com/visionmedia/commander).
  3. [![Build Status](https://secure.travis-ci.org/visionmedia/commander.js.png)](http://travis-ci.org/visionmedia/commander.js)
  4. ## Installation
  5. $ npm install commander
  6. ## Option parsing
  7. 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.
  8. ```js
  9. #!/usr/bin/env node
  10. /**
  11. * Module dependencies.
  12. */
  13. var program = require('commander');
  14. program
  15. .version('0.0.1')
  16. .option('-p, --peppers', 'Add peppers')
  17. .option('-P, --pineapple', 'Add pineapple')
  18. .option('-b, --bbq', 'Add bbq sauce')
  19. .option('-c, --cheese [type]', 'Add the specified type of cheese [marble]', 'marble')
  20. .parse(process.argv);
  21. console.log('you ordered a pizza with:');
  22. if (program.peppers) console.log(' - peppers');
  23. if (program.pineapple) console.log(' - pineappe');
  24. if (program.bbq) console.log(' - bbq');
  25. console.log(' - %s cheese', program.cheese);
  26. ```
  27. 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.
  28. ## Automated --help
  29. The help information is auto-generated based on the information commander already knows about your program, so the following `--help` info is for free:
  30. ```
  31. $ ./examples/pizza --help
  32. Usage: pizza [options]
  33. Options:
  34. -V, --version output the version number
  35. -p, --peppers Add peppers
  36. -P, --pineapple Add pineappe
  37. -b, --bbq Add bbq sauce
  38. -c, --cheese <type> Add the specified type of cheese [marble]
  39. -h, --help output usage information
  40. ```
  41. ## Coercion
  42. ```js
  43. function range(val) {
  44. return val.split('..').map(Number);
  45. }
  46. function list(val) {
  47. return val.split(',');
  48. }
  49. program
  50. .version('0.0.1')
  51. .usage('[options] <file ...>')
  52. .option('-i, --integer <n>', 'An integer argument', parseInt)
  53. .option('-f, --float <n>', 'A float argument', parseFloat)
  54. .option('-r, --range <a>..<b>', 'A range', range)
  55. .option('-l, --list <items>', 'A list', list)
  56. .option('-o, --optional [value]', 'An optional value')
  57. .parse(process.argv);
  58. console.log(' int: %j', program.integer);
  59. console.log(' float: %j', program.float);
  60. console.log(' optional: %j', program.optional);
  61. program.range = program.range || [];
  62. console.log(' range: %j..%j', program.range[0], program.range[1]);
  63. console.log(' list: %j', program.list);
  64. console.log(' args: %j', program.args);
  65. ```
  66. ## Custom help
  67. You can display arbitrary `-h, --help` information
  68. by listening for "--help". Commander will automatically
  69. exit once you are done so that the remainder of your program
  70. does not execute causing undesired behaviours, for example
  71. in the following executable "stuff" will not output when
  72. `--help` is used.
  73. ```js
  74. #!/usr/bin/env node
  75. /**
  76. * Module dependencies.
  77. */
  78. var program = require('../');
  79. function list(val) {
  80. return val.split(',').map(Number);
  81. }
  82. program
  83. .version('0.0.1')
  84. .option('-f, --foo', 'enable some foo')
  85. .option('-b, --bar', 'enable some bar')
  86. .option('-B, --baz', 'enable some baz');
  87. // must be before .parse() since
  88. // node's emit() is immediate
  89. program.on('--help', function(){
  90. console.log(' Examples:');
  91. console.log('');
  92. console.log(' $ custom-help --help');
  93. console.log(' $ custom-help -h');
  94. console.log('');
  95. });
  96. program.parse(process.argv);
  97. console.log('stuff');
  98. ```
  99. yielding the following help output:
  100. ```
  101. Usage: custom-help [options]
  102. Options:
  103. -h, --help output usage information
  104. -V, --version output the version number
  105. -f, --foo enable some foo
  106. -b, --bar enable some bar
  107. -B, --baz enable some baz
  108. Examples:
  109. $ custom-help --help
  110. $ custom-help -h
  111. ```
  112. ## .prompt(msg, fn)
  113. Single-line prompt:
  114. ```js
  115. program.prompt('name: ', function(name){
  116. console.log('hi %s', name);
  117. });
  118. ```
  119. Multi-line prompt:
  120. ```js
  121. program.prompt('description:', function(name){
  122. console.log('hi %s', name);
  123. });
  124. ```
  125. Coercion:
  126. ```js
  127. program.prompt('Age: ', Number, function(age){
  128. console.log('age: %j', age);
  129. });
  130. ```
  131. ```js
  132. program.prompt('Birthdate: ', Date, function(date){
  133. console.log('date: %s', date);
  134. });
  135. ```
  136. ## .password(msg[, mask], fn)
  137. Prompt for password without echoing:
  138. ```js
  139. program.password('Password: ', function(pass){
  140. console.log('got "%s"', pass);
  141. process.stdin.destroy();
  142. });
  143. ```
  144. Prompt for password with mask char "*":
  145. ```js
  146. program.password('Password: ', '*', function(pass){
  147. console.log('got "%s"', pass);
  148. process.stdin.destroy();
  149. });
  150. ```
  151. ## .confirm(msg, fn)
  152. Confirm with the given `msg`:
  153. ```js
  154. program.confirm('continue? ', function(ok){
  155. console.log(' got %j', ok);
  156. });
  157. ```
  158. ## .choose(list, fn)
  159. Let the user choose from a `list`:
  160. ```js
  161. var list = ['tobi', 'loki', 'jane', 'manny', 'luna'];
  162. console.log('Choose the coolest pet:');
  163. program.choose(list, function(i){
  164. console.log('you chose %d "%s"', i, list[i]);
  165. });
  166. ```
  167. ## Links
  168. - [API documentation](http://visionmedia.github.com/commander.js/)
  169. - [ascii tables](https://github.com/LearnBoost/cli-table)
  170. - [progress bars](https://github.com/visionmedia/node-progress)
  171. - [more progress bars](https://github.com/substack/node-multimeter)
  172. - [examples](https://github.com/visionmedia/commander.js/tree/master/examples)
  173. ## License
  174. (The MIT License)
  175. Copyright (c) 2011 TJ Holowaychuk &lt;tj@vision-media.ca&gt;
  176. Permission is hereby granted, free of charge, to any person obtaining
  177. a copy of this software and associated documentation files (the
  178. 'Software'), to deal in the Software without restriction, including
  179. without limitation the rights to use, copy, modify, merge, publish,
  180. distribute, sublicense, and/or sell copies of the Software, and to
  181. permit persons to whom the Software is furnished to do so, subject to
  182. the following conditions:
  183. The above copyright notice and this permission notice shall be
  184. included in all copies or substantial portions of the Software.
  185. THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
  186. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  187. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  188. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  189. CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  190. TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  191. SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.