Skip to content
Myr

Add combineFlagAndOptionalValue (#1326)

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

* Add combineFlagAndOptionalValue * Add combineFlagAndOptionalValue to migration tips * Add missing minus * Reorder example

authored by John Gee · committed by GitHub · parent 6b2e42c · Browse files · 5 files+60 added−1 removed
This commit

State

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

Diff stats

5 files+60 added−1 removed

  • tests:+21 added−0 removed
  • other:+39 added−1 removed

5 files+60 added−1 removed

modified:tests/command.parseOptions.test.js+21 added
286 unmodified lines
287 const result = program.parseOptions(['--rrr=value']);288 expect(result).toEqual({ operands: [], unknown: ['--rrr=value'] });289 expect(program.opts()).toEqual({ });290 });291292 test('when program has combo optional and combineFlagAndOptionalValue() then treat as value', () => {293 const program = createProgram();294 program.combineFlagAndOptionalValue();295 program.parseOptions(['-db']);296 expect(program.opts()).toEqual({ ddd: 'b' });297 });298299 test('when program has combo optional and combineFlagAndOptionalValue(true) then treat as value', () => {300 const program = createProgram();301 program.combineFlagAndOptionalValue(true);302 program.parseOptions(['-db']);303 expect(program.opts()).toEqual({ ddd: 'b' });304 });305306 test('when program has combo optional and combineFlagAndOptionalValue(false) then treat as boolean', () => {307 const program = createProgram();308 program.combineFlagAndOptionalValue(false);309 program.parseOptions(['-db']);310 expect(program.opts()).toEqual({ ddd: true, bbb: true });311 });312});
modified:typings/commander-tests.ts+4 added
132 unmodified lines
133// passCommandToAction134const passCommandToActionThis1: commander.Command = program.passCommandToAction();135const passCommandToActionThis2: commander.Command = program.passCommandToAction(false);136137// combineFlagAndOptionalValue138const combineFlagAndOptionalValueThis1: commander.Command = program.combineFlagAndOptionalValue();139const combineFlagAndOptionalValueThis2: commander.Command = program.combineFlagAndOptionalValue(false);140141// allowUnknownOption142const allowUnknownOptionThis1: commander.Command = program.allowUnknownOption();143const allowUnknownOptionThis2: commander.Command = program.allowUnknownOption(false);144
83 unmodified lines
modified:typings/index.d.ts+12 added
200 unmodified lines
201 */202 passCommandToAction(value?: boolean): this;203204 /**205 * Alter parsing of short flags with optional values.206 *207 * @example208 * // for `.option('-f,--flag [value]'):209 * .combineFlagAndOptionalValue(true) // `-f80` is treated like `--flag=80`, this is the default behaviour210 * .combineFlagAndOptionalValue(false) // `-fb` is treated like `-f -b`211 *212 * @returns `this` command for chaining213 */214 combineFlagAndOptionalValue(arg?: boolean): this;215216 /**217 * Allow unknown options on the command line.218 *219 * @param [arg] if `true` or omitted, no error will be thrown for unknown options.220 * @returns `this` command for chaining
179 unmodified lines
modified:CHANGELOG.md+3 added
112 unmodified lines
113If you use `program.args` or more complicated tests to detect a missing subcommand, you may be able to delete the code and rely on the default behaviour.114115If you use `.command('*')` to add a default command, you may be be able to switch to `isDefault:true` with a named command.116117If you want to continue combining short options with optional values as though they were boolean flags, set `combineFlagAndOptionalValue(false)`118to expand `-fb` to `-f -b` rather than `-f b`.119120## [5.0.0-4] (2020-03-03)121122(Released in 5.0.0)123
181 unmodified lines
modified:index.js+20 added-1 removed
125 unmodified lines
126 this._executableFile = null; // custom name for executable127 this._defaultCommandName = null;128 this._exitCallback = null;129 this._aliases = [];130 this._combineFlagAndOptionalValue = true;131132 this._hidden = false;133 this._hasHelpOption = true;134 this._helpFlags = '-h, --help';
61 unmodified lines
196 cmd._helpCommandDescription = this._helpCommandDescription;197 cmd._exitCallback = this._exitCallback;198 cmd._storeOptionsAsProperties = this._storeOptionsAsProperties;199 cmd._passCommandToAction = this._passCommandToAction;200 cmd._combineFlagAndOptionalValue = this._combineFlagAndOptionalValue;201202 cmd._executableFile = opts.executableFile || null; // Custom name for executable file, set missing to null to match constructor203 this.commands.push(cmd);204 cmd._parseExpectedArgs(args);
435 unmodified lines
640 return this._optionEx({ mandatory: true }, flags, description, fn, defaultValue);641 };642643 /**644 * Alter parsing of short flags with optional values.645 *646 * Examples:647 *648 * // for `.option('-f,--flag [value]'):649 * .combineFlagAndOptionalValue(true) // `-f80` is treated like `--flag=80`, this is the default behaviour650 * .combineFlagAndOptionalValue(false) // `-fb` is treated like `-f -b`651 *652 * @param {Boolean} [arg] - if `true` or omitted, an optional value can be specified directly after the flag.653 * @api public654 */655 combineFlagAndOptionalValue(arg) {656 this._combineFlagAndOptionalValue = (arg === undefined) || arg;657 return this;658 };659660 /**661 * Allow unknown options on the command line.662 *663 * @param {Boolean} [arg] - if `true` or omitted, no error will be thrown664 * for unknown options.
464 unmodified lines
1129 // Look for combo options following single dash, eat first one if known.1130 if (arg.length > 2 && arg[0] === '-' && arg[1] !== '-') {1131 const option = this._findOption(`-${arg[1]}`);1132 if (option) {1114 if (option.required || option.optional) {1133 if (option.required || (option.optional && this._combineFlagAndOptionalValue)) {1134 // option with value following in same argument1135 this.emit(`option:${option.name()}`, arg.slice(2));1136 } else {1137 // boolean option, emit and put back remainder of arg for further processing
745 unmodified lines