GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(derive): refuse a value_optional nothing reads, and let clap say … · jdx/usage@17a79ed

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • derive/src
    • model.rs
  • xtask/src
    • shadow.rs
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎derive/src/model.rs‎

Copy file name to clipboardExpand all lines: derive/src/model.rs
+47Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1394,6 +1394,17 @@ impl Field {
13941394
takes no value",
13951395
));
13961396
}
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+
}
13971408
if !choices.is_empty() && matches!(shape, Shape::Bool | Shape::Count) {
13981409
return Err(syn::Error::new(
13991410
span,
@@ -2625,6 +2636,42 @@ mod tests {
26252636
assert!(cli.fields[0].required_collection);
26262637
}
26272638

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+
26282675
#[test]
26292676
fn value_name_is_refused_where_there_is_no_value_to_name() {
26302677
// It names the placeholder a flag's *value* gets in help. `arg_meta` never emits it, and
Collapse file

‎xtask/src/shadow.rs‎

Copy file name to clipboardExpand all lines: xtask/src/shadow.rs
+26Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -858,6 +858,11 @@ fn clap_flag_opts(
858858
Some(max) => opts.push(format!("num_args = {least}..={max}")),
859859
None => opts.push(format!("num_args = {least}..")),
860860
}
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());
861866
}
862867
if flag.required {
863868
opts.push("required = true".into());
@@ -1649,6 +1654,27 @@ mod tests {
16491654
assert!(out.contains("version = \"1.55.0\""), "{out}");
16501655
}
16511656

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+
16521678
#[test]
16531679
fn an_expressible_property_is_not_counted() {
16541680
let (_, skipped) = rendered("name \"ex\"\nbin \"ex\"\ncmd \"go\" {\n alias \"g\"\n}\n");

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.