Skip to content
Myr

Add opts to addCommand, and rename noHelp to hidden (#1232)

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

authored by John Gee · committed by GitHub · parent 8ec3e7f · Browse files · 7 files+74 added−13 removed
This commit

State

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

Diff stats

7 files+74 added−13 removed

  • tests:+56 added−3 removed
  • other:+18 added−10 removed

7 files+74 added−13 removed

modified:tests/fixtures/pm+1 added-1 removed
9 unmodified lines
10 .command('echo', 'echo arguments')11 .command('list', 'list packages installed').alias('lst')12 .command('listen', 'listen for supported signal events').alias('l')13 .command('publish', 'publish or update package').alias('p')14 .command('default', 'default command', { noHelp: true, isDefault: true })14 .command('default', 'default command', { hidden: true, isDefault: true })15 .command('specifyInstall', 'specify install subcommand', { executableFile: 'pm-install' })16 .command('specifyPublish', 'specify publish subcommand', { executableFile: 'pm-publish' })17 .command('silent', 'silently succeed')18 .parse(process.argv);
modified:tests/command.default.test.js+36 added-2 removed
modified:tests/command.help.test.js+19 added
modified:typings/commander-tests.ts+2 added-2 removed
43 unmodified lines
44const versionThis3: commander.Command = program.version('1.2.3', '-r,--revision', 'show revision information');4546// command (and CommandOptions)47const commandNew1: commander.Command = program.command('action');48const commandNew2: commander.Command = program.command('action', { isDefault: true, noHelp: true });48const commandNew2: commander.Command = program.command('action', { isDefault: true, hidden: true, noHelp: true });49const commandThis1: commander.Command = program.command('exec', 'exec description');50const commandThis2: commander.Command = program.command('exec', 'exec description', { isDefault: true, noHelp: true, executableFile: 'foo' });50const commandThis2: commander.Command = program.command('exec', 'exec description', { isDefault: true, hidden: true, noHelp: true, executableFile: 'foo' });5152// addCommand53const addCommandThis: commander.Command = program.addCommand(new commander.Command('abc'));54
164 unmodified lines
modified:typings/index.d.ts+6 added-3 removed
81 unmodified lines
82 * @param description - description of executable command83 * @param opts - configuration options84 * @returns `this` command for chaining85 */86 command(nameAndArgs: string, description: string, opts?: commander.CommandOptions): this;86 command(nameAndArgs: string, description: string, opts?: commander.ExecutableCommandOptions): this;8788 /**89 * Factory routine to create a new unattached command.90 *
8 unmodified lines
99 * See .command() for creating an attached subcommand which inherits settings from its parent.100 *101 * @returns `this` command for chaining102 */103 addCommand(cmd: Command): this;103 addCommand(cmd: Command, opts?: CommandOptions): this;104105 /**106 * Define argument syntax for command.107 *
234 unmodified lines
342 }343 type CommandConstructor = new (name?: string) => Command;344345 interface CommandOptions {346 noHelp?: boolean;346 noHelp?: boolean; // old name for hidden347 hidden?: boolean;348 isDefault?: boolean;349 }350 interface ExecutableCommandOptions extends CommandOptions {351 executableFile?: string;352 }353354 interface ParseOptionsResult {
17 unmodified lines
modified:index.js+9 added-4 removed
117 unmodified lines
118 this._defaultCommandName = null;119 this._exitCallback = null;120 this._alias = null;121122 this._noHelp = false;122 this._hidden = false;123 this._helpFlags = '-h, --help';124 this._helpDescription = 'display help for command';125 this._helpShortFlag = '-h';126 this._helpLongFlag = '--help';
46 unmodified lines
173 cmd._executableHandler = true;174 }175 if (opts.isDefault) this._defaultCommandName = cmd._name;176177 cmd._noHelp = !!opts.noHelp;177 cmd._hidden = !!(opts.noHelp || opts.hidden);178 cmd._helpFlags = this._helpFlags;179 cmd._helpDescription = this._helpDescription;180 cmd._helpShortFlag = this._helpShortFlag;181 cmd._helpLongFlag = this._helpLongFlag;
33 unmodified lines
215 *216 * See .command() for creating an attached subcommand which inherits settings from its parent.217 *218 * @param {Command} cmd - new subcommand219 * @param {Object} [opts] - configuration options220 * @return {Command} `this` command for chaining221 * @api public222 */223223 addCommand(cmd) {224 addCommand(cmd, opts) {225 if (!cmd._name) throw new Error('Command passed to .addCommand() must have a name');226227 // To keep things simple, block automatic name generation for deeply nested executables.228 // Fail fast and detect when adding rather than later when parsing.
6 unmodified lines
235 });236 }237 checkExplicitNames(cmd.commands);238239 opts = opts || {};240 if (opts.isDefault) this._defaultCommandName = cmd._name;241 if (opts.noHelp || opts.hidden) cmd._hidden = true; // modifying passed command due to existing implementation242243 this.commands.push(cmd);244 cmd.parent = this;245 return this;246 };
1032 unmodified lines
1279 */12801281 prepareCommands() {1282 const commandDetails = this.commands.filter((cmd) => {1278 return !cmd._noHelp;1283 return !cmd._hidden;1284 }).map((cmd) => {1285 const args = cmd._args.map((arg) => {1286 return humanReadableArgName(arg);1287 }).join(' ');
430 unmodified lines
modified:Readme.md+1 added-1 removed
328 unmodified lines
329program330 .addCommand(build.makeBuildCommand());331```332333Configuration options can be passed with the call to `.command()`. Specifying `true` for `opts.noHelp` will remove the command from the generated help output. Specifying `true` for `opts.isDefault` will run the subcommand if no other subcommand is specified ([example](./examples/defaultCommand.js)).333Configuration options can be passed with the call to `.command()` and `.addCommand()`. Specifying `true` for `opts.hidden` will remove the command from the generated help output. Specifying `true` for `opts.isDefault` will run the subcommand if no other subcommand is specified ([example](./examples/defaultCommand.js)).334335### Specify the argument syntax336337You use `.arguments` to specify the arguments for the top-level command, and for subcommands they are usually included in the `.command` call. Angled brackets (e.g. `<required>`) indicate required input. Square brackets (e.g. `[optional]`) indicate optional input.
400 unmodified lines