Skip to content

fix: keep raw value for options with type: [String] - #178

Closed
iamhuman-cheolheelee wants to merge 1 commit into
cacjs:mainfrom
iamhuman-cheolheelee:fix/string-type-raw-value
Closed

iamhuman-cheolheelee wants to merge 1 commit into
cacjs:mainfrom
iamhuman-cheolheelee:fix/string-type-raw-value

Conversation

@iamhuman-cheolheelee

Copy link
Copy Markdown

Fixes #165 (the type: [String] part; see note below)

Root cause

src/cac.ts:282 runs mri(argv, mriOptions) before the type transform. mri turns any numeric-looking value into a number (+310000 -> 310000, 01234 -> 1234), and only after that does setByType (src/utils.ts:126) call String(value). The original text is already lost, so type: [String] can't bring it back. getMriOptions (src/utils.ts:51) never filled mri's string list.

Fix

getMriOptions now adds every name of an option that takes a value and has type: [String] to mri's string list, so mri keeps the argv text as-is. The existing transform still wraps it in an array.
Invariant: when an option is declared with type: [String], each value it produces is exactly the argv string the user passed.
Names come from rawName (the same kebab form mri sees), so --phone-number and short aliases are covered too.

Edge cases covered

  • leading zeros (01234), leading +, exponent (1e3), hex (0x10)
  • kebab-case long name plus short alias (-p)
  • repeated option (several values)
  • CJK value, empty string value
  • options without type, or with type: [], still get mri's numeric coercion (no behavior change)

Behavior change: an optional [value] option with type: [String] passed without a value now gives [''] instead of ['true'] (that's mri's string semantics). Boolean flags aren't affected.

Cost: one extra loop over the option names when building the mri options, O(options). Nothing new on the per-argument path.

Testing

  • Added array types with String transformFunction keep raw values in tests/index.test.ts. It fails on main (['310000']) and passes with the fix.
  • pnpm vitest run 18/18, pnpm lint clean, pnpm typecheck clean.

Notes

Quoting in the shell (-c "1234") never reaches the program, so an option without a type can't tell a quoted value from an unquoted one. type: [String] is how a user opts in to raw strings, and that's the case this PR fixes.

AI-assisted: I used Claude Code while investigating; I reviewed, ran and verified every change myself.

cc @peetzweg @sxzz

Closes #165

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@iamhuman-cheolheelee iamhuman-cheolheelee closed this by deleting the head repository Oct 23, 2026
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