Skip to content

fix: keep String-typed option values as strings instead of coercing t… - #185

Open
jnrahme wants to merge 1 commit into
cacjs:mainfrom
jnrahme:fix/string-typed-option-leading-zeros
Open

jnrahme wants to merge 1 commit into
cacjs:mainfrom
jnrahme:fix/string-typed-option-leading-zeros

Conversation

@jnrahme

@jnrahme jnrahme commented Dec 2, 2026

Copy link
Copy Markdown

Fixes #165

This supersedes the withdrawn #178, which was closed by its author on Oct 23 with no review or comments. It fixes the same type: [String] case with the same approach (keep the raw value, no numeric coercion).

An option declared with type: [String] lost leading zeros: -c 01234 came back as 1234.

Root cause: cac's forked mri parser coerces every numeric-looking value with +val before cac's own type transform runs, so by the time String(...) is applied the original text is already gone — String(1234) is "1234", not "01234". The shell strips the quotes, so cac cannot tell a quoted value from an unquoted one; the declared type is the only signal it has.

This change:

  • Adds a string: string[] list to MriOptions (src/mri.ts). toVal keeps the raw text for keys in that list instead of coercing them to numbers (aliases included).
  • getMriOptions (src/utils.ts) populates the list from every option whose config.type?.[0] === String, for all of the option's names.
  • Options without type: [String] keep the existing numeric coercion, so -c 1234 on an untyped option still yields 1234.

Tests: Added String-typed option keeps leading zeros in tests/index.test.ts: cli.parse('node bin -c 01234') with type: [String] now yields options.chars === ['01234'] and options.c === '01234'. It failed before the fix (['1234']). Full suite: pnpm vitest run — 23 passed, 0 failed. pnpm typecheck and pnpm lint are clean.

Notes for the maintainer:

  • AI assistance: I used AI assistance to draft this fix, following the approach in the issue; I checked the fix against the current code and the existing tests myself.
  • The short alias keeps the raw string value (options.c === '01234'), while the canonical name is array-wrapped by the existing setByType behaviour (options.chars === ['01234']). That matches how aliases already behave for other typed options; I did not change it.
  • This does not change the untyped case from the issue: without type: [String] there is no way for cac to know the value should stay a string, and numeric coercion is the documented default.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: quoted string is treated as a number

1 participant