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
Provenance
- Commit
95d54c3- Tree
aa29df2- Author
John Gee <john@ruru.gen.nz>- Committer
John Gee <john@ruru.gen.nz>- Parent 1
ac8db3a- Parent 2
5740e59
8 files changed+76 added−55 removed
modified:.github/dependabot.yml+9 added-4 removed
1234563456789101112version: 2updates:- package-ecosystem: "github-actions" target-branch: "develop" directory: "/" schedule: - package-ecosystem: "npm" target-branch: "develop" directory: "/" schedule: interval: "weekly" - package-ecosystem: "github-actions" target-branch: "develop" directory: "/" schedule: interval: "weekly"modified:lib/command.js+43 added-37 removed
108 unmodified lines109 return this;110 }111 112 /**113+ * @returns {Command[]}114+ * @api private115+ */116+117+ _getCommandAndAncestors() {118+ const result = [];119+ for (let command = this; command; command = command.parent) {120+ result.push(command);121+ }122+ return result;123+ }124+125+ /**126 * Define a command.127 *128 * There are two styles of command: pay attention to where to put the description.129 *642 unmodified lines772 * @return {Command} `this` command for chaining773 */774 775 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 }785 786 /**63 unmodified lines850 851 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 lines1097 _dispatchSubcommand(commandName, operands, unknown) {1098 const subCommand = this._findCommand(commandName);1099 if (!subCommand) this.help({ error: true });1100 1085- 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 }1112 1113 /**1114 * Invoke help directly if possible, or dispatch if necessary.11 unmodified lines1126 subCommand.help();1127 }1128 1129 // 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 }1134 1135 /**1136 * Check this.args against expected this._args.98 unmodified lines1235 1236 _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 lines1322 if (this._actionHandler) {1323 checkForUnknownOptions();1324 this._processArguments();1325 1308- 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 lines1392 */1393 1394 _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 }1404 1405 /**1406 * Display an error message if conflicting options are used together in this.32 unmodified lines1439 * @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 }1447 1448 /**1449 * Parse options from `argv` removing known options,154 unmodified lines1604 * @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 lines1991 * // or1992 * program.executableDir('subcommands');1993 *1994 * @param {string} [path]1977- * @return {string|Command}1995+ * @return {string|null|Command}1996 */1997 1998 executableDir(path) {1999 if (path === undefined) return this._executableDir;49 unmodified lines2049 contextOptions = undefined;2050 }2051 const context = this._getHelpContext(contextOptions);2052 2035- getCommandAndParents(this).reverse().forEach(command => command.emit('beforeAllHelp', context));2053+ this._getCommandAndAncestors().reverse().forEach(command => command.emit('beforeAllHelp', context));2054 this.emit('beforeHelp', context);2055 2056 let helpInformation = this.helpInformation(context);2057 if (deprecatedCallback) {3 unmodified lines2061 }2062 }2063 context.write(helpInformation);2064 2065+ 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 }2071 2072 /**2073 * You can pass in flags and a description to override the help134 unmodified lines2208 return arg;2209 });2210 }2211 2192-/**2193- * @param {Command} startCommand2194- * @returns {Command[]}2195- * @api private2196- */2197-2198-function getCommandAndParents(startCommand) {2199- const result = [];2200- for (let command = startCommand; command; command = command.parent) {2201- result.push(command);2202- }2203- return result;2204-}2205-2212 exports.Command = Command;modified:lib/help.js+6 added-6 removed
99 unmodified lines100 visibleGlobalOptions(cmd) {101 if (!this.showGlobalOptions) return [];102 103 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 lines239 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 }249 250 /**251 * Get the description for the command.213 unmodified linesmodified:tests/command.addHelpText.test.js+5 added-5 removed
179 unmodified lines180 expect(stderrSpy).toHaveBeenCalledWith('text\n');181 });182 183 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 lines192 expect(context.error).toBe(false);193 });194 195 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 lines205 expect(context.error).toBe(true);206 });207 208 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 lines217 expect(context.command).toBe(program);218 });219 220 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 lines230 expect(context.command).toBe(sub);231 });232 233 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 linesmodified:tests/commander.configureCommand.test.js+9 added-1 removed
79 unmodified lines8081828384848586878889909192939479 unmodified linestest('when storeOptionsAsProperties() after adding option then throw', () => { const program = new commander.Command(); program.option('--port <number>', 'port number', '80'); expect(() => { program.storeOptionsAsProperties(false); program.storeOptionsAsProperties(); }).toThrow();});
test('when storeOptionsAsProperties() after setting option value then throw', () => { const program = new commander.Command(); program.setOptionValue('foo', 'bar'); expect(() => { program.storeOptionsAsProperties(); }).toThrow();});modified:typings/index.d.ts+1 added-1 removed
817 unmodified lines818 executableDir(path: string): this;819 /**820 * Get the executable search directory.821 */822- executableDir(): string;822+ executableDir(): string | null;823 824 /**825 * Output help information for this command.826 *63 unmodified linesmodified:typings/index.test-d.ts+1 added-1 removed
273 unmodified lines274 expectType<commander.Command>(program.nameFromFilename(__filename));275 276 // executableDir277 expectType<commander.Command>(program.executableDir(__dirname));278-expectType<string>(program.executableDir());278+expectType<string | null>(program.executableDir());279 280 // outputHelp281 // eslint-disable-next-line @typescript-eslint/no-invalid-void-type, @typescript-eslint/no-confusing-void-expression282 expectType<void>(program.outputHelp());199 unmodified linesmodified:Readme.md+2 added
544 unmodified lines545 subcommand is specified ([example](./examples/defaultCommand.js)).546 547 You can add alternative names for a command with `.alias()`. ([example](./examples/alias.js))548 549+`.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.550+551 For 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.552 553 ### Command-arguments554 594 unmodified lines