Skip to content
Myr

add default executable (subcommand) option

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

authored by Qix · committed by Qix · parent f68ab32 · 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

5 files+58 added−2 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');
added:test/test.command.executableSubcommandDefault.js+46 added
modified:index.js+6 added-1 removed
163 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);
273 unmodified lines
446 // guess name447 this._name = this._name || basename(argv[1], '.js');448449 // 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 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};
636 unmodified lines
modified:Readme.md+3 added-1 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
162 unmodified lines