Skip to content

Commit 028db14

Browse files
authored
Merge pull request #24 from StatelessStudio/v2.0.2
V2.0.2
2 parents b0c3813 + d6f847a commit 028db14

4 files changed

Lines changed: 38 additions & 3 deletions

File tree

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ts-commands",
3-
"version": "2.0.1",
3+
"version": "2.0.2",
44
"description": "ts-commands",
55
"private": "true",
66
"typescript-template": {

src/argument-parser.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,14 @@ export class ArgumentParser {
180180
`Invalid value for argument: ${option.key} ${displayValue}`
181181
);
182182
}
183+
184+
if (!this.validateChoices(option, value)) {
185+
const choices = option.choices.join(', ');
186+
187+
throw new ArgumentError(
188+
`Invalid choice for argument: ${option.key} (${value}). Allowed choices are [${choices}]`
189+
);
190+
}
183191
}
184192

185193
protected isValid(option: CommandOption, value: ArgumentValue): boolean {
@@ -199,4 +207,15 @@ export class ArgumentParser {
199207
);
200208
}
201209
}
210+
211+
protected validateChoices(
212+
option: CommandOption,
213+
value: ArgumentValue
214+
): boolean {
215+
if (option.choices) {
216+
return (<string[]>option.choices).includes(<string>value);
217+
}
218+
219+
return true;
220+
}
202221
}

test/spec/argument-parser.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,4 +196,20 @@ describe('ArgumentParser', () => {
196196
'Invalid positional argument: extra.txt'
197197
);
198198
});
199+
200+
it('throws an error for invalid choice', () => {
201+
command.positional = [];
202+
command.options.push({
203+
key: 'color',
204+
type: OptionType.string,
205+
choices: ['red', 'green', 'blue'],
206+
});
207+
command.init();
208+
209+
const args = ['--color=yellow'];
210+
expect(() => parser.parse(args)).toThrowError(
211+
'Invalid choice for argument: color (yellow).' +
212+
' Allowed choices are [red, green, blue]'
213+
);
214+
});
199215
});

0 commit comments

Comments
 (0)