diff --git a/src/cac.ts b/src/cac.ts index 375ff6cd..87fb7db1 100644 --- a/src/cac.ts +++ b/src/cac.ts @@ -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, diff --git a/tests/index.test.ts b/tests/index.test.ts index 6927d442..725d899a 100644 --- a/tests/index.test.ts +++ b/tests/index.test.ts @@ -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 ', '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 ', '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()