Skip to content
Myr

docs

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Tj Holowaychuk · committed by Tj Holowaychuk · parent 467f574 · Browse files · 1 file+131 added−6 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+131 added−6 removed

  • other:+131 added−6 removed

1 file+131 added−6 removed

modified:lib/commander.js+131 added-6 removed
337 unmodified lines
338339/**340 * Parse command `args`.341 *342 * When listener(s) are available those343 * callbacks are invoked, otherwise the "*"344 * event is emitted and those actions are invoked.345 *346 * @param {Array} args347 * @return {Command} for chaining348 * @api private349 */350351Command.prototype.parseArgs = function(args){352 var cmds = this.commands349 , len = cmds.length;353 , len = cmds.length354 , name;355356 if (args.length) {352 this.emit('*', args);353 this.emit(arg = args.shift(), args);357 name = args[0];358 if (this.listeners(name).length) {359 this.emit(args.shift(), args);360 } else {361 this.emit('*', args);362 }363 }364365 return this;366};367368/**369 * Return an option matching `arg` if any.370 *371 * @param {String} arg372 * @return {Option}373 * @api private374 */375376Command.prototype.optionFor = function(arg){377 for (var i = 0, len = this.options.length; i < len; ++i) {378 if (this.options[i].is(arg)) {379 return this.options[i];380 }381 }382};383384/**385 * Parse options from `argv` returning `argv`386 * void of these options.387 *388 * @param {Array} argv389 * @return {Array}390 * @api public391 */392393Command.prototype.parseOptions = function(argv){394 var args = []395 , argv = argv.slice(2)396 , len = argv.length
40 unmodified lines
437438 return args;439};440441/**442 * Argument `name` is missing.443 *444 * @param {String} name445 * @api private446 */447448Command.prototype.missingArgument = function(name){449 console.error();450 console.error(" error: missing required argument `%s'", name);451 console.error();452 process.exit(1);453};454455/**456 * `Option` is missing an argument, but received `flag` or nothing.457 *458 * @param {String} option459 * @param {String} flag460 * @api private461 */462422Command.prototype.optionMissingArgument = function(option, got){463Command.prototype.optionMissingArgument = function(option, flag){464 console.error();424 if (got) {425 console.error(" error: option `%s' argument missing, got `%s'", option.flags, got);465 if (flag) {466 console.error(" error: option `%s' argument missing, got `%s'", option.flags, flag);467 } else {468 console.error(" error: option `%s' argument missing", option.flags);469 }470 console.error();471 process.exit(1);472};473474/**475 * Unknown option `flag`.476 *477 * @param {String} flag478 * @api private479 */480481Command.prototype.unknownOption = function(flag){482 console.error();483 console.error(" error: unknown option `%s'", flag);484 console.error();485 process.exit(1);486};487488/**489 * Set the program version to `str`.490 *491 * This method auto-registers the "-v, --version" flag492 * which will print the version number when passed.493 *494 * @param {String} str495 * @return {Command} for chaining496 * @api public497 */498499Command.prototype.version = function(str){500 if (0 == arguments.length) return this._version;501 this._version = str;502 this.option('-v, --version', 'output the version number');
3 unmodified lines
506 });507 return this;508};509510/**511 * Set / get the command usage `str`.512 *513 * @param {String} str514 * @return {String|Command}515 * @api public516 */517518Command.prototype.usage = function(str){519 if (0 == arguments.length) return this._usage || '[options]';520 this._usage = str;521 return this;522};523524/**525 * Return the largest option length.526 *527 * @return {Number}528 * @api private529 */530531Command.prototype.largestOptionLength = function(){532 return this.options.reduce(function(max, option){533 return Math.max(max, option.flags.length);534 }, 0);535};536537/**538 * Return help for options.539 *540 * @return {String}541 * @api private542 */543544Command.prototype.optionHelp = function(){545 var width = this.largestOptionLength();546 return this.options.map(function(option){547 return pad(option.flags, width)548 + ' ' + option.description;549 }).join('\n');550};551552/**553 * Return command help documentation.554 *555 * @return {String}556 * @api private557 */558559Command.prototype.commandHelp = function(){560 if (!this.commands.length) return '';561 return [562 ''
10 unmodified lines
573 , ''574 ].join('\n');575};576577/**578 * Return program help documentation.579 *580 * @return {String}581 * @api private582 */583584Command.prototype.helpInformation = function(){585 return [586 ''
6 unmodified lines
593 , ''594 ].join('\n');595};596597/**598 * Prompt for a `Number`.599 *600 * @param {String} str601 * @param {Function} fn602 * @api private603 */604605Command.prototype.promptForNumber = function(str, fn){606 this.promptSingleLine(str, function(val){607 val = Number(val);608 if (isNaN(val)) return program.promptForNumber(str + '(must be a number) ', fn);609 fn(val);610 });611};612613/**614 * Prompt for a `Date`.615 *616 * @param {String} str617 * @param {Function} fn618 * @api private619 */620621Command.prototype.promptForDate = function(str, fn){622 this.promptSingleLine(str, function(val){623 val = new Date(val);624 if (isNaN(val.getTime())) return program.promptForDate(str + '(must be a date) ', fn);625 fn(val);626 });627};628629/**630 * Single-line prompt.631 *632 * @param {String} str633 * @param {Function} fn634 * @api private635 */636637Command.prototype.promptSingleLine = function(str, fn){638 if ('function' == typeof arguments[2]) {639 return this['promptFor' + (fn.name || fn)](str, arguments[2]);640 }
4 unmodified lines
645 fn(val);646 }).resume();647};648649/**650 * Multi-line prompt.651 *652 * @param {String} str653 * @param {Function} fn654 * @api private655 */656657Command.prototype.promptMultiLine = function(str, fn){658 var buf = '';659 console.log(str);660 process.stdin.setEncoding('utf8');
50 unmodified lines