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
Provenance
- Commit
0504945- Tree
2f29727- Author
John Gee <john@ruru.gen.nz>- Committer
GitHub <noreply@github.com>- Parent
eade1e0
3 files changed+74 added−61 removed
modified:tests/command.allowExcessArguments.test.js+32 added-48 removed
1234567567891011121314151691011121314151617181920171823242519202122232425262728293536303731323334353637383940474841494243444546474849505159605261535455565758596070717273616263646566676869707182837273858687747576777879808182838485869899871008889909192939495969798111112991131001011021031041051061071081091101241251111261121131141151161171181191201211221371381231391241251261272 unmodified linesconst commander = require('../');
// Not testing output, just testing whether an error is detected.
describe('allowUnknownOption', () => { // Optional. Use internal knowledge to suppress output to keep test output clean. let writeErrorSpy;describe.each([true, false])('allowExcessArguments when action handler: %s', (hasActionHandler) => { function configureCommand(cmd) { cmd .exitOverride() .configureOutput({ writeErr: () => {} }); if (hasActionHandler) { cmd.action(() => {}); } }
beforeAll(() => { writeErrorSpy = jest.spyOn(process.stderr, 'write').mockImplementation(() => { }); });
afterEach(() => { writeErrorSpy.mockClear(); });
afterAll(() => { writeErrorSpy.mockRestore(); });
test('when specify excess program argument then no error by default', () => { const program = new commander.Command(); program .exitOverride() .action(() => {}); configureCommand(program);
expect(() => { program.parse(['excess'], { from: 'user' }); }).not.toThrow(); });
test('when specify excess program argument and allowExcessArguments(false) then error', () => { const program = new commander.Command(); configureCommand(program); program .exitOverride() .allowExcessArguments(false) .allowExcessArguments(false); .action(() => {});
expect(() => { program.parse(['excess'], { from: 'user' }); }).toThrow(); });
test('when specify excess program argument and allowExcessArguments() then no error', () => { const program = new commander.Command(); configureCommand(program); program .exitOverride() .allowExcessArguments() .allowExcessArguments(); .action(() => {});
expect(() => { program.parse(['excess'], { from: 'user' }); }).not.toThrow(); });
test('when specify excess program argument and allowExcessArguments(true) then no error', () => { const program = new commander.Command(); configureCommand(program); program .exitOverride() .allowExcessArguments(true) .allowExcessArguments(true); .action(() => {});
expect(() => { program.parse(['excess'], { from: 'user' }); }).not.toThrow(); });
test('when specify excess command argument then no error (by default)', () => { const program = new commander.Command(); program .exitOverride() .command('sub') .action(() => { }); const sub = program .command('sub'); configureCommand(sub);
expect(() => { program.parse(['sub', 'excess'], { from: 'user' }); }).not.toThrow(); });
test('when specify excess command argument and allowExcessArguments(false) then error', () => { const program = new commander.Command(); program .exitOverride() const sub = program .command('sub') .allowUnknownOption() .allowExcessArguments(false) .action(() => { }); .allowExcessArguments(false); configureCommand(sub);
expect(() => { program.parse(['sub', 'excess'], { from: 'user' }); }).toThrow(); });
test('when specify expected arg and allowExcessArguments(false) then no error', () => { const program = new commander.Command(); configureCommand(program); program .argument('<file>') .exitOverride() .allowExcessArguments(false) .allowExcessArguments(false); .action(() => {});
expect(() => { program.parse(['file'], { from: 'user' }); }).not.toThrow(); });
test('when specify excess after <arg> and allowExcessArguments(false) then error', () => { const program = new commander.Command(); configureCommand(program); program .argument('<file>') .exitOverride() .allowExcessArguments(false) .allowExcessArguments(false); .action(() => {});
expect(() => { program.parse(['file', 'excess'], { from: 'user' }); }).toThrow(); });
test('when specify excess after [arg] and allowExcessArguments(false) then error', () => { const program = new commander.Command(); configureCommand(program); program .argument('[file]') .exitOverride() .allowExcessArguments(false) .allowExcessArguments(false); .action(() => {});
expect(() => { program.parse(['file', 'excess'], { from: 'user' }); }).toThrow(); });
test('when specify args for [args...] and allowExcessArguments(false) then no error', () => { const program = new commander.Command(); configureCommand(program); program .argument('[files...]') .exitOverride() .allowExcessArguments(false) .allowExcessArguments(false); .action(() => {});
expect(() => { program.parse(['file1', 'file2', 'file3'], { from: 'user' }); }).not.toThrow();2 unmodified linesmodified:tests/command.exitOverride.test.js+17 added
118 unmodified lines119 expect(stderrSpy).toHaveBeenCalled();120 expectCommanderError(caughtErr, 1, 'commander.missingArgument', "error: missing required argument 'arg-name'");121 });122 123+ 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>');128+129+ let caughtErr;130+ try {131+ program.parse(['node', 'test']);132+ } catch (err) {133+ caughtErr = err;134+ }135+136+ expect(stderrSpy).toHaveBeenCalled();137+ expectCommanderError(caughtErr, 1, 'commander.missingArgument', "error: missing required argument 'arg-name'");138+ });139+140 test('when specify excess argument then throw CommanderError', () => {141 const program = new commander.Command();142 program143 .exitOverride()177 unmodified linesmodified:index.js+25 added-13 removed
1557 unmodified lines1558 if (parsed.unknown.length > 0) {1559 this.unknownOption(parsed.unknown[0]);1560 }1561 };1562-1563- 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+ };1577 1578+ 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 lines1601 } 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