Merge pull request #415 from Qix-/default-subcommand
Before adoption: this commit came in with the history the project adopted, so no landing records it.
add default executable (subcommand) option
authored by Randall Koutnik · committed by Randall Koutnik · parents 0238972, a456539 · Browse files · 5 files·+58 added−2 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+58 added−2 removed
- tests:+49 added−0 removed
- other:+9 added−2 removed
Provenance
- Commit
0af40a5- Tree
0ce87c5- Author
Randall Koutnik <rkoutnik@gmail.com>- Committer
Randall Koutnik <rkoutnik@gmail.com>- Parent 1
0238972- Parent 2
a456539
5 files changed+58 added−2 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');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:index.js+6 added-1 removed
163 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);273 unmodified lines446 // guess name447 this._name = this._name || basename(argv[1], '.js');448 449 // github-style sub-commands with no sub-command449- if (this.executables && argv.length < 3) {450+ 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 };636 unmodified linesmodified:Readme.md+3 added-1 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 162 unmodified lines