Skip to content
Myr

merge master

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

authored by zhiyelee · committed by zhiyelee · parents 619c648, 4d421f9 · Browse files · 8 files+105 added−37 removed
This commit

State

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

Diff stats

8 files+105 added−37 removed

  • tests:+78 added−25 removed
  • other:+27 added−12 removed

8 files+105 added−37 removed

modified:test/fixtures/pm+1 added
6 unmodified lines
7 .command('install [name]', 'install one or more packages')8 .command('search [query]', 'search with optional query')9 .command('list', 'list packages installed')10 .command('publish', 'publish or update package')11 .command('default', 'default command', {noHelp: true, isDefault: true})12 .parse(process.argv);
added:test/fixtures/pm-default+2 added
1#!/usr/bin/env node2console.log('default');
modified:test/test.command.allowUnknownOption.js+26 added-25 removed
added:test/test.command.executableSubcommandDefault.js+46 added
modified:test/test.options.defaults.js+3 added
modified:index.js+13 added-7 removed
84 unmodified lines
85 this.options = [];86 this._execs = {};87 this._allowUnknownOption = false;88 this._args = [];89 this._name = name;89 this._name = name || '';90}9192/**93 * Inherit from `EventEmitter.prototype`.
70 unmodified lines
164 if (desc) {165 cmd.description(desc);166 this.executables = true;167 this._execs[cmd._name] = true;168 if (opts.isDefault) this.defaultExecutable = cmd._name;169 }170171 cmd._noHelp = !!opts.noHelp;172 this.commands.push(cmd);
272 unmodified lines
445446 // guess name447 this._name = this._name || basename(argv[1], '.js');448448 // git-style sub-commands with no sub-command449 if (this.executables && argv.length < 3) {449 // github-style sub-commands with no sub-command450 if (this.executables && argv.length < 3 && !this.defaultExecutable) {451 // this user needs help452 argv.push('--help');453 }454
6 unmodified lines
461 // executable sub-commands462 var name = result.args[0];463 if (this._execs[name] && typeof this._execs[name] != "function") {464 return this.executeSubCommand(argv, args, parsed.unknown);465 } else if (this.defaultExecutable) {466 // use the default subcommand467 args.unshift(name = this.defaultExecutable);468 return this.executeSubCommand(argv, args, parsed.unknown);469 }470471 return result;472};
541 unmodified lines
1014 *1015 * @api public1016 */10171013Command.prototype.outputHelp = function() {1014 process.stdout.write(this.helpInformation());1018Command.prototype.outputHelp = function(cb) {1019 if (!cb) cb = function(passthru) { return passthru; } //supply benign callback if none specified1020 process.stdout.write(cb(this.helpInformation()));1021 this.emit('--help');1022};10231024/**
1 unmodified line
1026 *1027 * @api public1028 */10291024Command.prototype.help = function() {1025 this.outputHelp();1030Command.prototype.help = function(cb) {1031 this.outputHelp(cb);1032 process.exit();1033};10341035/**
72 unmodified lines
modified:package.json+1 added-1 removed
modified:Readme.md+13 added-4 removed
165 unmodified lines
166program167 .version('0.0.1')168 .command('install [name]', 'install one or more packages')169 .command('search [query]', 'search with optional query')170 .command('list', 'list packages installed')170 .command('list', 'list packages installed', {isDefault: true})171 .parse(process.argv);172```173174When `.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. 175The commander will try to search the executables in the directory of the entry script (like `./examples/pm`) with the name `program-command`, like `pm-install`, `pm-search`.176177Options can be passed with the call to `.command()`. Specifying `true` for `opts.noHelp` will remove the option from the generated help output. Specifying `true` for `opts.isDefault` will run the subcommand if no other subcommand is specified.178179If the program is designed to be installed globally, make sure the executables have proper modes, like `755`.180181### `--harmony`182
84 unmodified lines
267 $ custom-help -h268269```270269## .outputHelp()271## .outputHelp(cb)272273Output help information without exiting.274Optional callback cb allows post-processing of help text before it is displayed.275276If you want to display help by default (e.g. if no command was provided), you can use something like:277278```js279var program = require('commander');280var colors = require('colors');281282program283 .version('0.0.1')284 .command('getstream [url]', 'get stream URL')285 .parse(process.argv);286287 if (!process.argv.slice(2).length) {284 program.outputHelp();288 program.outputHelp(make_red);289 }290291function make_red(txt) {292 return colors.red(txt); //display the help text in red on the console293}294```295288## .help()296## .help(cb)297298 Output help information and exit immediately.299 Optional callback cb allows post-processing of help text before it is displayed.300301## Examples302303```js
48 unmodified lines