Throw for unsupported option flag setup (#2270)
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 2c9051a · Browse files · 3 files·+62 added−13 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+62 added−13 removed
- tests:+36 added−2 removed
- other:+26 added−11 removed
Provenance
- Commit
966720a- Tree
7150ea5- Author
John Gee <john@ruru.gen.nz>- Committer
GitHub <noreply@github.com>- Parent
2c9051a
3 files changed+62 added−13 removed
modified:lib/option.js+26 added-11 removed
310 unmodified lines311 312 function splitOptionFlags(flags) {313 let shortFlag;314 let longFlag;315- // Use original very loose parsing to maintain backwards compatibility for now,316- // which allowed for example unintended `-sw, --short-word` [sic].317- const flagParts = flags.split(/[ |,]+/);318- if (flagParts.length > 1 && !/^[[<]/.test(flagParts[1]))319- shortFlag = flagParts.shift();320- longFlag = flagParts.shift();321- // Add support for lone short flag without significantly changing parsing!322- if (!shortFlag && /^-[^-]$/.test(longFlag)) {323- shortFlag = longFlag;324- longFlag = undefined;325- }315+ // short flag, single dash and single character316+ const shortFlagExp = /^-[^-]$/;317+ // long flag, double dash and at least one character318+ const longFlagExp = /^--[^-]/;319+320+ const flagParts = flags.split(/[ |,]+/).concat('guard');321+ if (shortFlagExp.test(flagParts[0])) shortFlag = flagParts.shift();322+ if (longFlagExp.test(flagParts[0])) longFlag = flagParts.shift();323+324+ // Check for some unsupported flags that people try.325+ if (/^-[^-][^-]/.test(flagParts[0]))326+ throw new Error(327+ `invalid Option flags, short option is dash and single character: '${flags}'`,328+ );329+ if (shortFlag && shortFlagExp.test(flagParts[0]))330+ throw new Error(331+ `invalid Option flags, more than one short flag: '${flags}'`,332+ );333+ if (longFlag && longFlagExp.test(flagParts[0]))334+ throw new Error(335+ `invalid Option flags, more than one long flag: '${flags}'`,336+ );337+ // Generic error if failed to find a flag or an unexpected flag left over.338+ if (!(shortFlag || longFlag) || flagParts[0].startsWith('-'))339+ throw new Error(`invalid Option flags: '${flags}'`);340+341 return { shortFlag, longFlag };342 }343 344 exports.Option = Option;1 unmodified lineadded:tests/option.bad-flags.test.js+34 added
12345678910111213141516171819202122232425262728293031323334const { Option } = require('../');
// Check that unsupported flags throw.test.each([ { flags: '-a, -b' }, // too many short flags { flags: '-a, -b <value>' }, { flags: '-a, -b, --long' }, { flags: '--one, --two' }, // too many long flags { flags: '--one, --two [value]' }, { flags: '-ws' }, // short flag with more than one character { flags: 'sdkjhskjh' }, // oops, no flags { flags: '-a,-b' }, // try all the separators { flags: '-a|-b' }, { flags: '-a -b' },])('when construct Option with flags %p then throw', ({ flags }) => { expect(() => { new Option(flags); }).toThrow(/^invalid Option flags/);});
// Check that supported flags do not throw.test.each([ { flags: '-s' }, // single short { flags: '--long' }, // single long { flags: '-b, --both' }, // short and long { flags: '-b,--both <comma>' }, { flags: '-b|--both <bar>' }, { flags: '-b --both [space]' }, { flags: '-v, --variadic <files...>' },])('when construct Option with flags %p then do not throw', ({ flags }) => { expect(() => { new Option(flags); }).not.toThrow();});modified:tests/options.registerClash.test.js+2 added-2 removed
27 unmodified lines28293031323332333435363719 unmodified lines27 unmodified lines
test('when reuse flags in subcommand then does not throw', () => { expect(() => { const program = new Command(); program.option('e, --example'); program.command('sub').option('e, --example'); program.option('-e, --example'); program.command('sub').option('-e, --example'); }).not.toThrow(); });});
19 unmodified lines