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 — feat(argv): a flag whose value may be left off by jdx · Pull Request #969 · jdx/usage

feat(argv): a flag whose value may be left off #969

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Sign up for GitHub

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
jdx merged 2 commits into agent/shadow-identity from agent/optional-flag-value
Aug 17, 2026

Uh oh!

There was an error while loading. Please reload this page.

Merged

feat(argv): a flag whose value may be left off #969

Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit Hold shift + click to select a range
0c06e0a
feat(argv): a flag whose value may be left off
jdx Aug 17, 2026
9560b0e
fix(derive): refuse a value_optional nothing reads, and record it as …
jdx Aug 17, 2026
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view
Prev Previous commit
fix(derive): refuse a value_optional nothing reads, and record it as …
…clap's loss

Two rounds of feedback on #969, and the second round corrected the first fix.

**The guard was testing the wrong thing.** `is_arg` is only true where `arg`
was written, so `#[usage(value_optional)] out: Option<String>` — a positional
by default, with no `long`, `short` or `arg` — still compiled and was dropped.
It now tests `is_flag`, which covers both spellings of a positional. Only a
flag's value has a say in this: a positional's brackets come from its type, so
the page went on saying the opposite of what was declared.

**And clap must not be told.** The first fix emitted `num_args = 0..=1`,
reasoning that clap can express an optional value and a `skipped.note` would
report a limitation it does not have. That was wrong, and Bugbot said so
plainly: `value_optional` is help-only — usage-lib's parser refuses a bare
`--bump` exactly as it refuses a bare `--port` — so declaring it in clap makes
the clap shadow accept a line the usage shadow rejects. The pair exists to be
one grammar told to two frameworks, and a fixture that quietly changes the
grammar is worse than one that reports a gap.

So it is a note after all, and a truthful one: clap cannot print `[BUMP]`
without also accepting `--bump` on its own.

Both mutation-checked.
  • Loading branch information

Uh oh!

There was an error while loading. Please reload this page.

@jdx
jdx committed Aug 17, 2026
commit 9560b0e79836b1b0e47fba7d1e40b89709105bcf

Some comments aren't visible on the classic Files Changed page.

There are no files selected for viewing

57 changes: 57 additions & 0 deletions 57 derive/src/model.rs
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -1444,6 +1444,21 @@ impl Field {
}

let is_flag = !longs.is_empty() || !shorts.is_empty();
// Only a flag's value has a say in this. A positional's brackets come from its type
// already — `Option<T>` renders `[NAME]` and `T` renders `<NAME>` — so the attribute
// would be read by nothing, and a declaration nothing reads is worse than an error: the
// page would keep saying the opposite of what was asked for.
//
// Tested against `is_flag` and not `is_arg`: `is_arg` is only true where `arg` was
// written, and a field with no `long`, `short` or `arg` is a positional as well. That
// gap let `#[usage(value_optional)] out: Option<String>` compile and be dropped.
if value_optional && !is_flag {
return Err(syn::Error::new(
span,
"`value_optional` is for a flag's value; a positional says this with its \
type, where `Option<T>` is already `[NAME]`",
));
}
if is_flag && is_arg {
return Err(syn::Error::new(
span,
Expand Down Expand Up @@ -2625,6 +2640,48 @@ mod tests {
assert!(cli.fields[0].required_collection);
}

#[test]
fn value_optional_is_refused_where_nothing_would_read_it() {
// It says a *flag's* value may be left off. `arg_meta` never emits it, and a valueless
// flag has no value to make optional — so on a positional or a `bool`/`count` flag the
// declaration compiled and was dropped. Worse than an error: a positional's brackets
// come from its type, so the page went on saying the opposite of what was asked.
for (decl, word) in [
(
"#[usage(arg, value_optional)]\n out: Option<String>,",
"type",
),
// No `arg` either: a field with no `long`, `short` or `arg` is a positional too,
// and testing `is_arg` rather than `is_flag` let this one through.
(
"#[usage(value_optional)]\n out: Option<String>,",
"type",
),
(
"#[usage(long, value_optional)]\n out: bool,",
"no value",
),
(
"#[usage(long, count, value_optional)]\n out: u8,",
"no value",
),
] {
let err = rejection(&format!(
"struct Ex {{\n {decl}\n }}"
));
assert!(err.contains(word), "unhelpful message for `{decl}`: {err}");
}
// And where there is a flag value, it still works.
let cli = cli(r#"
struct Ex {
#[usage(long, value_optional)]
bump: Option<String>,
}
"#)
.expect("should compile");
assert!(cli.fields[0].value_optional);
}

#[test]
fn value_name_is_refused_where_there_is_no_value_to_name() {
// It names the placeholder a flag's *value* gets in help. `arg_meta` never emits it, and
Expand Down
37 changes: 37 additions & 0 deletions 37 xtask/src/shadow.rs
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -862,6 +862,16 @@ fn clap_flag_opts(
Some(max) => opts.push(format!("num_args = {least}..={max}")),
None => opts.push(format!("num_args = {least}..")),
}
} else if !arg.required {
// Noted, not emitted. `num_args = 0..=1` was the obvious translation and it is the
// wrong one: `value_optional` is help-only — usage-lib's parser refuses a bare
// `--bump` exactly as it refuses a bare `--port` — so declaring it in clap would
// make the clap shadow accept a line the usage shadow rejects, and the pair exists
// to be one grammar told to two frameworks.
//
// A real limitation, then, rather than a gap in this file: clap cannot print
// `[BUMP]` without also accepting `--bump` on its own.
skipped.note("a flag's value being optional in help only");
}
if flag.required {
opts.push("required = true".into());
Expand Down Expand Up @@ -1664,6 +1674,33 @@ mod tests {
assert!(out.contains("version = \"1.55.0\""), "{out}");
}

#[test]
fn the_clap_dialect_counts_an_optional_value_as_lost() {
// Reported twice on #969, in opposite directions, and the second one was right. Emitting
// `num_args = 0..=1` made clap accept `--bump` with no value where usage-lib rejects it,
// so the two shadows stopped describing one grammar. clap cannot show `[BUMP]` without
// accepting the bare form, which makes this a limitation to record rather than a
// translation to make.
let spec = "name \"ex\"\nbin \"ex\"\nflag --bump {\n arg \"[BUMP]\" required=#false\n}\n";
let (out, skipped) = rendered_as(spec, Dialect::Clap);
assert!(
!out.contains("num_args"),
"the grammar must not change: {out}"
);
assert_eq!(
skipped
.counts
.get("a flag's value being optional in help only"),
Some(&1),
"{:?}",
skipped.counts
);

// The usage dialect still says it, since that is where it means something.
let (out, _) = rendered_as(spec, Dialect::Usage);
assert!(out.contains("value_optional"), "{out}");
}

#[test]
fn an_expressible_property_is_not_counted() {
let (_, skipped) = rendered("name \"ex\"\nbin \"ex\"\ncmd \"go\" {\n alias \"g\"\n}\n");
Expand Down
Loading
Oops, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

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.
You are visitor 01402809 — 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.