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
Provenance
- Commit
84fe0da- Tree
02c7f01- Author
John Gee <john@ruru.gen.nz>- Committer
GitHub <noreply@github.com>- Parent
6b2e42c
5 files changed+60 added−1 removed
modified:tests/command.parseOptions.test.js+21 added
286 unmodified lines287 const result = program.parseOptions(['--rrr=value']);288 expect(result).toEqual({ operands: [], unknown: ['--rrr=value'] });289 expect(program.opts()).toEqual({ });290 });291+292+ 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+ });298+299+ 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+ });305+306+ 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 lines133 // passCommandToAction134 const passCommandToActionThis1: commander.Command = program.passCommandToAction();135 const passCommandToActionThis2: commander.Command = program.passCommandToAction(false);136 137+// combineFlagAndOptionalValue138+const combineFlagAndOptionalValueThis1: commander.Command = program.combineFlagAndOptionalValue();139+const combineFlagAndOptionalValueThis2: commander.Command = program.combineFlagAndOptionalValue(false);140+141 // allowUnknownOption142 const allowUnknownOptionThis1: commander.Command = program.allowUnknownOption();143 const allowUnknownOptionThis2: commander.Command = program.allowUnknownOption(false);144 83 unmodified linesmodified:typings/index.d.ts+12 added
200 unmodified lines201 */202 passCommandToAction(value?: boolean): this;203 204 /**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;215+216+ /**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 chaining179 unmodified linesmodified:CHANGELOG.md+3 added
112 unmodified lines113 If 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.114 115 If you use `.command('*')` to add a default command, you may be be able to switch to `isDefault:true` with a named command.116 117+If you want to continue combining short options with optional values as though they were boolean flags, set `combineFlagAndOptionalValue(false)`118+to expand `-fb` to `-f -b` rather than `-f b`.119+120 ## [5.0.0-4] (2020-03-03)121 122 (Released in 5.0.0)123 181 unmodified linesmodified:index.js+20 added-1 removed
125 unmodified lines126 this._executableFile = null; // custom name for executable127 this._defaultCommandName = null;128 this._exitCallback = null;129 this._aliases = [];130+ this._combineFlagAndOptionalValue = true;131 132 this._hidden = false;133 this._hasHelpOption = true;134 this._helpFlags = '-h, --help';61 unmodified lines196 cmd._helpCommandDescription = this._helpCommandDescription;197 cmd._exitCallback = this._exitCallback;198 cmd._storeOptionsAsProperties = this._storeOptionsAsProperties;199 cmd._passCommandToAction = this._passCommandToAction;200+ cmd._combineFlagAndOptionalValue = this._combineFlagAndOptionalValue;201 202 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 lines640 return this._optionEx({ mandatory: true }, flags, description, fn, defaultValue);641 };642 643 /**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+ };659+660+ /**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 lines1129 // 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 processing745 unmodified lines