fix(parse): allow hyphen-prefixed flag values#715
Conversation
📝 WalkthroughWalkthroughAdds an Changesallow_hyphen_values support
Estimated code review effort: 3 (Moderate) | ~20 minutes Sequence Diagram(s)sequenceDiagram
participant User
participant Parser as parse_partial_with_env
participant Spec as SpecFlag
User->>Parser: pass token starting with "-"
Parser->>Parser: check flag_awaiting_value queue
Parser->>Spec: check allow_hyphen_values
alt allow_hyphen_values is true
Spec-->>Parser: true
Parser->>Parser: validate choices for token
Parser->>Parser: store value in out.flags (String/MultiString)
else allow_hyphen_values is false
Spec-->>Parser: false
Parser->>Parser: reinterpret token as flag(s)
end
Parser-->>User: return parsed flags
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
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. Comment |
Greptile SummaryThis PR introduces an opt-in
Confidence Score: 5/5Safe to merge; the feature is opt-in and four regression tests cover all advertised scenarios without touching the default parsing path. The core parsing change is a clean early-exit block that only fires when both the leading lib/src/spec/flag.rs — the allow_hyphen_values() / double_dash == Automatic coupling is worth revisiting if SpecDoubleDashChoices grows additional variants or flag-arg double_dash semantics are used elsewhere. Important Files Changed
Reviews (3): Last reviewed commit: "fix(spec): preserve flag struct compatib..." | Re-trigger Greptile |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
lib/src/parse.rs (1)
564-597: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winConsumption block duplicates the existing
flag_awaiting_valuedrain logic.Lines 569-594 are nearly identical to the existing drain at Lines 670-695 (choice validation,
varvs scalar insert,w = ""). Only the guard differs. Consider extracting a shared helper (e.g.drain_pending_flag_values(&mut out, spec, w, custom_env)?) to keep the two paths from diverging.Note the guard only inspects
flag_awaiting_value.last(), yet thewhileloop drains every pending entry, assigningwto the last-pushed flag and""to any older ones. This matches the existing block's semantics, so behavior is consistent, but it's worth confirming that's intended when multiple flags are pending (e.g. grouped shorts that each take a value).🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@lib/src/parse.rs` around lines 564 - 597, The `parse` function in `lib/src/parse.rs` has duplicated `flag_awaiting_value` drain logic in the consumption block, which should be consolidated to avoid drift between paths. Extract the shared choice-validation and value-assignment sequence into a helper (for example, a private `drain_pending_flag_values` used by both branches) and keep the existing `var` versus scalar insertion behavior and `w = ""` handling identical. Also review the `last()` guard versus draining the full stack in that block to ensure the intended ordering remains correct for multiple pending flags.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@lib/src/parse.rs`:
- Around line 564-597: The `parse` function in `lib/src/parse.rs` has duplicated
`flag_awaiting_value` drain logic in the consumption block, which should be
consolidated to avoid drift between paths. Extract the shared choice-validation
and value-assignment sequence into a helper (for example, a private
`drain_pending_flag_values` used by both branches) and keep the existing `var`
versus scalar insertion behavior and `w = ""` handling identical. Also review
the `last()` guard versus draining the full stack in that block to ensure the
intended ordering remains correct for multiple pending flags.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Central YAML (base), Organization UI (inherited)
Review profile: CHILL
Plan: Pro Plus
Run ID: 172b4b65-b7da-4609-a0fd-e752e717e077
📒 Files selected for processing (3)
lib/src/parse.rslib/src/spec/builder.rslib/src/spec/flag.rs
Summary
allow_hyphen_valuesto flag specs and builders-a -destroy,--args=-destroy, and the unchanged default behaviorCloses #713.
Testing
cargo test -p usage-lib hyphen_valuescargo test -p usage-lib --all-featurescargo clippy --all --all-features -- -D warningscargo test --all --all-featureswas also run; the Rust suites passed until existing Node-backed CLI example tests failed becausenodeis not installed on PATH in this shell.Note
Medium Risk
Touches core CLI parsing order for all specs, but behavior changes only when
allow_hyphen_valuesis enabled; regression tests cover the main cases.Overview
Opt-in flag values that look like flags — specs can set
allow_hyphen_values=#trueon value-taking flags (KDL, builders, and clap-derived specs). The parser treats the next-…token as that flag’s value before short/long flag parsing, including embedded forms like--args=-destroyand repeated variadic values.Pending value handling is centralized in
drain_pending_flag_values.complete-word’s--cwordis regenerated with this setting so negative indices parse correctly. Flags without the option keep today’s behavior (e.g.-a -destroystill splits-destroyinto other short flags).Reviewed by Cursor Bugbot for commit 89d5db0. Bugbot is set up for automated code reviews on this repo. Configure here.
Summary by CodeRabbit
-valueand--args=-value.