Skip to content
Myr

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

3 files+62 added−13 removed

modified:lib/option.js+26 added-11 removed
310 unmodified lines
311312function 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 = /^--[^-]/;319320 const flagParts = flags.split(/[ |,]+/).concat('guard');321 if (shortFlagExp.test(flagParts[0])) shortFlag = flagParts.shift();322 if (longFlagExp.test(flagParts[0])) longFlag = flagParts.shift();323324 // 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}'`);340341 return { shortFlag, longFlag };342}343344exports.Option = Option;
1 unmodified line
added:tests/option.bad-flags.test.js+34 added
modified:tests/options.registerClash.test.js+2 added-2 removed