Skip to content
Myr

Add public Arguments property (#2010)

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 8edcfd9 · Browse files · 7 files+52 added−32 removed
This commit

State

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

Trailers

  • Co-authored-by: Wee Bit <aweebit64@gmail.com>

Diff stats

7 files+52 added−32 removed

  • tests:+15 added−15 removed
  • other:+37 added−17 removed

7 files+52 added−32 removed

modified:docs/deprecated.md+17 added
13 unmodified lines
14 - [cmd.description(cmdDescription, argDescriptions)](#cmddescriptioncmddescription-argdescriptions)15 - [InvalidOptionArgumentError](#invalidoptionargumenterror)16 - [Short option flag longer than a single character](#short-option-flag-longer-than-a-single-character)17 - [Import from `commander/esm.mjs`](#import-from-commanderesmmjs)18 - [cmd.\_args](#cmd_args)1920## RegExp .option() parameter2122The `.option()` method allowed a RegExp as the third parameter to restrict what values were accepted.
184 unmodified lines
207import { Command } from 'commander';208```209210README updated in Commander v9. Deprecated from Commander v9.211212## cmd._args213214This was always private, but was previously the only way to access the command `Argument` array.215216```js217const registeredArguments = program._args;218```219220The registered command arguments are now accessible via `.registeredArguments`.221222```js223const registeredArguments = program.registeredArguments;224```225226Deprecated from Commander v11.
modified:lib/command.js+14 added-13 removed
27 unmodified lines
28 this.parent = null;29 this._allowUnknownOption = false;30 this._allowExcessArguments = true;31 /** @type {Argument[]} */32 this._args = [];32 this.registeredArguments = [];33 this._args = this.registeredArguments; // deprecated old name34 /** @type {string[]} */35 this.args = []; // cli args with options removed36 this.rawArgs = [];37 this.processedArgs = []; // like .args but after custom processing and collecting variadic
318 unmodified lines
356 * @param {Argument} argument357 * @return {Command} `this` command for chaining358 */359 addArgument(argument) {359 const previousArgument = this._args.slice(-1)[0];360 const previousArgument = this.registeredArguments.slice(-1)[0];361 if (previousArgument && previousArgument.variadic) {362 throw new Error(`only the last argument can be variadic '${previousArgument.name()}'`);363 }364 if (argument.required && argument.defaultValue !== undefined && argument.parseArg === undefined) {365 throw new Error(`a default value for a required argument is never used: '${argument.name()}'`);366 }366 this._args.push(argument);367 this.registeredArguments.push(argument);368 return this;369 }370371 /**
111 unmodified lines
483484 action(fn) {485 const listener = (args) => {486 // The .action callback takes an extra parameter which is the command or options.486 const expectedArgsCount = this._args.length;487 const expectedArgsCount = this.registeredArguments.length;488 const actionArgs = args.slice(0, expectedArgsCount);489 if (this._storeOptionsAsProperties) {490 actionArgs[expectedArgsCount] = this; // backwards compatible "options"491 } else {
617 unmodified lines
1109 ]);1110 }11111112 /**1112 * Check this.args against expected this._args.1113 * Check this.args against expected this.registeredArguments.1114 *1115 * @api private1116 */11171118 _checkNumberOfArguments() {1119 // too few1119 this._args.forEach((arg, i) => {1120 this.registeredArguments.forEach((arg, i) => {1121 if (arg.required && this.args[i] == null) {1122 this.missingArgument(arg.name());1123 }1124 });1125 // too many1125 if (this._args.length > 0 && this._args[this._args.length - 1].variadic) {1126 if (this.registeredArguments.length > 0 && this.registeredArguments[this.registeredArguments.length - 1].variadic) {1127 return;1128 }1128 if (this.args.length > this._args.length) {1129 if (this.args.length > this.registeredArguments.length) {1130 this._excessArguments(this.args);1131 }1132 }11331134 /**1134 * Process this.args using this._args and save as this.processedArgs!1135 * Process this.args using this.registeredArguments and save as this.processedArgs!1136 *1137 * @api private1138 */1139
10 unmodified lines
11501151 this._checkNumberOfArguments();11521153 const processedArgs = [];1153 this._args.forEach((declaredArg, index) => {1154 this.registeredArguments.forEach((declaredArg, index) => {1155 let value = declaredArg.defaultValue;1156 if (declaredArg.variadic) {1157 // Collect together remaining arguments for passing together as an array.1158 if (index < this.args.length) {
606 unmodified lines
17651766 _excessArguments(receivedArgs) {1767 if (this._allowExcessArguments) return;17681768 const expected = this._args.length;1769 const expected = this.registeredArguments.length;1770 const s = (expected === 1) ? '' : 's';1771 const forSubcommand = this.parent ? ` for '${this.name()}'` : '';1772 const message = `error: too many arguments${forSubcommand}. Expected ${expected} argument${s} but got ${receivedArgs.length}.`;1773 this.error(message, { code: 'commander.excessArguments' });
131 unmodified lines
1905 usage(str) {1906 if (str === undefined) {1907 if (this._usage) return this._usage;19081908 const args = this._args.map((arg) => {1909 const args = this.registeredArguments.map((arg) => {1910 return humanReadableArgName(arg);1911 });1912 return [].concat(1913 (this.options.length || this._hasHelpOption ? '[options]' : []),1914 (this.commands.length ? '[command]' : []),1914 (this._args.length ? args : [])1915 (this.registeredArguments.length ? args : [])1916 ).join(' ');1917 }19181919 this._usage = str;
262 unmodified lines
modified:lib/help.js+4 added-4 removed
119 unmodified lines
120121 visibleArguments(cmd) {122 // Side effect! Apply the legacy descriptions before the arguments are displayed.123 if (cmd._argsDescription) {124 cmd._args.forEach(argument => {124 cmd.registeredArguments.forEach(argument => {125 argument.description = argument.description || cmd._argsDescription[argument.name()] || '';126 });127 }128129 // If there are any arguments with a description then return all the arguments.130 if (cmd._args.find(argument => argument.description)) {131 return cmd._args;130 if (cmd.registeredArguments.find(argument => argument.description)) {131 return cmd.registeredArguments;132 }133 return [];134 }135
5 unmodified lines
141 */142143 subcommandTerm(cmd) {144 // Legacy. Ignores custom usage string, and nested commands.145 const args = cmd._args.map(arg => humanReadableArgName(arg)).join(' ');145 const args = cmd.registeredArguments.map(arg => humanReadableArgName(arg)).join(' ');146 return cmd._name +147 (cmd._aliases[0] ? '|' + cmd._aliases[0] : '') +148 (cmd.options.length ? ' [options]' : '') + // simplistic check for non-help option149 (args ? ' ' + args : '');
315 unmodified lines
modified:tests/command.argumentVariations.test.js+9 added-9 removed
modified:tests/command.createArgument.test.js+6 added-6 removed
modified:typings/index.d.ts+1 added
288 unmodified lines
289 args: string[];290 processedArgs: any[];291 readonly commands: readonly Command[];292 readonly options: readonly Option[];293 readonly registeredArguments: readonly Argument[];294 parent: Command | null;295296 constructor(name?: string);297
582 unmodified lines
modified:typings/index.test-d.ts+1 added
28 unmodified lines
29// eslint-disable-next-line @typescript-eslint/no-explicit-any30expectType<any[]>(program.processedArgs);31expectType<readonly commander.Command[]>(program.commands);32expectType<readonly commander.Option[]>(program.options);33expectType<readonly commander.Argument[]>(program.registeredArguments);34expectType<commander.Command | null>(program.parent);3536// version37expectType<commander.Command>(program.version('1.2.3'));
446 unmodified lines