Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for variadic to choices #1454

Merged
merged 1 commit into from Jan 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 17 additions & 6 deletions index.js
Expand Up @@ -425,6 +425,18 @@ class Option {
return this;
};

/**
* @api private
*/

_concatValue(value, previous) {
if (previous === this.defaultValue || !Array.isArray(previous)) {
return [value];
}

return previous.concat(value);
}

/**
* Only allow option value to be one of choices.
*
Expand All @@ -434,10 +446,13 @@ class Option {

choices(values) {
this.argChoices = values;
this.parseArg = (arg) => {
this.parseArg = (arg, previous) => {
if (!values.includes(arg)) {
throw new InvalidOptionArgumentError(`Allowed choices are ${values.join(', ')}.`);
}
if (this.variadic) {
return this._concatValue(arg, previous);
}
return arg;
};
return this;
Expand Down Expand Up @@ -976,11 +991,7 @@ class Command extends EventEmitter {
throw err;
}
} else if (val !== null && option.variadic) {
if (oldValue === defaultValue || !Array.isArray(oldValue)) {
val = [val];
} else {
val = oldValue.concat(val);
}
val = option._concatValue(val, oldValue);
}

// unassigned or boolean value
Expand Down
18 changes: 18 additions & 0 deletions tests/options.variadic.test.js
Expand Up @@ -40,6 +40,24 @@ describe('variadic option with required value', () => {
expect(program.opts().required).toEqual(['one', 'two']);
});

test('when variadic used with choices and one value then set in array', () => {
const program = new commander.Command();
program
.addOption(new commander.Option('-r,--required <value...>').choices(['one', 'two']));

program.parse(['--required', 'one'], { from: 'user' });
expect(program.opts().required).toEqual(['one']);
});

test('when variadic used with choices and two values then set in array', () => {
const program = new commander.Command();
program
.addOption(new commander.Option('-r,--required <value...>').choices(['one', 'two']));

program.parse(['--required', 'one', 'two'], { from: 'user' });
expect(program.opts().required).toEqual(['one', 'two']);
});

test('when variadic with short combined argument then not variadic', () => {
const program = new commander.Command();
program
Expand Down