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
Provenance
- Commit
b5d95ee- Tree
cff1178- Author
John Gee <john@ruru.gen.nz>- Committer
GitHub <noreply@github.com>- Parent
8ec3e7f
7 files changed+74 added−13 removed
modified:tests/fixtures/pm+1 added-1 removed
9 unmodified lines10 .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
42 unmodified lines4344454647474849505152535354555657585960616263646566676869707172737475767778798081828384858687888990919242 unmodified lines program.parse('node test.js'.split(' ')); expect(actionMock).toHaveBeenCalled(); });
test('when default subcommand and unrecognised argument then call default with argument', () => { test('when default subcommand and unrecognised argument then call default', () => { const { program, actionMock } = makeProgram(); program.parse('node test.js an-argument'.split(' ')); expect(actionMock).toHaveBeenCalled(); });
test('when default subcommand and unrecognised option then call default with option', () => { test('when default subcommand and unrecognised option then call default', () => { const { program, actionMock } = makeProgram(); program.parse('node test.js --an-option'.split(' ')); expect(actionMock).toHaveBeenCalled(); });});
describe('default added command', () => { function makeProgram() { const actionMock = jest.fn(); const defaultCmd = new commander.Command('default') .allowUnknownOption() .action(actionMock);
const program = new commander.Command(); program .command('other'); program .addCommand(defaultCmd, { isDefault: true }); return { program, actionMock }; }
test('when default subcommand and no command then call default', () => { const { program, actionMock } = makeProgram(); program.parse('node test.js'.split(' ')); expect(actionMock).toHaveBeenCalled(); });
test('when default subcommand and unrecognised argument then call default', () => { const { program, actionMock } = makeProgram(); program.parse('node test.js an-argument'.split(' ')); expect(actionMock).toHaveBeenCalled(); });
test('when default subcommand and unrecognised option then call default', () => { const { program, actionMock } = makeProgram(); program.parse('node test.js --an-option'.split(' ')); expect(actionMock).toHaveBeenCalled(); });});modified:tests/command.help.test.js+19 added
86 unmodified lines8788899091929394959697989910010110210310410510610710810911011111211311411511686 unmodified lines expect(writeSpy).toHaveBeenCalledWith(helpReplacement); writeSpy.mockClear();});
// noHelp is now named hidden, not officially deprecated yettest('when command sets noHelp then not displayed in helpInformation', () => { const program = new commander.Command(); program .command('secret', 'secret description', { noHelp: true }); const helpInformation = program.helpInformation(); expect(helpInformation).not.toMatch('secret');});
test('when command sets hidden then not displayed in helpInformation', () => { const program = new commander.Command(); program .command('secret', 'secret description', { hidden: true }); const helpInformation = program.helpInformation(); expect(helpInformation).not.toMatch('secret');});
test('when addCommand with hidden:true then not displayed in helpInformation', () => { const secretCmd = new commander.Command('secret');
const program = new commander.Command(); program .addCommand(secretCmd, { hidden: true }); const helpInformation = program.helpInformation(); expect(helpInformation).not.toMatch('secret');});modified:typings/commander-tests.ts+2 added-2 removed
43 unmodified lines44 const versionThis3: commander.Command = program.version('1.2.3', '-r,--revision', 'show revision information');45 46 // command (and CommandOptions)47 const commandNew1: commander.Command = program.command('action');48-const commandNew2: commander.Command = program.command('action', { isDefault: true, noHelp: true });48+const commandNew2: commander.Command = program.command('action', { isDefault: true, hidden: true, noHelp: true });49 const commandThis1: commander.Command = program.command('exec', 'exec description');50-const commandThis2: commander.Command = program.command('exec', 'exec description', { isDefault: true, noHelp: true, executableFile: 'foo' });50+const commandThis2: commander.Command = program.command('exec', 'exec description', { isDefault: true, hidden: true, noHelp: true, executableFile: 'foo' });51 52 // addCommand53 const addCommandThis: commander.Command = program.addCommand(new commander.Command('abc'));54 164 unmodified linesmodified:typings/index.d.ts+6 added-3 removed
81 unmodified lines82 * @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;87 88 /**89 * Factory routine to create a new unattached command.90 *8 unmodified lines99 * 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;104 105 /**106 * Define argument syntax for command.107 *234 unmodified lines342 }343 type CommandConstructor = new (name?: string) => Command;344 345 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 }353 354 interface ParseOptionsResult {17 unmodified linesmodified:index.js+9 added-4 removed
117 unmodified lines118 this._defaultCommandName = null;119 this._exitCallback = null;120 this._alias = null;121 122- 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 lines173 cmd._executableHandler = true;174 }175 if (opts.isDefault) this._defaultCommandName = cmd._name;176 177- 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 lines215 *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 */223 223- addCommand(cmd) {224+ addCommand(cmd, opts) {225 if (!cmd._name) throw new Error('Command passed to .addCommand() must have a name');226 227 // 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 lines235 });236 }237 checkExplicitNames(cmd.commands);238 239+ 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 implementation242+243 this.commands.push(cmd);244 cmd.parent = this;245 return this;246 };1032 unmodified lines1279 */1280 1281 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 linesmodified:Readme.md+1 added-1 removed
328 unmodified lines329 program330 .addCommand(build.makeBuildCommand());331 ```332 333-Configuration 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)).333+Configuration 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)).334 335 ### Specify the argument syntax336 337 You 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