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
Provenance
- Commit
0a14d4c- Tree
975afe3- Author
zhiyelee <zhiyelee@gmail.com>- Committer
zhiyelee <zhiyelee@gmail.com>- Parent 1
619c648- Parent 2
4d421f9
8 files changed+105 added−37 removed
modified:test/fixtures/pm+1 added
6 unmodified lines7 .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 node2+console.log('default');modified:test/test.command.allowUnknownOption.js+26 added-25 removed
123454576791089101112131415161718192021161723241819272021222324253326352728293031323334393536374338454639404142434448454647485253495055565152535455565758/** * Module dependencies. */
var util = require('util');var program = require('../') , sinon = require('sinon').sandbox.create() , should = require('should'); , should = require('should');
var oldProcessExit = process.exit;var oldConsoleError = console.error;var stubError = sinon.stub(console, 'error');var stubExit = sinon.stub(process, 'exit');
program .version('0.0.1') .option('-p, --pepper', 'add pepper');program.parse('node test -m'.split(' '));
var consoleErrors = [];process.exit = function () {};console.error = function () { consoleErrors.push(util.format.apply(util, arguments));};stubError.callCount.should.equal(3);
program.parse('node test -m'.split(' '));consoleErrors.length.should.equal(3);
// test subcommandvar consoleErrors = [];resetStubStatus();program .command('sub') .action(function () { });program.parse('node test sub -m'.split(' '));consoleErrors.length.should.equal(3);
consoleErrors = [];stubError.callCount.should.equal(3);stubExit.calledOnce.should.be.true();
// command with `allowUnknownOption`resetStubStatus();program .version('0.0.1') .option('-p, --pepper', 'add pepper');
program .allowUnknownOption() .parse('node test -m'.split(' '));consoleErrors.length.should.equal(0);
// test subcommandvar consoleErrors = [];stubError.callCount.should.equal(0);stubExit.calledOnce.should.be.false();
// subcommand with `allowUnknownOption`resetStubStatus();program .command('sub') .command('sub2') .allowUnknownOption() .action(function () { });program.parse('node test sub -m'.split(' '));consoleErrors.length.should.equal(3);program.parse('node test sub2 -m'.split(' '));
process.exit = oldProcessExit;console.error = oldConsoleError;stubError.callCount.should.equal(0);stubExit.calledOnce.should.be.false();
function resetStubStatus() { stubError.reset(); stubExit.reset();}added:test/test.command.executableSubcommandDefault.js+46 added
12345678910111213141516171819202122232425262728293031323334353637383940414243444546var exec = require('child_process').exec , path = require('path') , should = require('should');
var bin = path.join(__dirname, './fixtures/pm')// success caseexec(bin + ' default', function(error, stdout, stderr) { stdout.should.equal('default\n');});
// success case (default)exec(bin, function(error, stdout, stderr) { stdout.should.equal('default\n');});
// not existexec(bin + ' list', function (error, stdout, stderr) { //stderr.should.equal('\n pm-list(1) does not exist, try --help\n\n'); // TODO error info are not the same in between <=v0.8 and later version should.notEqual(0, stderr.length);});
// success caseexec(bin + ' install', function (error, stdout, stderr) { stdout.should.equal('install\n');});
// subcommand bin file with explicit extensionexec(bin + ' publish', function (error, stdout, stderr) { stdout.should.equal('publish\n');});
// spawn EACCESexec(bin + ' search', function (error, stdout, stderr) { // TODO error info are not the same in between <v0.10 and v0.12 should.notEqual(0, stderr.length);});
// when `bin` is a symbol link for mocking global installvar bin = path.join(__dirname, './fixtures/pmlink')// success caseexec(bin + ' install', function (error, stdout, stderr) { stdout.should.equal('install\n');});modified:test/test.options.defaults.js+3 added
12 unmodified lines1314151617181920212223242 unmodified lines12 unmodified lines .option('-s, --no-sauce', 'Uh… okay') .option('-r, --crust <type>', 'What kind of crust would you like?', 'hand-tossed') .option('-c, --cheese [type]', 'optionally specify the type of cheese', 'mozzarella');
program.should.have.property('_name', '');
program.parse(['node', 'test']);program.should.have.property('_name', 'test');program.should.not.have.property('anchovies');program.should.not.have.property('onions');program.should.not.have.property('olives');program.should.have.property('sauce', true);2 unmodified linesmodified:index.js+13 added-7 removed
84 unmodified lines85 this.options = [];86 this._execs = {};87 this._allowUnknownOption = false;88 this._args = [];89- this._name = name;89+ this._name = name || '';90 }91 92 /**93 * Inherit from `EventEmitter.prototype`.70 unmodified lines164 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 }170 171 cmd._noHelp = !!opts.noHelp;172 this.commands.push(cmd);272 unmodified lines445 446 // guess name447 this._name = this._name || basename(argv[1], '.js');448 448- // 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 lines461 // 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 }470 471 return result;472 };541 unmodified lines1014 *1015 * @api public1016 */1017 1013-Command.prototype.outputHelp = function() {1014- process.stdout.write(this.helpInformation());1018+Command.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 };1023 1024 /**1 unmodified line1026 *1027 * @api public1028 */1029 1024-Command.prototype.help = function() {1025- this.outputHelp();1030+Command.prototype.help = function(cb) {1031+ this.outputHelp(cb);1032 process.exit();1033 };1034 1035 /**72 unmodified linesmodified:package.json+1 added-1 removed
1233456726 unmodified lines{ "name": "commander", "version": "2.8.1", "version": "2.8.2", "description": "the complete solution for node.js command-line programs", "keywords": [ "command", "option",26 unmodified linesmodified:Readme.md+13 added-4 removed
165 unmodified lines166 program167 .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 ```173 174 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. 175 The 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`.176 177+Options 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.178+179 If the program is designed to be installed globally, make sure the executables have proper modes, like `755`.180 181 ### `--harmony`182 84 unmodified lines267 $ custom-help -h268 269 ```270 269-## .outputHelp()271+## .outputHelp(cb)272 273 Output help information without exiting.274+Optional callback cb allows post-processing of help text before it is displayed.275 276 If you want to display help by default (e.g. if no command was provided), you can use something like:277 278 ```js279 var program = require('commander');280+var colors = require('colors');281 282 program283 .version('0.0.1')284 .command('getstream [url]', 'get stream URL')285 .parse(process.argv);286 287 if (!process.argv.slice(2).length) {284- program.outputHelp();288+ program.outputHelp(make_red);289 }290+291+function make_red(txt) {292+ return colors.red(txt); //display the help text in red on the console293+}294 ```295 288-## .help()296+## .help(cb)297 298 Output help information and exit immediately.299+ Optional callback cb allows post-processing of help text before it is displayed.300 301 ## Examples302 303 ```js48 unmodified lines