Skip to content
Myr

Make new excess arguments error opt-in (#1429)

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

* Make allowExcessArguments opt-in (again) and inherited configuration * Remove stale JSDoc * Update documentation for allowExcessArguments being opt-in * Make comment more specific about what is being checked for excess arguments * Rework example class override

authored by John Gee · committed by GitHub · parent 8ac84ec · Browse files · 6 files+42 added−66 removed
This commit

State

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

Diff stats

6 files+42 added−66 removed

  • tests:+4 added−4 removed
  • other:+38 added−62 removed

6 files+42 added−66 removed

modified:examples/custom-command-class.js+27 added-27 removed
modified:tests/command.allowExcessArguments.test.js+4 added-4 removed
modified:typings/index.d.ts+1 added-1 removed
400 unmodified lines
401 */402 allowUnknownOption(allowUnknown?: boolean): this;403404 /**405 * Allow excess arguments on the command line.405 * Allow excess command-arguments on the command line. Pass false to make excess arguments an error.406 *407 * @returns `this` command for chaining408 */409 allowExcessArguments(allowExcess?: boolean): this;
212 unmodified lines
modified:CHANGELOG.md+1 added-28 removed
10 unmodified lines
11## [7.0.0] (date goes here)1213### Added1415- *Breaking:* error message if there are too many command-arguments on command line for the action handler ([#1409])16 - if should be allowed then declare extra arguments, or use `.allowExcessArguments()`15- `.allowExcessArguments(false)` to show an error message if there are too many command-arguments on command line for the action handler ([#1409])16- `.configureOutput()` to modify use of stdout and stderr or customise display of errors ([#1387])17- use `.addHelpText()` to add text before or after the built-in help, for just current command or also for all subcommands ([#1296])18- enhance Option class ([#1331])19 - allow hiding options from help
98 unmodified lines
118 if (command.opts().trace) console.log(`Command name is ${command.name()}`);119 });120```121123**excess command-arguments**124125There is now an error if there are too many command-arguments on the command line (only checked if there is an action handler).126If the extra arguments are supported by your command then you can either declare the expected arguments, or allow excess arguments.127128```js129// Old code before Commander 7130program131 .action(() => {});132program.parse(['a', 'b', 'c'], { from: 'user' }); // now causes an error133```134135```js136// New code, declare arguments137program138 .arguments('[args...]')139 .action(() => {});140```141142```js143// New code, allow excess arguments144program145 .allowExcessArguments()146 .action(() => {});147```148122## [7.0.0-2] (2020-12-14)123124(Released in 7.0.0)125
265 unmodified lines
modified:index.js+4 added-4 removed
533 unmodified lines
534 this.commands = [];535 this.options = [];536 this.parent = null;537 this._allowUnknownOption = false;538 this._allowExcessArguments = false;538 this._allowExcessArguments = true;539 this._args = [];540 this.rawArgs = null;541 this._scriptPath = null;542 this._name = name || '';
91 unmodified lines
634 cmd._helpConfiguration = this._helpConfiguration;635 cmd._exitCallback = this._exitCallback;636 cmd._storeOptionsAsProperties = this._storeOptionsAsProperties;637 cmd._combineFlagAndOptionalValue = this._combineFlagAndOptionalValue;638 cmd._allowExcessArguments = this._allowExcessArguments;639 cmd._enablePositionalOptions = this._enablePositionalOptions;640641 cmd._executableFile = opts.executableFile || null; // Custom name for executable file, set missing to null to match constructor642 this.commands.push(cmd);
483 unmodified lines
1126 return this;1127 };11281129 /**1129 * Allow excess arguments on the command line.1130 * Allow excess command-arguments on the command line. Pass false to make excess arguments an error.1131 *1132 * @param {Boolean} [allowExcess=true] - if `true` or omitted, no error will be thrown1133 * for excess arguments.1134 */
589 unmodified lines
1724 this._displayError(1, 'commander.missingArgument', message);1725 };17261727 /**1727 * `Option` is missing an argument, but received `flag` or nothing.1728 * `Option` is missing an argument.1729 *1730 * @param {Option} option1730 * @param {string} [flag]1731 * @api private1732 */17331734 optionMissingArgument(option) {
466 unmodified lines
modified:Readme.md+5 added-2 removed
481 unmodified lines
482 await program.parseAsync(process.argv);483}484```485486A command's options and arguments on the command line are validated when the command is used. Any unknown options or unexpected command-arguments will be reported as an error, or you can suppress these checks with `.allowUnknownOption()` and `.allowExcessArguments()`.486A command's options and arguments on the command line are validated when the command is used. Any unknown options or missing arguments will be reported as an error. You can suppress the unknown option checks with `.allowUnknownOption()`. By default it is not an error to487pass more arguments than declared, but you can make this an error with `.allowExcessArguments(false)`.488489### Stand-alone executable (sub)commands490491When `.command()` is invoked with a description argument, this tells Commander that you're going to use stand-alone executables for subcommands.
223 unmodified lines
715program --port=80 arg716program arg --port=80717```718718719By default the option processing shows an error for an unknown option. To have an unknown option treated as an ordinary command-argument and continue looking for options, use `.allowUnknownOption()`. This lets you mix known and unknown options.720721By default the argument processing does not display an error for more command-arguments than expected.722To display an error for excess arguments, use`.allowExcessArguments(false)`.723724### Legacy options as properties 725726Before Commander 7, the option values were stored as properties on the command.727This was convenient to code but the downside was possible clashes with
187 unmodified lines