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
4 changes: 3 additions & 1 deletion src/cac.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,10 +329,12 @@ export class CAC extends EventTarget {
if (key !== '_') {
const keys = key.split('.')
setDotProp(options, keys, value)
setByType(options, transforms)
}
}

// Apply type transforms once, after all values (including dot-nested ones) are set
setByType(options, transforms)

return {
args,
options,
Expand Down
22 changes: 22 additions & 0 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,28 @@ test('array types with transformFunction', () => {
expect(options.scale).toEqual(true)
})

test('array types apply transformFunction once', () => {
const cli = cac()

cli
.option('-c, --color <hex>', 'Color as hex', {
type: [(v: string) => Number.parseInt(v, 16)],
})
.option('--verbose', 'Verbose output')

const { options } = cli.parse(`node bin --color ff --verbose`.split(' '))
expect(options.color).toEqual([255])
})

test('array types keep every dot-nested value', () => {
const cli = cac()

cli.option('--env <env>', 'Set env', { type: [] })

const { options } = cli.parse(`node bin --env.a 1 --env.b 2`.split(' '))
expect(options.env).toEqual([{ a: 1, b: 2 }])
})

test('throw on unknown options', () => {
const cli = cac()

Expand Down