Skip to content

Commit

Permalink
Add strictOptionalOptionArguments
Browse files Browse the repository at this point in the history
  • Loading branch information
aweebit committed Aug 7, 2023
1 parent d7e2399 commit 1517423
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
18 changes: 17 additions & 1 deletion lib/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class Command extends EventEmitter {
this._defaultCommandName = null;
this._exitCallback = null;
this._aliases = [];
this._strictOptionalOptionArguments = false;
this._combineFlagAndOptionalValue = true;
this._description = '';
this._summary = '';
Expand Down Expand Up @@ -100,6 +101,7 @@ class Command extends EventEmitter {
this._helpConfiguration = sourceCommand._helpConfiguration;
this._exitCallback = sourceCommand._exitCallback;
this._storeOptionsAsProperties = sourceCommand._storeOptionsAsProperties;
this._strictOptionalOptionArguments = sourceCommand._strictOptionalOptionArguments;
this._combineFlagAndOptionalValue = sourceCommand._combineFlagAndOptionalValue;
this._allowExcessArguments = sourceCommand._allowExcessArguments;
this._enablePositionalOptions = sourceCommand._enablePositionalOptions;
Expand Down Expand Up @@ -675,6 +677,20 @@ Expecting one of '${allowedValues.join("', '")}'`);
return this._optionEx({ mandatory: true }, flags, description, fn, defaultValue);
}

/**
* When set to `true`, handle options with optional option-arguments as prescribed by POSIX,
* i.e. only allow providing values for such option-arguments by combining them with a flag
* (e.g. `-cmozzarella`, `--cheese=mozzarella`).
*
* @param {Boolean} [strict=true]
* @return {Command} `this` command for chaining
*/

strictOptionalOptionArguments(strict = true) {
this._strictOptionalOptionArguments = !!strict;
return this;
}

/**
* Alter parsing of short flags with optional values.
*
Expand Down Expand Up @@ -1471,7 +1487,7 @@ Expecting one of '${allowedValues.join("', '")}'`);
} else if (option.optional) {
let value = null;
// historical behaviour is optional value is following arg unless an option
if (args.length > 0 && !isFlag(args[0])) {
if (!(this._strictOptionalOptionArguments) && args.length > 0 && !isFlag(args[0])) {
value = args.shift();
}
this.emit(`option:${option.name()}`, value);
Expand Down
10 changes: 10 additions & 0 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,16 @@ export class Command {
*/
getOptionValueSourceWithGlobals(key: string): OptionValueSource | undefined;

/**
* When set to `true`, handle options with optional option-arguments as prescribed by POSIX,
* i.e. only allow providing values for such option-arguments by combining them with a flag
* (e.g. `-cmozzarella`, `--cheese=mozzarella`).
*
* @returns `this` command for chaining
*/

strictOptionalOptionArguments(strict?: boolean): this;

/**
* Alter parsing of short flags with optional values.
*
Expand Down

0 comments on commit 1517423

Please sign in to comment.