Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ export function findOptionError(argv: string[], options: OptionDef[]): string |
}
if (eqIdx === -1 && !schema.booleans.has(key)) {
const value = argv[i + 1];
if (value === undefined || value.startsWith('-')) {
if (value === undefined || (value.startsWith('-')
&& !(schema.numbers.has(key) && Number.isFinite(Number(value))))) {
return `Option --${rawKey} requires a value.`;
}
i += 2;
Expand Down
20 changes: 20 additions & 0 deletions test/args.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,24 @@ describe('scanCommandPath', () => {
.toContain('requires a value');
}
});

it.each(['-2', '-0.5', '-1e2'])('accepts a separate negative numeric value: %s', (value) => {
const options: OptionDef[] = [
{ flag: '--pitch <n>', description: 'Pitch adjustment', type: 'number' },
];
const argv = ['speech', 'synthesize', '--pitch', value];

expect(findOptionError(argv, options)).toBeUndefined();
expect(parseFlags(argv, options).pitch).toBe(Number(value));
});

it('still rejects an option in place of a numeric value', () => {
expect(findOptionError(['--timeout', '--verbose'], OPTIONS))
.toBe('Option --timeout requires a value.');
});

it('still rejects a leading dash in a separate string value', () => {
expect(findOptionError(['--message', '-text'], OPTIONS))
.toBe('Option --message requires a value.');
});
});
Loading