fix(lib): validate choices for variadic args and flags#520
Conversation
Summary of ChangesHello @jdx, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses a missing validation step for variadic arguments and flags. It ensures that when Highlights
Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable Autofix in the Cursor dashboard.
There was a problem hiding this comment.
Code Review
This pull request introduces crucial choice validation for variadic arguments and flags, enhancing the robustness and correctness of the parsing logic. The addition of comprehensive test cases for both valid and invalid scenarios is commendable and ensures the new functionality works as expected. There is a minor inconsistency in error handling for variadic flags when a help argument is encountered, which should be addressed to maintain consistent user experience.
| if let Some(choices) = &arg.choices { | ||
| if !choices.choices.contains(&w) { | ||
| bail!( | ||
| "Invalid choice for option {}: {w}, expected one of {}", | ||
| flag.name, | ||
| choices.choices.join(", ") | ||
| ); | ||
| } | ||
| } |
There was a problem hiding this comment.
The choice validation for variadic flags is missing the is_help_arg check that is present in the non-variadic flag validation path (lines 495-500). This inconsistency could lead to a user receiving an "Invalid choice" error when attempting to request help for a variadic flag, instead of the expected help message. It's important to handle help arguments gracefully in all validation paths.
| if let Some(choices) = &arg.choices { | |
| if !choices.choices.contains(&w) { | |
| bail!( | |
| "Invalid choice for option {}: {w}, expected one of {}", | |
| flag.name, | |
| choices.choices.join(", ") | |
| ); | |
| } | |
| } | |
| if let Some(choices) = &arg.choices { | |
| if !choices.choices.contains(&w) { | |
| if is_help_arg(spec, &w) { | |
| out.errors | |
| .push(render_help_err(spec, &out.cmd, w.len() > 2)); | |
| return Ok(out); | |
| } | |
| bail!( | |
| "Invalid choice for option {}: {w}, expected one of {}", | |
| flag.name, | |
| choices.choices.join(", ") | |
| ); | |
| } | |
| } |
There was a problem hiding this comment.
Pull request overview
This PR fixes a bug where variadic arguments and flags with choices constraints were not being validated, allowing any value to be accepted. The fix adds choice validation to both variadic code paths in the parser, matching the behavior of non-variadic arguments and flags.
Changes:
- Added choice validation for variadic flags (parse.rs lines 476-484)
- Added choice validation for variadic arguments (parse.rs lines 516-529)
- Added 4 test cases covering valid and invalid choices for both variadic args and flags
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| lib/src/parse.rs | Added choice validation logic for variadic flags and arguments |
| lib/tests/parse.rs | Added test cases for variadic argument and flag choice validation |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if let Some(choices) = &arg.choices { | ||
| if !choices.choices.contains(&w) { | ||
| bail!( | ||
| "Invalid choice for option {}: {w}, expected one of {}", | ||
| flag.name, | ||
| choices.choices.join(", ") | ||
| ); | ||
| } |
There was a problem hiding this comment.
The variadic flag choice validation is missing help argument handling that exists in the non-variadic flag path (lines 495-498). When a user provides an invalid choice for a variadic flag, they should still be able to request help with -h or --help. Add the same help check here before the bail! call to match the pattern used in non-variadic flags and variadic args.
82947f9 to
02510ea
Compare
Variadic arguments and flags with `choices` were not being validated, allowing any value to pass. Added choice validation to both variadic code paths matching the existing non-variadic behavior. Closes jdx/mise#8334 Co-Authored-By: Claude Opus 4.6 <[email protected]>
02510ea to
88fbc18
Compare
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #520 +/- ##
==========================================
- Coverage 77.88% 75.83% -2.05%
==========================================
Files 48 48
Lines 6660 6721 +61
Branches 6660 6721 +61
==========================================
- Hits 5187 5097 -90
- Misses 1109 1153 +44
- Partials 364 471 +107 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
### 🐛 Bug Fixes - **(lib)** validate choices for variadic args and flags by [@jdx](https://github.com/jdx) in [#520](#520) ### 🛡️ Security - require AI disclosure on GitHub comments by [@jdx](https://github.com/jdx) in [#519](#519) ### 📦️ Dependency Updates - update autofix-ci/action action to v1.3.3 by [@renovate[bot]](https://github.com/renovate[bot]) in [#515](#515) - update rust crate clap to v4.5.60 by [@renovate[bot]](https://github.com/renovate[bot]) in [#516](#516) - lock file maintenance by [@renovate[bot]](https://github.com/renovate[bot]) in [#518](#518)
This MR contains the following updates: | Package | Update | Change | |---|---|---| | [usage](https://github.com/jdx/usage) | patch | `2.18.0` → `2.18.1` | MR created with the help of [el-capitano/tools/renovate-bot](https://gitlab.com/el-capitano/tools/renovate-bot). **Proposed changes to behavior should be submitted there as MRs.** --- ### Release Notes <details> <summary>jdx/usage (usage)</summary> ### [`v2.18.1`](https://github.com/jdx/usage/blob/HEAD/CHANGELOG.md#2181---2026-02-24) [Compare Source](jdx/usage@v2.18.0...v2.18.1) ##### 🐛 Bug Fixes - **(lib)** validate choices for variadic args and flags by [@​jdx](https://github.com/jdx) in [#​520](jdx/usage#520) ##### 🛡️ Security - require AI disclosure on GitHub comments by [@​jdx](https://github.com/jdx) in [#​519](jdx/usage#519) ##### 📦️ Dependency Updates - update autofix-ci/action action to v1.3.3 by [@​renovate\[bot\]](https://github.com/renovate\[bot]) in [#​515](jdx/usage#515) - update rust crate clap to v4.5.60 by [@​renovate\[bot\]](https://github.com/renovate\[bot]) in [#​516](jdx/usage#516) - lock file maintenance by [@​renovate\[bot\]](https://github.com/renovate\[bot]) in [#​518](jdx/usage#518) </details> --- ### Configuration 📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined). 🚦 **Automerge**: Enabled. ♻ **Rebasing**: Whenever MR is behind base branch, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this MR and you won't be reminded about this update again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this MR, check this box --- This MR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0My4zNS4xIiwidXBkYXRlZEluVmVyIjoiNDMuMzUuMSIsInRhcmdldEJyYW5jaCI6Im1haW4iLCJsYWJlbHMiOlsiUmVub3ZhdGUgQm90IiwiYXV0b21hdGlvbjpib3QtYXV0aG9yZWQiLCJkZXBlbmRlbmN5LXR5cGU6OnBhdGNoIl19-->
Summary
choiceswere not being validated — any value was acceptedparse.rs, matching the existing non-variadic behaviorCloses jdx/mise#8334
Test plan
cargo test -p usage-lib --test parse— all 32 tests passcargo clippy -p usage-lib --all-features -- -D warnings— clean🤖 Generated with Claude Code
Note
Low Risk
Narrow change to CLI parsing validation: variadic args/flags with
choicesnow error where they previously accepted any value, which may affect callers relying on permissive behavior.Overview
Fixes a parsing gap where variadic positional args and variadic flag values (
var=true) were not being validated against declaredchoices.parse.rsnow enforceschoicesfor both variadic arg capture and variadic flag value capture, mirroring the existing non-variadic behavior (including--help/-hhandling). Adds tests ensuring valid values pass and invalid values produce the expected error messages for both variadic args and flags.Written by Cursor Bugbot for commit 88fbc18. This will update automatically on new commits. Configure here.