Skip to content
Myr

Merge branch 'develop' into release/12.x

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

authored by John Gee · committed by John Gee · parents ac8db3a, 5740e59 · Browse files · 8 files+76 added−55 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+76 added−55 removed

  • tests:+14 added−6 removed
  • other:+62 added−49 removed

8 files+76 added−55 removed

modified:.github/dependabot.yml+9 added-4 removed
modified:lib/command.js+43 added-37 removed
108 unmodified lines
109 return this;110 }111112 /**113 * @returns {Command[]}114 * @api private115 */116117 _getCommandAndAncestors() {118 const result = [];119 for (let command = this; command; command = command.parent) {120 result.push(command);121 }122 return result;123 }124125 /**126 * Define a command.127 *128 * There are two styles of command: pay attention to where to put the description.129 *
642 unmodified lines
772 * @return {Command} `this` command for chaining773 */774775 storeOptionsAsProperties(storeAsProperties = true) {763 this._storeOptionsAsProperties = !!storeAsProperties;776 if (this.options.length) {777 throw new Error('call .storeOptionsAsProperties() before adding options');778 }779 if (Object.keys(this._optionValues).length) {780 throw new Error('call .storeOptionsAsProperties() before setting option values');781 }782 this._storeOptionsAsProperties = !!storeAsProperties;783 return this;784 }785786 /**
63 unmodified lines
850851 getOptionValueSourceWithGlobals(key) {852 // global overwrites local, like optsWithGlobals853 let source;838 getCommandAndParents(this).forEach((cmd) => {854 this._getCommandAndAncestors().forEach((cmd) => {855 if (cmd.getOptionValueSource(key) !== undefined) {856 source = cmd.getOptionValueSource(key);857 }858 });
238 unmodified lines
1097 _dispatchSubcommand(commandName, operands, unknown) {1098 const subCommand = this._findCommand(commandName);1099 if (!subCommand) this.help({ error: true });11001085 let hookResult;1086 hookResult = this._chainOrCallSubCommandHook(hookResult, subCommand, 'preSubcommand');1087 hookResult = this._chainOrCall(hookResult, () => {1101 let promiseChain;1102 promiseChain = this._chainOrCallSubCommandHook(promiseChain, subCommand, 'preSubcommand');1103 promiseChain = this._chainOrCall(promiseChain, () => {1104 if (subCommand._executableHandler) {1105 this._executeSubCommand(subCommand, operands.concat(unknown));1106 } else {1107 return subCommand._parseCommand(operands, unknown);1108 }1109 });1094 return hookResult;1110 return promiseChain;1111 }11121113 /**1114 * Invoke help directly if possible, or dispatch if necessary.
11 unmodified lines
1126 subCommand.help();1127 }11281129 // Fallback to parsing the help flag to invoke the help.1114 return this._dispatchSubcommand(subcommandName, [], [this._helpLongFlag]);1130 return this._dispatchSubcommand(subcommandName, [], [1131 this._helpLongFlag || this._helpShortFlag1132 ]);1133 }11341135 /**1136 * Check this.args against expected this._args.
98 unmodified lines
12351236 _chainOrCallHooks(promise, event) {1237 let result = promise;1238 const hooks = [];1221 getCommandAndParents(this)1239 this._getCommandAndAncestors()1240 .reverse()1241 .filter(cmd => cmd._lifeCycleHooks[event] !== undefined)1242 .forEach(hookedCommand => {1243 hookedCommand._lifeCycleHooks[event].forEach((callback) => {
78 unmodified lines
1322 if (this._actionHandler) {1323 checkForUnknownOptions();1324 this._processArguments();13251308 let actionResult;1309 actionResult = this._chainOrCallHooks(actionResult, 'preAction');1310 actionResult = this._chainOrCall(actionResult, () => this._actionHandler(this.processedArgs));1326 let promiseChain;1327 promiseChain = this._chainOrCallHooks(promiseChain, 'preAction');1328 promiseChain = this._chainOrCall(promiseChain, () => this._actionHandler(this.processedArgs));1329 if (this.parent) {1312 actionResult = this._chainOrCall(actionResult, () => {1330 promiseChain = this._chainOrCall(promiseChain, () => {1331 this.parent.emit(commandEvent, operands, unknown); // legacy1332 });1333 }1316 actionResult = this._chainOrCallHooks(actionResult, 'postAction');1317 return actionResult;1334 promiseChain = this._chainOrCallHooks(promiseChain, 'postAction');1335 return promiseChain;1336 }1337 if (this.parent && this.parent.listenerCount(commandEvent)) {1338 checkForUnknownOptions();1339 this._processArguments();
52 unmodified lines
1392 */13931394 _checkForMissingMandatoryOptions() {1395 // Walk up hierarchy so can call in subcommand after checking for displaying help.1378 for (let cmd = this; cmd; cmd = cmd.parent) {1396 this._getCommandAndAncestors().forEach((cmd) => {1397 cmd.options.forEach((anOption) => {1398 if (anOption.mandatory && (cmd.getOptionValue(anOption.attributeName()) === undefined)) {1399 cmd.missingMandatoryOptionValue(anOption);1400 }1401 });1384 }1402 });1403 }14041405 /**1406 * Display an error message if conflicting options are used together in this.
32 unmodified lines
1439 * @api private1440 */1441 _checkForConflictingOptions() {1442 // Walk up hierarchy so can call in subcommand after checking for displaying help.1425 for (let cmd = this; cmd; cmd = cmd.parent) {1443 this._getCommandAndAncestors().forEach((cmd) => {1444 cmd._checkForConflictingLocalOptions();1427 }1445 });1446 }14471448 /**1449 * Parse options from `argv` removing known options,
154 unmodified lines
1604 * @return {Object}1605 */1606 optsWithGlobals() {1607 // globals overwrite locals1590 return getCommandAndParents(this).reduce(1608 return this._getCommandAndAncestors().reduce(1609 (combinedOptions, cmd) => Object.assign(combinedOptions, cmd.opts()),1610 {}1611 );1612 }
378 unmodified lines
1991 * // or1992 * program.executableDir('subcommands');1993 *1994 * @param {string} [path]1977 * @return {string|Command}1995 * @return {string|null|Command}1996 */19971998 executableDir(path) {1999 if (path === undefined) return this._executableDir;
49 unmodified lines
2049 contextOptions = undefined;2050 }2051 const context = this._getHelpContext(contextOptions);20522035 getCommandAndParents(this).reverse().forEach(command => command.emit('beforeAllHelp', context));2053 this._getCommandAndAncestors().reverse().forEach(command => command.emit('beforeAllHelp', context));2054 this.emit('beforeHelp', context);20552056 let helpInformation = this.helpInformation(context);2057 if (deprecatedCallback) {
3 unmodified lines
2061 }2062 }2063 context.write(helpInformation);20642065 if (this._helpLongFlag) {2047 this.emit(this._helpLongFlag); // deprecated2066 this.emit(this._helpLongFlag); // deprecated2067 }2068 this.emit('afterHelp', context);2049 getCommandAndParents(this).forEach(command => command.emit('afterAllHelp', context));2069 this._getCommandAndAncestors().forEach(command => command.emit('afterAllHelp', context));2070 }20712072 /**2073 * You can pass in flags and a description to override the help
134 unmodified lines
2208 return arg;2209 });2210}22112192/**2193 * @param {Command} startCommand2194 * @returns {Command[]}2195 * @api private2196 */21972198function getCommandAndParents(startCommand) {2199 const result = [];2200 for (let command = startCommand; command; command = command.parent) {2201 result.push(command);2202 }2203 return result;2204}22052212exports.Command = Command;
modified:lib/help.js+6 added-6 removed
99 unmodified lines
100 visibleGlobalOptions(cmd) {101 if (!this.showGlobalOptions) return [];102103 const globalOptions = [];104 for (let parentCmd = cmd.parent; parentCmd; parentCmd = parentCmd.parent) {105 const visibleOptions = parentCmd.options.filter((option) => !option.hidden);104 for (let ancestorCmd = cmd.parent; ancestorCmd; ancestorCmd = ancestorCmd.parent) {105 const visibleOptions = ancestorCmd.options.filter((option) => !option.hidden);106 globalOptions.push(...visibleOptions);107 }108 if (this.sortOptions) {109 globalOptions.sort(this.compareOptions);
129 unmodified lines
239 let cmdName = cmd._name;240 if (cmd._aliases[0]) {241 cmdName = cmdName + '|' + cmd._aliases[0];242 }243 let parentCmdNames = '';244 for (let parentCmd = cmd.parent; parentCmd; parentCmd = parentCmd.parent) {245 parentCmdNames = parentCmd.name() + ' ' + parentCmdNames;243 let ancestorCmdNames = '';244 for (let ancestorCmd = cmd.parent; ancestorCmd; ancestorCmd = ancestorCmd.parent) {245 ancestorCmdNames = ancestorCmd.name() + ' ' + ancestorCmdNames;246 }247 return parentCmdNames + cmdName + ' ' + cmd.usage();247 return ancestorCmdNames + cmdName + ' ' + cmd.usage();248 }249250 /**251 * Get the description for the command.
213 unmodified lines
modified:tests/command.addHelpText.test.js+5 added-5 removed
179 unmodified lines
180 expect(stderrSpy).toHaveBeenCalledWith('text\n');181 });182183 test('when help requested then context.error is false', () => {184 let context = {};184 let context;185 const program = new commander.Command();186 program187 .exitOverride()188 .addHelpText('before', (contextParam) => { context = contextParam; });
3 unmodified lines
192 expect(context.error).toBe(false);193 });194195 test('when help for error then context.error is true', () => {196 let context = {};196 let context;197 const program = new commander.Command();198 program199 .exitOverride()200 .addHelpText('before', (contextParam) => { context = contextParam; })
4 unmodified lines
205 expect(context.error).toBe(true);206 });207208 test('when help on program then context.command is program', () => {209 let context = {};209 let context;210 const program = new commander.Command();211 program212 .exitOverride()213 .addHelpText('before', (contextParam) => { context = contextParam; });
3 unmodified lines
217 expect(context.command).toBe(program);218 });219220 test('when help on subcommand and "before" subcommand then context.command is subcommand', () => {221 let context = {};221 let context;222 const program = new commander.Command();223 program224 .exitOverride();225 const sub = program.command('sub')
4 unmodified lines
230 expect(context.command).toBe(sub);231 });232233 test('when help on subcommand and "beforeAll" on program then context.command is subcommand', () => {234 let context = {};234 let context;235 const program = new commander.Command();236 program237 .exitOverride()238 .addHelpText('beforeAll', (contextParam) => { context = contextParam; });
7 unmodified lines
modified:tests/commander.configureCommand.test.js+9 added-1 removed
modified:typings/index.d.ts+1 added-1 removed
817 unmodified lines
818 executableDir(path: string): this;819 /**820 * Get the executable search directory.821 */822 executableDir(): string;822 executableDir(): string | null;823824 /**825 * Output help information for this command.826 *
63 unmodified lines
modified:typings/index.test-d.ts+1 added-1 removed
273 unmodified lines
274expectType<commander.Command>(program.nameFromFilename(__filename));275276// executableDir277expectType<commander.Command>(program.executableDir(__dirname));278expectType<string>(program.executableDir());278expectType<string | null>(program.executableDir());279280// outputHelp281// eslint-disable-next-line @typescript-eslint/no-invalid-void-type, @typescript-eslint/no-confusing-void-expression282expectType<void>(program.outputHelp());
199 unmodified lines
modified:Readme.md+2 added
544 unmodified lines
545subcommand is specified ([example](./examples/defaultCommand.js)).546547You can add alternative names for a command with `.alias()`. ([example](./examples/alias.js))548549`.command()` automatically copies the inherited settings from the parent command to the newly created subcommand. This is only done during creation, any later setting changes to the parent are not inherited.550551For safety, `.addCommand()` does not automatically copy the inherited settings from the parent command. There is a helper routine `.copyInheritedSettings()` for copying the settings when they are wanted.552553### Command-arguments554
594 unmodified lines