Skip to content
Myr

List excess arguments in error message (#2384)

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

* List excess arguments in error message * Fixed logic and reverted changelog * Show all arguments in error message in case typo is somewhere earlier * Restore trailing spaces in CHANGELOG.md

authored by stiff · committed by GitHub · parent 395cf71 · Browse files · 2 files+5 added−3 removed
This commit

State

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

Diff stats

2 files+5 added−3 removed

  • tests:+2 added−2 removed
  • other:+3 added−1 removed

2 files+5 added−3 removed

modified:lib/command.js+3 added-1 removed
2147 unmodified lines
2148 if (this._allowExcessArguments) return;21492150 const expected = this.registeredArguments.length;2151 const s = expected === 1 ? '' : 's';2152 const received = receivedArgs.length;2153 const forSubcommand = this.parent ? ` for '${this.name()}'` : '';2154 const details = receivedArgs.join(', ');2153 const message = `error: too many arguments${forSubcommand}. Expected ${expected} argument${s} but got ${receivedArgs.length}.`;2155 const message = `error: too many arguments${forSubcommand}. Expected ${expected} argument${s} but got ${received}: ${details}.`;2156 this.error(message, { code: 'commander.excessArguments' });2157 }21582159 /**
621 unmodified lines
modified:tests/command.exitOverride.test.js+2 added-2 removed
181 unmodified lines
182 expectCommanderError(183 caughtErr,184 1,185 'commander.excessArguments',186 'error: too many arguments. Expected 0 arguments but got 1.',186 'error: too many arguments. Expected 0 arguments but got 1: excess.',187 );188 });189190 test('when specify command with excess argument then throw CommanderError', () => {
15 unmodified lines
206 expectCommanderError(207 caughtErr,208 1,209 'commander.excessArguments',210 "error: too many arguments for 'speak'. Expected 0 arguments but got 1.",210 "error: too many arguments for 'speak'. Expected 0 arguments but got 1: excess.",211 );212 });213214 test('when specify --help then throw CommanderError', () => {
246 unmodified lines