diff --git a/src/args.ts b/src/args.ts index 686ad035..f824a553 100644 --- a/src/args.ts +++ b/src/args.ts @@ -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; diff --git a/test/args.test.ts b/test/args.test.ts index b193a27c..11a47cbe 100644 --- a/test/args.test.ts +++ b/test/args.test.ts @@ -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 ', 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.'); + }); });