From f31095ba89ebe6c1fab22928eec6bf53e0583ccd Mon Sep 17 00:00:00 2001 From: edram Date: Thu, 17 Sep 2026 11:24:33 +0800 Subject: [PATCH 1/2] test: reproduce omitted array option bug Capture the unexpected array value when an unrelated option is passed. The regression test intentionally fails until the parser is fixed. Refs: #175 Co-Authored-By: Codex GPT-6 --- tests/index.test.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/index.test.ts b/tests/index.test.ts index bbb176b4..d97b8917 100644 --- a/tests/index.test.ts +++ b/tests/index.test.ts @@ -151,6 +151,23 @@ test('array types with transformFunction', () => { expect(options.scale).toEqual(true) }) +test('omitted array option stays absent when another option is passed', () => { + const cli = cac() + .option('--type ', 'Choose a project type', { + default: 'node', + }) + .option('--externals ', 'Add externals', { + type: [], + }) + + const { options: withoutOption } = cli.parse(['node', 'bin']) + expect(withoutOption).not.toHaveProperty('externals') + + const { options: withOption } = cli.parse(['node', 'bin', '--type', 'node']) + expect(withOption.type).toBe('node') + expect(withOption).not.toHaveProperty('externals') +}) + test('throw on unknown options', () => { const cli = cac() From ce4aa8035bcb0b11f24e11ee5c4f0264f4374341 Mon Sep 17 00:00:00 2001 From: edram Date: Thu, 17 Sep 2026 12:40:48 +0800 Subject: [PATCH 2/2] fix: ignore absent array options Avoid converting array options that were not provided. Fixes: #175 Co-Authored-By: Codex GPT-6 --- src/utils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils.ts b/src/utils.ts index 1ce99e03..706266d6 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -128,7 +128,7 @@ export function setByType( transforms: { [k: string]: any }, ): void { for (const [key, transform] of Object.entries(transforms)) { - if (transform.shouldTransform) { + if (transform.shouldTransform && Object.hasOwn(obj, key)) { obj[key] = [obj[key]].flat() if (typeof transform.transformFunction === 'function') {