GeezerVison is showing github.com — feat(parse): add scalar repeat policy · jdx/usage@2b7a0a3
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: PLAN.md +16-15Lines changed: 16 additions & 15 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -505,8 +505,12 @@ Groups are the opposite case: `Command::get_groups`, `ArgGroup::get_args` and | 505 | 505 | empty command line still means help. | 506 | 506 | - [ ] **Remaining command parsing policy** — | 507 | 507 | `args_conflicts_with_subcommands`, `subcommand_negates_reqs`, | 508 | | - `subcommand_precedence_over_arg`, `allow_missing_positional`, and | 509 | | - `args_override_self`. | | 508 | + `subcommand_precedence_over_arg`, and `allow_missing_positional`. | | 509 | +- [x] **`args_override_self`.** Repeated scalar flags are corrections by default; | | 510 | + the later value wins. `args_override_self=false` opts a command into duplicate | | 511 | + errors. KDL, usage-lib, the typed derive/static metadata, generated Go, and the | | 512 | + clap bridge carry the policy. Repeatable, variadic, and count flags retain their | | 513 | + collecting behavior. | 510 | 514 |
| 511 | 515 | **Help output** | 512 | 516 |
| @@ -854,12 +858,10 @@ checked-in `mise.usage.kdl` for both parsers. | 854 | 858 | tree, ~160µs validating it, and ~24µs actually parsing — so even against clap's | 855 | 859 | parse alone, with the tree already built and paid for, this is 12× faster. | 856 | 860 | - [x] **Differential fuzzing** — proptest over argv on the mise spec, against | 857 | | - usage-lib **and clap**. Two parsers was the wrong design and the first run showed | 858 | | - it: every disagreement had usage-argv stricter than usage-lib, which reads as a | 859 | | - pile of usage-argv bugs until clap is asked the same question and sides with | 860 | | - usage-argv. usage-lib is lax — it accepts a missing subcommand, a missing flag | 861 | | - value, and a repeated non-repeatable flag. So clap is the standard for | 862 | | - _accepting_, being what mise ships, while usage-lib stays the standard for | | 861 | + usage-lib **and clap**. The three-way comparison distinguishes intentional usage | | 862 | + defaults from regressions: unknown flags and scalar repeats are permissive unless a | | 863 | + command opts into strictness, while missing subcommands and flag values remain errors. | | 864 | + Fleet commands can request clap parity explicitly; usage-lib stays the standard for | 863 | 865 | rendering. | 864 | 866 | It found one real usage-argv bug — a lone `-` selecting the root's default | 865 | 867 | subcommand instead of binding as a value — now fixed. | @@ -1396,10 +1398,9 @@ including telling you to delete the label afterwards. | 1396 | 1398 | ("a repeat is a correction… the later occurrence wins") and `long-unknown` ("more likely | 1397 | 1399 | data in transit than a mistake", with `unknown_flags "error"` as the opt-in). Acting on | 1398 | 1400 | the wrong reading got as far as three failing conformance vectors. | 1399 | | - One refusal comes from a different layer: for repeated command-line occurrences, | 1400 | | - `Error::DuplicateFlag` is constructed only in `derive/src/codegen.rs`, never by | 1401 | | - usage-argv's parser, so a derive-generated binary still rejects a repeated flag as clap | 1402 | | - does. Separately, `Spec::to_kdl` validates `duplicate_flag_form` at the spec boundary so | | 1401 | + Repeated command-line occurrences now follow the same permissive principle: a later | | 1402 | + scalar corrects an earlier one, while `args_override_self=false` opts a command into | | 1403 | + strict duplicate errors. Separately, `Spec::to_kdl` validates `duplicate_flag_form` at the spec boundary so | 1403 | 1404 | two declarations cannot claim the same spelling. Unknown flags are intentionally different: usage parsers | 1404 | 1405 | are permissive by default and a command opts into `unknown_flags="error"` when it owns the | 1405 | 1406 | whole grammar. That is what fleet adopters should declare for clap parity, while forwarding | @@ -1412,9 +1413,9 @@ including telling you to delete the label afterwards. | 1412 | 1413 | a position, not a compatibility concession: clap's strict default is held | 1413 | 1414 | to be the wrong one — a wrapper appending to a command line it did not | 1414 | 1415 | write is ordinary, which is the grammar's "data in transit" rationale — | 1415 | | - and what `mise run` tasks accept does not change. The derive's strictness | 1416 | | - about a _repeated_ non-repeatable flag is a separate rule and unchanged. | 1417 | | - The migration guide presents the difference as intentional, with strict | | 1416 | + and what `mise run` tasks accept does not change. Repeated scalar flags follow the same | | 1417 | + default and have their own `args_override_self=false` strict opt-in. | | 1418 | + The migration guide presents both differences as intentional, with strict | 1418 | 1419 | one root-level line away, as communique's rewrite already declares it. | 1419 | 1420 | - [x] A flag missing its value is dropped silently — now an error, in `parse` but | 1420 | 1421 | not `parse_partial`, since a half-typed flag is exactly what a completion is | |
Collapse file Copy file name to clipboardExpand all lines: argv/src/spec.rs +22Lines changed: 22 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -708,6 +708,8 @@ pub struct CommandMeta<'a> { | 708 | 708 | /// everything reading that spec — help, docs, completions — otherwise describes a | 709 | 709 | /// command as runnable when it is not. | 710 | 710 | pub subcommand_required: bool, | | 711 | + /// Whether later single-valued occurrences replace earlier ones. | | 712 | + pub args_override_self: bool, | 711 | 713 | /// Text printed above the usage line, and below everything else. | 712 | 714 | /// | 713 | 715 | /// The spec's `before_help`/`after_help` and their long forms. mise puts an Examples | @@ -743,6 +745,7 @@ impl CommandMeta<'_> { | 743 | 745 | mount: None, | 744 | 746 | restart_token: None, | 745 | 747 | subcommand_required: false, | | 748 | + args_override_self: true, | 746 | 749 | before_help: None, | 747 | 750 | before_long_help: None, | 748 | 751 | after_help: None, | @@ -1128,6 +1131,9 @@ impl Spec<'_> { | 1128 | 1131 | if self.root.cmd.dont_delimit_trailing_values { | 1129 | 1132 | writeln!(out, "dont_delimit_trailing_values #true")?; | 1130 | 1133 | } | | 1134 | + if !self.root.args_override_self { | | 1135 | + writeln!(out, "args_override_self #false")?; | | 1136 | + } | 1131 | 1137 | // A `complete` block for every completer this CLI declares, naming the command that | 1132 | 1138 | // asks the binary itself. Written rather than declared, so there is one place a | 1133 | 1139 | // completer is said to exist: the Rust function. Everything that reads a spec — the | @@ -1374,6 +1380,9 @@ fn write_command<'a>( | 1374 | 1380 | if meta.cmd.dont_delimit_trailing_values { | 1375 | 1381 | out.push_str(" dont_delimit_trailing_values=#true"); | 1376 | 1382 | } | | 1383 | + if !meta.args_override_self { | | 1384 | + out.push_str(" args_override_self=#false"); | | 1385 | + } | 1377 | 1386 | out.push_str(" {\n"); | 1378 | 1387 |
| 1379 | 1388 | let inner = depth + 1; | @@ -2474,6 +2483,19 @@ pub trait CommandArgs: Sized { | 2474 | 2483 | Ok(()) | 2475 | 2484 | } | 2476 | 2485 |
| | 2486 | + /// Run checks under the repeat policy of the command that composed these args. | | 2487 | + /// | | 2488 | + /// A flattened argument group is part of its parent's command, so the parent's | | 2489 | + /// `args_override_self` setting governs repeats in the group as well. Hand-written | | 2490 | + /// implementations keep their existing behavior through this default. | | 2491 | + fn check_with_args_override_self<'t, 'v>( | | 2492 | + partial: &mut Self::Partial, | | 2493 | + args_override_self: bool, | | 2494 | + ) -> Result<(), crate::Error<'t, 'v>> { | | 2495 | + let _ = args_override_self; | | 2496 | + Self::check(partial) | | 2497 | + } | | 2498 | + | 2477 | 2499 | /// Build the struct from what was collected. | 2478 | 2500 | /// | 2479 | 2501 | /// Fallible because a command can require a subcommand of its own, and "none was | |
Collapse file Copy file name to clipboardExpand all lines: benches/gate/tests/differential.rs +22-24Lines changed: 22 additions & 24 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 25 | 25 | //! surfaced. The third was real and is fixed: `subcommand_required` was in the spec and no | 26 | 26 | //! parser read it (#992). | 27 | 27 | //! | 28 | | -//! So where do the refusals come from? `Error::DuplicateFlag` is constructed in exactly one | 29 | | -//! place — `derive/src/codegen.rs` — and never by usage-argv's parser. The layers differ, not | 30 | | -//! the parsers: | | 28 | +//! Repeats are permissive in usage by default, including at the derive layer. A command that | | 29 | +//! owns its entire grammar can opt into clap's rule with `args_override_self=false`: | 31 | 30 | //! | 32 | 31 | //! | | a flag given twice | | 33 | 32 | //! |---|---| | 34 | 33 | //! | usage-argv, the parser | accepts, last wins — conformant | | 35 | 34 | //! | usage-lib | accepts, last wins — conformant | | 36 | | -//! | usage-derive's post-binding check | refuses | | | 35 | +//! | usage-derive, by default | accepts, last wins — conformant | | 37 | 36 | //! | clap | refuses | | 38 | 37 | //! | 39 | | -//! Both are right in their own domain. A derive-generated binary is what an adopter compares to | 40 | | -//! clap, and there the two agree. A spec-driven parse is what mise runs *task* arguments | 41 | | -//! through, and there a wrapper appending `--jobs 2` to a command line it did not write is the | 42 | | -//! documented case. So a disagreement of this shape is a fact about the layers, not a defect, | 43 | | -//! and the arm below says so — do not "fix" usage-lib to close it. | | 38 | +//! The deliberate default keeps wrappers and task runners from rejecting corrections. Fleet | | 39 | +//! commands that need clap parity declare strictness explicitly; the gate's generated shadow | | 40 | +//! leaves the default visible here so tightening it cannot happen accidentally. | 44 | 41 | //! | 45 | 42 | //! # A verdict is not enough | 46 | 43 | //! | 47 | 44 | //! Each class below names the *reason* a parser refused, not only that it did. Three booleans | 48 | | -//! would allow-list every line of a known shape whatever caused it — and one such line is | 49 | | -//! `mise -y config ls -y`, where usage-argv alone refuses a global given at two levels. It wore | 50 | | -//! the same three booleans as the bare-`-` divergence and rode through on them until the reasons | 51 | | -//! were compared. usage-lib's failures are `miette::Error`, a message with no class behind it, | | 45 | +//! would allow-list every line of a known shape whatever caused it. This previously caught a | | 46 | +//! global flag incorrectly rejected across a command boundary; the regression test remains even | | 47 | +//! though permissive repeats have since made that particular line valid more broadly. | | 48 | +//! usage-lib's failures are `miette::Error`, a message with no class behind it, | 52 | 49 | //! so it stays a yes-or-no and the narrowing leans on the two parsers that can say why. | 53 | 50 | //! | 54 | 51 | //! # Mounts are stripped, and must be | @@ -505,11 +502,11 @@ fn a_bare_word_diverges_because_of_the_mount_not_the_parser() { | 505 | 502 | } | 506 | 503 |
| 507 | 504 | #[test] | 508 | | -fn a_global_may_be_given_once_per_level() { | 509 | | - // Found by the generator once `head` let it put tokens before the command path, and fixed | 510 | | - // in the commit this test came with. `-y` is `global=#true`; clap lets it be given again on | 511 | | - // a subcommand — the inner occurrence wins — and refuses a repeat at one level. usage-argv | 512 | | - // now draws the same line, which matters because `mise -y install -y` works today. | | 505 | +fn a_global_repeat_is_permissive_unless_the_command_opts_into_strictness() { | | 506 | + // Found by the generator once `head` let it put tokens before the command path. `-y` is | | 507 | + // `global=#true`; clap lets it be given again on | | 508 | + // a subcommand — the inner occurrence wins — and refuses a repeat at one level. Usage keeps | | 509 | + // the last occurrence at either level unless that command opts into strictness. | 513 | 510 | let spec = spec(); | 514 | 511 | let go = |words: &[&str]| { | 515 | 512 | run( | @@ -527,15 +524,16 @@ fn a_global_may_be_given_once_per_level() { | 527 | 524 | assert_eq!(o.accepted(), (true, true, true), "{words:?} {o:?}"); | 528 | 525 | } | 529 | 526 |
| 530 | | - // Twice at one level: refused, and refused by clap too — whether that level is the root or | 531 | | - // a command three words in. This is the half a blanket exemption for globals would lose. | | 527 | + // Twice at one level: usage's permissive default keeps the later occurrence, while clap's | | 528 | + // strict default refuses it. | 532 | 529 | for words in [ | 533 | 530 | &["-y", "-y"][..], | 534 | 531 | &["config", "ls", "-y", "-y"][..], | 535 | 532 | &["-y", "config", "ls", "-y", "-y"][..], | 536 | 533 | ] { | 537 | 534 | let o = go(words); | 538 | | - assert_eq!(o.argv, Verdict::Conflict, "{words:?} {o:?}"); | | 535 | + assert_eq!(o.argv, Verdict::Accept, "{words:?} {o:?}"); | | 536 | + assert!(o.lib, "{words:?} {o:?}"); | 539 | 537 | assert_eq!(o.clap, Verdict::Conflict, "{words:?} {o:?}"); | 540 | 538 | } | 541 | 539 | } | @@ -682,9 +680,9 @@ fn usage_lib_accepting_a_repeat_is_the_grammar_not_a_defect() { | 682 | 680 | "usage-lib must accept an unknown flag: `long-unknown` specifies it. {unknown:?}" | 683 | 681 | ); | 684 | 682 |
| 685 | | - // And the refusal that does exist comes from the layer above the parser: the derive's | 686 | | - // post-binding check, which is what an adopter compares against clap. | 687 | | - assert_eq!(repeated.argv, Verdict::Conflict, "{repeated:?}"); | | 683 | + // The typed derive follows the grammar's permissive default too. A CLI that owns the grammar | | 684 | + // opts into strictness explicitly; clap starts strict. | | 685 | + assert_eq!(repeated.argv, Verdict::Accept, "{repeated:?}"); | 688 | 686 | assert_eq!(repeated.clap, Verdict::Conflict, "{repeated:?}"); | 689 | 687 | } | 690 | 688 |
| |
Collapse file Copy file name to clipboardExpand all lines: clap_usage/src/report.rs -6Lines changed: 0 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -15,7 +15,6 @@ pub enum FidelityFeature { | 15 | 15 | GranularHide, | 16 | 16 | AllowMissingPositional, | 17 | 17 | ArgsConflictWithSubcommands, | 18 | | - ArgsOverrideSelf, | 19 | 18 | SubcommandPrecedenceOverArg, | 20 | 19 | SubcommandNegatesRequirements, | 21 | 20 | FlattenHelp, | @@ -93,11 +92,6 @@ fn visit(cmd: &Command, ancestors: &[String], losses: &mut BTreeSet<FidelityLoss | 93 | 92 | FidelityFeature::ArgsConflictWithSubcommands, | 94 | 93 | "args_conflicts_with_subcommands", | 95 | 94 | ); | 96 | | - command_loss( | 97 | | - cmd.is_args_override_self(), | 98 | | - FidelityFeature::ArgsOverrideSelf, | 99 | | - "args_override_self", | 100 | | - ); | 101 | 95 | command_loss( | 102 | 96 | cmd.is_subcommand_precedence_over_arg_set(), | 103 | 97 | FidelityFeature::SubcommandPrecedenceOverArg, | |
Collapse file Copy file name to clipboardExpand all lines: clap_usage/tests/snapshots/simple__simple.snap +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -6,6 +6,7 @@ expression: output | 6 | 6 | name example | 7 | 7 | bin example | 8 | 8 | unknown_flags error | | 9 | +args_override_self #false | 9 | 10 | usage "Usage: example [OPTIONS]" | 10 | 11 | flag --file help="some input file" { | 11 | 12 | arg <FILE> | |
Collapse file Copy file name to clipboardExpand all lines: conformance/src/tables.rs +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -145,6 +145,7 @@ pub fn build( | 145 | 145 | mount: cmd.mounts.first().map(|m| leak(&m.run)), | 146 | 146 | restart_token: opt(&cmd.restart_token), | 147 | 147 | subcommand_required: cmd.subcommand_required, | | 148 | + args_override_self: cmd.args_override_self, | 148 | 149 | before_help: opt(&cmd.before_help), | 149 | 150 | before_long_help: opt(&cmd.before_help_long), | 150 | 151 | after_help: opt(&cmd.after_help), | |
Collapse file Copy file name to clipboard +63Lines changed: 63 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +//! Repeating a scalar is a correction by default, with strictness available per command. | | 2 | +
| | 3 | +use std::ffi::OsStr; | | 4 | + | | 5 | +use usage_derive::Cli; | | 6 | + | | 7 | +#[derive(Cli, Debug)] | | 8 | +#[usage(bin = "permissive")] | | 9 | +struct Permissive { | | 10 | + #[usage(long)] | | 11 | + jobs: Option<u32>, | | 12 | + #[usage(long)] | | 13 | + verbose: bool, | | 14 | +} | | 15 | + | | 16 | +#[derive(Cli, Debug)] | | 17 | +#[usage(bin = "strict", args_override_self = false)] | | 18 | +struct Strict { | | 19 | + #[usage(long)] | | 20 | + jobs: Option<u32>, | | 21 | + #[usage(long)] | | 22 | + verbose: bool, | | 23 | +} | | 24 | + | | 25 | +fn argv<'a>(words: &'a [&'a str]) -> Vec<&'a OsStr> { | | 26 | + words.iter().map(OsStr::new).collect() | | 27 | +} | | 28 | + | | 29 | +#[test] | | 30 | +fn a_later_scalar_wins_by_default() { | | 31 | + let parsed = Permissive::parse_from(&argv(&["--jobs", "1", "--jobs", "2"])) | | 32 | + .expect("repeating a scalar is permitted by default"); | | 33 | + assert_eq!(parsed.jobs, Some(2)); | | 34 | + | | 35 | + let parsed = Permissive::parse_from(&argv(&["--verbose", "--verbose"])) | | 36 | + .expect("the same policy applies to switches"); | | 37 | + assert!(parsed.verbose); | | 38 | +} | | 39 | + | | 40 | +#[test] | | 41 | +fn a_command_can_reject_duplicate_scalars() { | | 42 | + let parsed = Strict::parse_from(&argv(&["--jobs", "1", "--verbose"])) | | 43 | + .expect("one occurrence remains valid"); | | 44 | + assert_eq!(parsed.jobs, Some(1)); | | 45 | + assert!(parsed.verbose); | | 46 | + | | 47 | + for words in [ | | 48 | + &["--jobs", "1", "--jobs", "2"][..], | | 49 | + &["--verbose", "--verbose"][..], | | 50 | + ] { | | 51 | + let err = Strict::parse_from(&argv(words)).expect_err("strict commands reject repeats"); | | 52 | + assert!(format!("{err:?}").contains("DuplicateFlag"), "{err:?}"); | | 53 | + } | | 54 | +} | | 55 | + | | 56 | +#[test] | | 57 | +fn only_the_non_default_policy_is_emitted() { | | 58 | + let permissive = Permissive::to_kdl(); | | 59 | + assert!(!permissive.contains("args_override_self"), "{permissive}"); | | 60 | + | | 61 | + let strict = Strict::to_kdl(); | | 62 | + assert!(strict.contains("args_override_self #false"), "{strict}"); | | 63 | +} |
Collapse file Copy file name to clipboardExpand all lines: conformance/tests/global_given_twice.rs +4-1Lines changed: 4 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -17,6 +17,7 @@ use usage_derive::{Args, Cli, Subcommands}; | 17 | 17 |
| 18 | 18 | /// Do the thing | 19 | 19 | #[derive(Args)] | | 20 | +#[usage(args_override_self = false)] | 20 | 21 | struct Sub { | 21 | 22 | /// Its own flag | 22 | 23 | #[usage(long)] | @@ -27,6 +28,7 @@ struct Sub { | 27 | 28 |
| 28 | 29 | /// Deeper still | 29 | 30 | #[derive(Args)] | | 31 | +#[usage(args_override_self = false)] | 30 | 32 | struct Leaf { | 31 | 33 | #[usage(long)] | 32 | 34 | leafy: bool, | @@ -46,6 +48,7 @@ enum Commands { | 46 | 48 |
| 47 | 49 | /// What every level shares, spliced in rather than declared again | 48 | 50 | #[derive(Args)] | | 51 | +#[usage(args_override_self = false)] | 49 | 52 | struct Shared { | 50 | 53 | /// Global, and living in a flattened group — whose partial is its own, so the | 51 | 54 | /// level markers have to be cleared through it rather than around it | @@ -57,7 +60,7 @@ struct Shared { | 57 | 60 | } | 58 | 61 |
| 59 | 62 | #[derive(Cli)] | 60 | | -#[usage(bin = "ex")] | | 63 | +#[usage(bin = "ex", args_override_self = false)] | 61 | 64 | struct Ex { | 62 | 65 | /// Say yes to everything | 63 | 66 | #[usage(long, short = 'y', global)] | |
Collapse file Copy file name to clipboardExpand all lines: conformance/tests/post_binding.rs +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -391,7 +391,7 @@ fn required_unless_is_satisfied_by_the_other_flag() { | 391 | 391 | /// Only `--file` declares the override, which is enough: the relationship holds | 392 | 392 | /// between the two flags, so the later one wins whichever way round they are given. | 393 | 393 | #[derive(Cli)] | 394 | | -#[usage(bin = "ovr")] | | 394 | +#[usage(bin = "ovr", args_override_self = false)] | 395 | 395 | struct Ovr { | 396 | 396 | /// Read from a file | 397 | 397 | #[usage(long, overrides("--stdin", "--url"))] | @@ -544,7 +544,7 @@ fn each_occurrence_of_a_bounded_flag_counts_for_itself() { | 544 | 544 | /// holds something when nobody gives it one. Its spec said so and the derive could not, so it | 545 | 545 | /// was the last thing mise's 211-command spec could express that the derive could not. | 546 | 546 | #[derive(Cli)] | 547 | | -#[usage(bin = "ex")] | | 547 | +#[usage(bin = "ex", args_override_self = false)] | 548 | 548 | struct Defaulted { | 549 | 549 | /// Filesystem events to filter to | 550 | 550 | #[usage(long, var, default = "create", default = "remove", default = "modify")] | |
|