From 690c7df75ececc5a3a1d8c7040ee002f6af5534b Mon Sep 17 00:00:00 2001 From: ybai08 <271969568+ybai08@users.noreply.github.com> Date: Sat, 17 Oct 2026 17:22:34 -0500 Subject: [PATCH] fix: keep args after boolean options as strings --- src/cac.ts | 3 ++- src/utils.ts | 42 ++++++++++++++++++++++++++++++++++++++++++ tests/index.test.ts | 22 ++++++++++++++++++++++ 3 files changed, 66 insertions(+), 1 deletion(-) diff --git a/src/cac.ts b/src/cac.ts index 375ff6cd..b5918b36 100644 --- a/src/cac.ts +++ b/src/cac.ts @@ -11,6 +11,7 @@ import { camelcaseOptionName, getFileName, getMriOptions, + inlineBooleanValues, setByType, setDotProp, } from './utils.ts' @@ -280,7 +281,7 @@ export class CAC extends EventTarget { argv = argv.slice(0, doubleDashesIndex) } - let parsed = mri(argv, mriOptions) + let parsed = mri(inlineBooleanValues(argv, mriOptions), mriOptions) parsed = Object.keys(parsed).reduce( (res, name) => { return { diff --git a/src/utils.ts b/src/utils.ts index 099642ea..5065b567 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -85,6 +85,48 @@ export function getMriOptions(options: Option[]): MriOptions { return result } +/** + * mri pushes the arg after a boolean option to the positional args, but casts + * it to a number first: `--watch 03` gives `[3]`, while `03` alone gives `['03']`. + * Pass `true` inline (`--watch=true`) so that mri parses the next arg as + * an ordinary positional arg instead. + */ +export function inlineBooleanValues( + argv: string[], + mriOptions: MriOptions, +): string[] { + const booleans = new Set( + mriOptions.boolean.flatMap((name) => [ + name, + ...(mriOptions.alias[name] || []), + ]), + ) + + return argv.map((arg, i) => { + const next = argv[i + 1] + // Only an option without an inline value takes the next arg, and mri does + // not push an empty, dashed, `true` or `false` value to the positional args + if ( + !arg.startsWith('-') || + arg.includes('=') || + next === undefined || + next === '' || + next.startsWith('-') || + next === 'true' || + next === 'false' + ) { + return arg + } + + const name = arg.replace(/^-+/, '') + if (name.startsWith('no-')) return arg + + // Like mri, treat `-abc` as `-a -b -c`, so only `c` takes the next arg + const lastName = arg.length - name.length === 2 ? name : name.slice(-1) + return booleans.has(lastName) ? `${arg}=true` : arg + }) +} + export function findLongest(arr: string[]): string { return arr.sort((a, b) => b.length - a.length)[0] } diff --git a/tests/index.test.ts b/tests/index.test.ts index 6927d442..2cf8ec30 100644 --- a/tests/index.test.ts +++ b/tests/index.test.ts @@ -81,6 +81,28 @@ test('double dashes', () => { expect(options['--']).toEqual(['npm', 'test']) }) +test('keep args after boolean options as strings', () => { + const cli = cac() + + cli.option('-r, --run', 'run').option('-w, --watch', 'watch') + + const { args, options } = cli.parse(`node bin --run 03 04 -w 1e3`.split(' ')) + expect(args).toEqual(['03', '04', '1e3']) + expect(options).toEqual({ + '--': [], + r: true, + run: true, + w: true, + watch: true, + }) + + expect(cli.parse(`node bin -rw 0x10`.split(' ')).args).toEqual(['0x10']) + expect(cli.parse(`node bin --run false 1`.split(' '))).toEqual({ + args: ['1'], + options: { '--': [], r: false, run: false }, + }) +}) + test('default value for negated option', () => { const cli = cac()