refactor(cli): declarative flag constraints via clap (ergonomics 1/3)#35
Merged
Conversation
CLI ergonomics, option 2 (non-breaking). Replace hand-written mutual-exclusion and requires validation with clap's `operation` arg-group (scan/insert/remove/ list-sections now mutually exclusive) and `requires` attributes (--check needs --scan, --desc needs --insert, --no-backup needs --atomic, --recover-forward needs --recover, --to-end needs --line, --require-match needs --remove). Removes ~7 manual checks from main.rs; clap enforces and reports them now. Error wording for these constraints now comes from clap.
There was a problem hiding this comment.
Pull request overview
This PR refactors togl-cli argument validation by moving several cross-flag constraints from imperative checks in main.rs into clap’s declarative requires/group configuration, and updates integration tests to match clap’s standardized error wording.
Changes:
- Updated several integration tests to assert clap-style “required arguments were not provided” messages for missing dependent flags.
- Removed multiple manual CLI validation checks in
main.rsand replaced them with comments indicating clap now enforces those constraints. - Added
requires = ...andgroup = "operation"annotations incli.rsto express dependencies and mode selection declaratively.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
crates/togl-cli/tests/integration.rs |
Updates failing assertions to match clap’s new validation error text for required-argument constraints. |
crates/togl-cli/src/main.rs |
Removes several manual flag-combination checks, relying on clap for enforcement. |
crates/togl-cli/src/cli.rs |
Adds clap requires constraints and assigns mode flags to an operation group. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
49
to
52
| /// List all section IDs found in files (discovery mode, no toggling) | ||
| #[arg(long = "list-sections")] | ||
| #[arg(long = "list-sections", group = "operation")] | ||
| pub list_sections: bool, | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Option 2 of the CLI ergonomics refactor (non-breaking). Replaces the growing wall of hand-written
if cli.X && cli.Y { error }validation with clap's declarative constraints.operationarg-group —--scan/--insert/--remove/--list-sectionsare now mutually exclusive by construction (clap rejects two modes).requires—--check→--scan,--desc→--insert,--no-backup→--atomic,--recover-forward→--recover,--to-end→--line,--require-match→--remove.main.rs; clap enforces them now.--pairworks with or without-Sdepending on mode; valued-default flags).User-visible: validation error wording for these constraints now comes from clap ("the following required arguments were not provided") — 4 test assertions updated to match. Same combinations are still rejected.
Suite: 143 integration + 104 lib, clippy + fmt clean. Next: Option 1 (subcommands), then Option 3 (stdin/stdout).