Skip to content
Myr

Check command-arguments even without action handler (#1502)

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

* Check command-arguments even without action handler * Separate checking number of arguments and collecting variadic

authored by John Gee · committed by GitHub · parent eade1e0 · Browse files · 3 files+74 added−61 removed
This commit

State

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

Diff stats

3 files+74 added−61 removed

  • tests:+49 added−48 removed
  • other:+25 added−13 removed

3 files+74 added−61 removed

modified:tests/command.allowExcessArguments.test.js+32 added-48 removed
modified:tests/command.exitOverride.test.js+17 added
118 unmodified lines
119 expect(stderrSpy).toHaveBeenCalled();120 expectCommanderError(caughtErr, 1, 'commander.missingArgument', "error: missing required argument 'arg-name'");121 });122123 test('when specify program without required argument and no action handler then throw CommanderError', () => {124 const program = new commander.Command();125 program126 .exitOverride()127 .argument('<arg-name>');128129 let caughtErr;130 try {131 program.parse(['node', 'test']);132 } catch (err) {133 caughtErr = err;134 }135136 expect(stderrSpy).toHaveBeenCalled();137 expectCommanderError(caughtErr, 1, 'commander.missingArgument', "error: missing required argument 'arg-name'");138 });139140 test('when specify excess argument then throw CommanderError', () => {141 const program = new commander.Command();142 program143 .exitOverride()
177 unmodified lines
modified:index.js+25 added-13 removed
1557 unmodified lines
1558 if (parsed.unknown.length > 0) {1559 this.unknownOption(parsed.unknown[0]);1560 }1561 };15621563 const commandEvent = `command:${this.name()}`;1564 if (this._actionHandler) {1565 checkForUnknownOptions();1566 // Check expected arguments and collect variadic together.1567 const args = this.args.slice();1562 const checkNumberOfArguments = () => {1563 // too few1564 this._args.forEach((arg, i) => {1569 if (arg.required && args[i] == null) {1565 if (arg.required && this.args[i] == null) {1566 this.missingArgument(arg.name());1571 } else if (arg.variadic) {1572 args[i] = args.splice(i);1573 args.length = Math.min(i + 1, args.length);1567 }1568 });1576 if (args.length > this._args.length) {1577 this._excessArguments(args);1569 // too many1570 if (this._args.length > 0 && this._args[this._args.length - 1].variadic) {1571 return;1572 }1573 if (this.args.length > this._args.length) {1574 this._excessArguments(this.args);1575 }1576 };15771578 const commandEvent = `command:${this.name()}`;1579 if (this._actionHandler) {1580 checkForUnknownOptions();1581 checkNumberOfArguments();1582 // Collect trailing args into variadic.1583 let actionArgs = this.args;1584 const declaredArgCount = this._args.length;1585 if (declaredArgCount > 0 && this._args[declaredArgCount - 1].variadic) {1586 actionArgs = this.args.slice(0, declaredArgCount - 1);1587 actionArgs[declaredArgCount - 1] = this.args.slice(declaredArgCount - 1);1588 }1580 this._actionHandler(args);1589 this._actionHandler(actionArgs);1590 if (this.parent) this.parent.emit(commandEvent, operands, unknown); // legacy1591 } else if (this.parent && this.parent.listenerCount(commandEvent)) {1592 checkForUnknownOptions();1593 checkNumberOfArguments();1594 this.parent.emit(commandEvent, operands, unknown); // legacy1595 } else if (operands.length) {1596 if (this._findCommand('*')) { // legacy default command1597 this._dispatchSubcommand('*', operands, unknown);
3 unmodified lines
1601 } else if (this.commands.length) {1602 this.unknownCommand();1603 } else {1604 checkForUnknownOptions();1605 checkNumberOfArguments();1606 }1607 } else if (this.commands.length) {1608 // This command has subcommands and nothing hooked up at this level, so display help.1609 this.help({ error: true });1610 } else {1611 checkForUnknownOptions();1612 checkNumberOfArguments();1613 // fall through for caller to handle after calling .parse()1614 }1615 }1616 };
693 unmodified lines