GeezerVison is showing github.com — fix(derive): refuse a value_optional nothing reads, and let clap say … · jdx/usage@17a79ed
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: derive/src/model.rs +47Lines changed: 47 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1394,6 +1394,17 @@ impl Field { | 1394 | 1394 | takes no value", | 1395 | 1395 | )); | 1396 | 1396 | } | | 1397 | + // Only a flag's value has a say in this. A positional's brackets come from its type | | 1398 | + // already — `Option<String>` renders `[NAME]` and `String` renders `<NAME>` — so the | | 1399 | + // attribute would be read by nothing, and a declaration nothing reads is worse than an | | 1400 | + // error: the page would keep saying the opposite of what was asked for. | | 1401 | + if value_optional && is_arg { | | 1402 | + return Err(syn::Error::new( | | 1403 | + span, | | 1404 | + "`value_optional` is for a flag's value; a positional says this with its \ | | 1405 | + type, where `Option<T>` is already `[NAME]`", | | 1406 | + )); | | 1407 | + } | 1397 | 1408 | if !choices.is_empty() && matches!(shape, Shape::Bool | Shape::Count) { | 1398 | 1409 | return Err(syn::Error::new( | 1399 | 1410 | span, | @@ -2625,6 +2636,42 @@ mod tests { | 2625 | 2636 | assert!(cli.fields[0].required_collection); | 2626 | 2637 | } | 2627 | 2638 |
| | 2639 | + #[test] | | 2640 | + fn value_optional_is_refused_where_nothing_would_read_it() { | | 2641 | + // It says a *flag's* value may be left off. `arg_meta` never emits it, and a valueless | | 2642 | + // flag has no value to make optional — so on a positional or a `bool`/`count` flag the | | 2643 | + // declaration compiled and was dropped. Worse than an error: a positional's brackets | | 2644 | + // come from its type, so the page went on saying the opposite of what was asked. | | 2645 | + for (decl, word) in [ | | 2646 | + ( | | 2647 | + "#[usage(arg, value_optional)]\n out: Option<String>,", | | 2648 | + "type", | | 2649 | + ), | | 2650 | + ( | | 2651 | + "#[usage(long, value_optional)]\n out: bool,", | | 2652 | + "no value", | | 2653 | + ), | | 2654 | + ( | | 2655 | + "#[usage(long, count, value_optional)]\n out: u8,", | | 2656 | + "no value", | | 2657 | + ), | | 2658 | + ] { | | 2659 | + let err = rejection(&format!( | | 2660 | + "struct Ex {{\n {decl}\n }}" | | 2661 | + )); | | 2662 | + assert!(err.contains(word), "unhelpful message for `{decl}`: {err}"); | | 2663 | + } | | 2664 | + // And where there is a flag value, it still works. | | 2665 | + let cli = cli(r#" | | 2666 | + struct Ex { | | 2667 | + #[usage(long, value_optional)] | | 2668 | + bump: Option<String>, | | 2669 | + } | | 2670 | + "#) | | 2671 | + .expect("should compile"); | | 2672 | + assert!(cli.fields[0].value_optional); | | 2673 | + } | | 2674 | + | 2628 | 2675 | #[test] | 2629 | 2676 | fn value_name_is_refused_where_there_is_no_value_to_name() { | 2630 | 2677 | // It names the placeholder a flag's *value* gets in help. `arg_meta` never emits it, and | |
Collapse file Copy file name to clipboardExpand all lines: xtask/src/shadow.rs +26Lines changed: 26 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -858,6 +858,11 @@ fn clap_flag_opts( | 858 | 858 | Some(max) => opts.push(format!("num_args = {least}..={max}")), | 859 | 859 | None => opts.push(format!("num_args = {least}..")), | 860 | 860 | } | | 861 | + } else if !arg.required { | | 862 | + // The same word clap uses for the bounds above: an optional value is a count of | | 863 | + // nought-or-one. Emitted rather than noted as a loss, because clap *can* say it — | | 864 | + // a `skipped.note` here would report a limitation clap does not have. | | 865 | + opts.push("num_args = 0..=1".into()); | 861 | 866 | } | 862 | 867 | if flag.required { | 863 | 868 | opts.push("required = true".into()); | @@ -1649,6 +1654,27 @@ mod tests { | 1649 | 1654 | assert!(out.contains("version = \"1.55.0\""), "{out}"); | 1650 | 1655 | } | 1651 | 1656 |
| | 1657 | + #[test] | | 1658 | + fn the_clap_dialect_says_a_value_is_optional_too() { | | 1659 | + // Reported on #969: the usage dialect gained `value_optional` and clap dropped it with | | 1660 | + // no note, so the fidelity report went quiet about it. Emitted rather than noted, | | 1661 | + // because clap *can* say this — `num_args = 0..=1`, the same word already used for the | | 1662 | + // variadic bounds — and a `skipped.note` would report a limitation clap does not have. | | 1663 | + let spec = "name \"ex\"\nbin \"ex\"\nflag --bump {\n arg \"[BUMP]\" required=#false\n}\n"; | | 1664 | + let (out, skipped) = rendered_as(spec, Dialect::Clap); | | 1665 | + assert!(out.contains("num_args = 0..=1"), "{out}"); | | 1666 | + assert!( | | 1667 | + !skipped.counts.keys().any(|k| k.contains("optional")), | | 1668 | + "clap can express it, so nothing should be counted as lost: {:?}", | | 1669 | + skipped.counts | | 1670 | + ); | | 1671 | + | | 1672 | + // And a required value says nothing, rather than `num_args = 1..=1`. | | 1673 | + let spec = "name \"ex\"\nbin \"ex\"\nflag --port {\n arg <PORT>\n}\n"; | | 1674 | + let (out, _) = rendered_as(spec, Dialect::Clap); | | 1675 | + assert!(!out.contains("num_args"), "{out}"); | | 1676 | + } | | 1677 | + | 1652 | 1678 | #[test] | 1653 | 1679 | fn an_expressible_property_is_not_counted() { | 1654 | 1680 | let (_, skipped) = rendered("name \"ex\"\nbin \"ex\"\ncmd \"go\" {\n alias \"g\"\n}\n"); | |
|