commander
Advanced tools
Comparing version 13.0.0 to 13.1.0
@@ -21,3 +21,3 @@ const { InvalidArgumentError } = require('./error.js'); | ||
const optionFlags = splitOptionFlags(flags); | ||
this.short = optionFlags.shortFlag; | ||
this.short = optionFlags.shortFlag; // May be a short flag, undefined, or even a long flag (if option has two long flags). | ||
this.long = optionFlags.longFlag; | ||
@@ -325,21 +325,40 @@ this.negate = false; | ||
const flagParts = flags.split(/[ |,]+/).concat('guard'); | ||
// Normal is short and/or long. | ||
if (shortFlagExp.test(flagParts[0])) shortFlag = flagParts.shift(); | ||
if (longFlagExp.test(flagParts[0])) longFlag = flagParts.shift(); | ||
// Long then short. Rarely used but fine. | ||
if (!shortFlag && shortFlagExp.test(flagParts[0])) | ||
shortFlag = flagParts.shift(); | ||
// Allow two long flags, like '--ws, --workspace' | ||
// This is the supported way to have a shortish option flag. | ||
if (!shortFlag && longFlagExp.test(flagParts[0])) { | ||
shortFlag = longFlag; | ||
longFlag = flagParts.shift(); | ||
} | ||
// Check for some unsupported flags that people try. | ||
if (/^-[^-][^-]/.test(flagParts[0])) | ||
// Check for unprocessed flag. Fail noisily rather than silently ignore. | ||
if (flagParts[0].startsWith('-')) { | ||
const unsupportedFlag = flagParts[0]; | ||
const baseError = `option creation failed due to '${unsupportedFlag}' in option flags '${flags}'`; | ||
if (/^-[^-][^-]/.test(unsupportedFlag)) | ||
throw new Error( | ||
`${baseError} | ||
- a short flag is a single dash and a single character | ||
- either use a single dash and a single character (for a short flag) | ||
- or use a double dash for a long option (and can have two, like '--ws, --workspace')`, | ||
); | ||
if (shortFlagExp.test(unsupportedFlag)) | ||
throw new Error(`${baseError} | ||
- too many short flags`); | ||
if (longFlagExp.test(unsupportedFlag)) | ||
throw new Error(`${baseError} | ||
- too many long flags`); | ||
throw new Error(`${baseError} | ||
- unrecognised flag format`); | ||
} | ||
if (shortFlag === undefined && longFlag === undefined) | ||
throw new Error( | ||
`invalid Option flags, short option is dash and single character: '${flags}'`, | ||
`option creation failed due to no flags found in '${flags}'.`, | ||
); | ||
if (shortFlag && shortFlagExp.test(flagParts[0])) | ||
throw new Error( | ||
`invalid Option flags, more than one short flag: '${flags}'`, | ||
); | ||
if (longFlag && longFlagExp.test(flagParts[0])) | ||
throw new Error( | ||
`invalid Option flags, more than one long flag: '${flags}'`, | ||
); | ||
// Generic error if failed to find a flag or an unexpected flag left over. | ||
if (!(shortFlag || longFlag) || flagParts[0].startsWith('-')) | ||
throw new Error(`invalid Option flags: '${flags}'`); | ||
@@ -346,0 +365,0 @@ return { shortFlag, longFlag }; |
{ | ||
"name": "commander", | ||
"version": "13.0.0", | ||
"version": "13.1.0", | ||
"description": "the complete solution for node.js command-line programs", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -178,4 +178,12 @@ # Commander.js | ||
Options are defined with the `.option()` method, also serving as documentation for the options. Each option can have a short flag (single character) and a long name, separated by a comma or space or vertical bar ('|'). | ||
Options are defined with the `.option()` method, also serving as documentation for the options. Each option can have a short flag (single character) and a long name, separated by a comma or space or vertical bar ('|'). To allow a wider range of short-ish flags than just | ||
single characters, you may also have two long options. Examples: | ||
```js | ||
program | ||
.option('-p, --port <number>', 'server port number') | ||
.option('--trace', 'add extra debugging output') | ||
.option('--ws, --workspace <name>', 'use a custom workspace') | ||
``` | ||
The parsed options can be accessed by calling `.opts()` on a `Command` object, and are passed to the action handler. | ||
@@ -182,0 +190,0 @@ |
@@ -620,3 +620,3 @@ // Type definitions for commander | ||
* .option('-p, --pepper', 'add pepper') | ||
* .option('-p, --pizza-type <TYPE>', 'type of pizza') // required option-argument | ||
* .option('--pt, --pizza-type <TYPE>', 'type of pizza') // required option-argument | ||
* .option('-c, --cheese [CHEESE]', 'add extra cheese', 'mozzarella') // optional option-argument with default | ||
@@ -623,0 +623,0 @@ * .option('-t, --tip <VALUE>', 'add tip to purchase cost', parseFloat) // custom parse function |
Sorry, the diff of this file is too big to display
200514
4589
1150