citty icon indicating copy to clipboard operation
citty copied to clipboard

fix(parser): handle short alias with = syntax (-n=value)

Open guoyangzhen opened this issue 8 months ago β€’ 5 comments

Resolves #237

Problem

-n=John with short alias n for name returns { n: "=John", name: "=John" } instead of { n: "John", name: "John" }.

Root cause: node:util parseArgs does not handle -<char>=<value> syntax for short aliases. It treats -n=John as -n with value =John.

Fix

Two-part fix in src/_parser.ts:

  1. Filter single-char aliases from allOptions β€” aliases like n should only be registered via the short property of their target option, not as standalone options. Otherwise parseArgs gets confused when n appears as both a full option and a short alias.

  2. Preprocess -<char>=<value> syntax β€” before passing args to parseArgs, expand -n=John to --name=John when n is a known alias for name. This bypasses the parseArgs limitation entirely.

Tests

  • Existing it.fails test now passes β†’ changed to it
  • Added test: multiple short aliases with = syntax
  • Added test: unknown short flag with = passes through without crash
  • 19/19 tests pass βœ…

Rebased

Rebased on latest main to resolve merge conflicts. The parser changes from upstream (alias coercion, type handling) are preserved β€” this PR now only adds the = syntax handling.

guoyangzhen avatar Mar 16 '26 13:03 guoyangzhen

[!NOTE]

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • [ ] ▢️ Resume reviews
  • [ ] πŸ” Trigger review
πŸ“ Walkthrough

Walkthrough

Parser narrows registered options to long/multi-character names (booleans, strings, defaults, multi-char aliases), collects single-character short aliases separately, and adds type-aware coercion: "false" β†’ false for booleans and boolean true β†’ "" for string-declared options.

Changes

Cohort / File(s) Summary
Parser core
src/_parser.ts
Adds isStringType, computes shortAliasNames from 1-char alias entries, restricts allOptions to long/multi-character keys (booleans/strings/defaults/multi-char aliases), and applies value coercion after parsing (string "false" β†’ boolean false for booleans; boolean true β†’ "" for string options). Alias propagation and --no- handling remain.
Parser tests
test/parser.test.ts
Converts a previously failing alias test into a normal test; adds assertions for boolean coercion with --flag=true/false and for string-declared options receiving a bare --option (coerced to empty string, not boolean).

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • unjs/citty#230: Related short-option/alias parsing with = behavior and tests for -f=bar style inputs.
  • unjs/citty#239: Related parser logic changes around type-aware coercion and alias handling.

Suggested reviewers

  • pi0

Poem

🐰 I nibble dashes, hop through names,

Short hops point where long flags claim.
"false" unmasked, booleans set right,
Strings stay strings in moonlit night.
πŸ₯•

πŸš₯ Pre-merge checks | βœ… 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
βœ… Passed checks (4 passed)
Check name Status Explanation
Title check βœ… Passed The title accurately summarizes the main change: fixing the parser to handle short aliases with the equals syntax (e.g., -n=value).
Linked Issues check βœ… Passed The PR successfully addresses issue #237 by excluding single-character alias keys from full option registration and ensuring values are correctly propagated to long options.
Out of Scope Changes check βœ… Passed All changes in the parser and tests directly relate to fixing the short alias with equals syntax issue; no out-of-scope changes detected.
Description Check βœ… Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
πŸ§ͺ Generate unit tests (beta)
  • [ ] Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❀️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

coderabbitai[bot] avatar Mar 16 '26 13:03 coderabbitai[bot]

Can you please add tests?

pi0 avatar Mar 16 '26 13:03 pi0

Done! Changed it.fails to it (the fix makes the existing test pass) and added two more edge case tests:

  • Short alias with = for boolean flags (-v=true)
  • Multiple short aliases in the same call

guoyangzhen avatar Mar 16 '26 13:03 guoyangzhen

Updated to latest upstream code. The parser fix filters single-char aliases from allOptions so they are only registered via short property. Test changed from it.fails to it.

guoyangzhen avatar Mar 21 '26 13:03 guoyangzhen

@pi0 Done! I rebased onto the latest main β€” the it.fails now passes, and I added two more edge case tests:

  1. Multiple short aliases with = syntax (-n=John -a=admin)
  2. Unknown short flag with = β€” passes through without crash

19/19 tests pass. Also squashed the previous messy commit history into a clean single commit.

guoyangzhen avatar Mar 21 '26 21:03 guoyangzhen