From 2f0b47e281d3bf63461d8feb8e4f7e1621ff5d6c Mon Sep 17 00:00:00 2001 From: Shaurya Kesarwani Date: Mon, 26 Oct 2026 13:52:14 +0530 Subject: [PATCH] fix(parse): apply array type transform once per parse setByType ran inside the loop over parsed keys, so a type transform was re-applied once per remaining key (aliases included), and wrapping a dot-nested option in an array mid-loop made later keys like `--env.b` land on the array instead of the object. --- src/cac.ts | 4 +++- tests/index.test.ts | 22 ++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) 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()