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/cac.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
camelcaseOptionName,
getFileName,
getMriOptions,
inlineBooleanValues,
setByType,
setDotProp,
} from './utils.ts'
Expand Down Expand Up @@ -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 {
Expand Down
42 changes: 42 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}
Expand Down
22 changes: 22 additions & 0 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down