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
6 changes: 5 additions & 1 deletion src/args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,11 @@ export function parseFlags(argv: string[], options: OptionDef[]): GlobalFlags {
if (arr) arr.push(value);
else (flags as Record<string, unknown>)[camelKey] = [value];
} else if (schema.numbers.has(camelKey)) {
(flags as Record<string, unknown>)[camelKey] = Number(value);
const numericValue = Number(value);
if (value.trim() === '' || !Number.isFinite(numericValue)) {
throw new Error(`Flag --${key} requires a numeric value, got "${value}".`);
}
(flags as Record<string, unknown>)[camelKey] = numericValue;
} else {
(flags as Record<string, unknown>)[camelKey] = value;
}
Expand Down
28 changes: 28 additions & 0 deletions test/args.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { describe, it, expect } from 'bun:test';
import { parseFlags } from '../src/args';
import type { OptionDef } from '../src/command';

const OPTIONS: OptionDef[] = [
{ flag: '--timeout <seconds>', description: 'Request timeout', type: 'number' },
{ flag: '--message <text>', description: 'Message text', type: 'array' },
];

describe('parseFlags', () => {
it('rejects non-numeric values for number flags', () => {
expect(() => parseFlags(['--timeout', 'abc'], OPTIONS)).toThrow(
'Flag --timeout requires a numeric value, got "abc".',
);
});

it('rejects empty values for number flags', () => {
expect(() => parseFlags(['--timeout='], OPTIONS)).toThrow(
'Flag --timeout requires a numeric value, got "".',
);
});

it('still accepts finite numeric values', () => {
const flags = parseFlags(['--timeout', '1.5'], OPTIONS);

expect(flags.timeout).toBe(1.5);
});
});
Loading