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(help): a command's page should say what that command does · jdx/usage@0b891c7

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • argv/src
    • help.rs
  • conformance/tests
    • help_request.rs
  • lib/src/docs/cli
    • mod.rs
    • templates
      • spec_template_long.tera
      • spec_template_short.tera
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎argv/src/help.rs‎

Copy file name to clipboardExpand all lines: argv/src/help.rs
+35-18Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -228,17 +228,22 @@ pub fn short_help(spec: &Spec<'_>, path: &[&str], meta: &CommandMeta<'_>) -> Str
228228
let _ = writeln!(out, "{before}\n");
229229
}
230230

231-
// The program, then what it is for. usage-lib prints the name when the spec gives one and
232-
// the binary otherwise, and only when there is a version to put beside it.
233-
if let Some(version) = spec.version {
234-
let name = if spec.name.is_empty() {
235-
spec.bin.unwrap_or_default()
236-
} else {
237-
spec.name
238-
};
239-
let _ = writeln!(out, "{name} {version}");
231+
// The program, then what it is for — on the program's own page. A subcommand's page says
232+
// what the subcommand does; see the long form for why. usage-lib prints the name when the
233+
// spec gives one and the binary otherwise, and only when there is a version beside it.
234+
let root = path.len() <= 1;
235+
if root {
236+
if let Some(version) = spec.version {
237+
let name = if spec.name.is_empty() {
238+
spec.bin.unwrap_or_default()
239+
} else {
240+
spec.name
241+
};
242+
let _ = writeln!(out, "{name} {version}");
243+
}
240244
}
241-
if let Some(about) = spec.about {
245+
let about = if root { spec.about } else { meta.about };
246+
if let Some(about) = about {
242247
let _ = writeln!(out, "{about}\n");
243248
}
244249
let _ = writeln!(out, "Usage: {}", usage_line(path, meta));
@@ -465,15 +470,27 @@ pub fn long_help(spec: &Spec<'_>, path: &[&str], meta: &CommandMeta<'_>) -> Stri
465470
let _ = writeln!(out, "{before}\n");
466471
}
467472

468-
if let Some(version) = spec.version {
469-
let name = if spec.name.is_empty() {
470-
spec.bin.unwrap_or_default()
471-
} else {
472-
spec.name
473-
};
474-
let _ = writeln!(out, "{name} {version}");
473+
// The banner and the program's own description belong to the program's page. A
474+
// subcommand's page describes the subcommand: `communique generate --help` said
475+
// "Editorialized release notes powered by AI" and never once said what `generate` does,
476+
// which is the question that was asked. clap prints the command's own description here.
477+
let root = path.len() <= 1;
478+
if root {
479+
if let Some(version) = spec.version {
480+
let name = if spec.name.is_empty() {
481+
spec.bin.unwrap_or_default()
482+
} else {
483+
spec.name
484+
};
485+
let _ = writeln!(out, "{name} {version}");
486+
}
475487
}
476-
if let Some(about) = spec.long_about.or(spec.about) {
488+
let about = if root {
489+
spec.long_about.or(spec.about)
490+
} else {
491+
meta.long_about.or(meta.about)
492+
};
493+
if let Some(about) = about {
477494
let _ = writeln!(out, "{about}\n");
478495
}
479496
let _ = writeln!(out, "Usage: {}", usage_line(path, meta));
Collapse file

‎conformance/tests/help_request.rs‎

Copy file name to clipboardExpand all lines: conformance/tests/help_request.rs
+78Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,3 +313,81 @@ fn the_page_advertises_exactly_where_the_word_works() {
313313
"a leaf promises nothing: {leaf_page}"
314314
);
315315
}
316+
317+
/// A command whose page should be about *it*
318+
#[derive(usage_derive::Args)]
319+
struct Deploy {
320+
/// Which environment
321+
#[usage(long)]
322+
env: Option<String>,
323+
}
324+
325+
#[derive(usage_derive::Subcommands)]
326+
enum Which {
327+
/// Ship the current build somewhere
328+
Deploy(Box<Deploy>),
329+
}
330+
331+
/// A tool that does several things
332+
#[derive(usage_derive::Cli)]
333+
#[usage(name = "tool", bin = "tool", version = "2.0")]
334+
struct Described {
335+
#[usage(subcommand)]
336+
command: Option<Which>,
337+
}
338+
339+
#[test]
340+
fn a_commands_page_says_what_that_command_does() {
341+
// The question `tool deploy --help` asks is "what does deploy do", and the answer used to
342+
// be the program's own description — every page carried the root's banner and about, so a
343+
// subcommand page never once said what the subcommand was for. clap prints the command's
344+
// own description here.
345+
let root = Described::spec().root.cmd;
346+
let deploy = root
347+
.subcommands
348+
.iter()
349+
.find(|c| c.name == "deploy")
350+
.expect("deploy");
351+
352+
for long in [false, true] {
353+
let page = usage_argv::help::render(Described::spec(), deploy, long).expect("a page");
354+
assert!(
355+
page.starts_with("Ship the current build somewhere\n"),
356+
"long={long}: {page}"
357+
);
358+
// And not the program's banner, which belongs to the program's page.
359+
assert!(!page.contains("tool 2.0"), "long={long}: {page}");
360+
assert!(
361+
!page.contains("A tool that does several things"),
362+
"long={long}: {page}"
363+
);
364+
}
365+
}
366+
367+
#[test]
368+
fn the_programs_own_page_still_introduces_the_program() {
369+
// The other half: the root has no command of its own to describe, so it keeps the banner
370+
// and the program's description.
371+
for long in [false, true] {
372+
let page = usage_argv::help::render(Described::spec(), Described::spec().root.cmd, long)
373+
.expect("a page");
374+
// `name`, not `bin` — usage-lib's rule, which is why the fixture declares one: a
375+
// struct called `Described` would otherwise banner itself as `described`.
376+
assert!(page.starts_with("tool 2.0\n"), "long={long}: {page}");
377+
assert!(
378+
page.contains("A tool that does several things"),
379+
"long={long}: {page}"
380+
);
381+
}
382+
}
383+
384+
#[test]
385+
fn the_described_fields_are_bound() {
386+
use std::ffi::OsStr;
387+
let argv = ["deploy", "--env", "prod"].map(OsStr::new);
388+
let parsed = Described::parse_from(&argv).expect("should parse");
389+
let Some(Which::Deploy(d)) = parsed.command else {
390+
panic!("expected deploy")
391+
};
392+
assert_eq!(d.env.as_deref(), Some("prod"));
393+
}
Collapse file

‎lib/src/docs/cli/mod.rs‎

Copy file name to clipboardExpand all lines: lib/src/docs/cli/mod.rs
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ pub fn render_help(spec: &Spec, cmd: &SpecCommand, long: bool) -> String {
1111
ctx.insert("spec", &docs_spec);
1212
ctx.insert("cmd", &docs_cmd);
1313
ctx.insert("long", &long);
14+
// Which page this is. The banner and the program's own description belong to the
15+
// program's page; a subcommand's page describes the subcommand, which is the question
16+
// that was asked. `full_cmd` is the path a user would type, so the root's is empty.
17+
ctx.insert("root", &docs_cmd.full_cmd.is_empty());
1418
let template = if long {
1519
"spec_template_long.tera"
1620
} else {
Collapse file

‎lib/src/docs/cli/templates/spec_template_long.tera‎

Copy file name to clipboardExpand all lines: lib/src/docs/cli/templates/spec_template_long.tera
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
{% elif spec.before_help %}{{ spec.before_help }}
88

99
{% endif -%}
10+
{%- if root %}
1011
{%- if spec.name and spec.version %}{{ spec.name }} {{ spec.version }}
1112
{% elif spec.version %}{{ spec.bin }} {{ spec.version }}
1213
{% endif -%}
@@ -15,6 +16,13 @@
1516
{% elif spec.about %}{{ spec.about }}
1617

1718
{% endif -%}
19+
{%- else -%}
20+
{%- if cmd.help_long %}{{ cmd.help_long }}
21+
22+
{% elif cmd.help %}{{ cmd.help }}
23+
24+
{% endif -%}
25+
{%- endif -%}
1826
Usage: {{ (spec.bin ~ " " ~ cmd.usage) | trim }}
1927

2028
{%- if cmd.subcommands %}
Collapse file

‎lib/src/docs/cli/templates/spec_template_short.tera‎

Copy file name to clipboardExpand all lines: lib/src/docs/cli/templates/spec_template_short.tera
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,18 @@
33
{% elif spec.before_help %}{{ spec.before_help }}
44

55
{% endif -%}
6+
{%- if root %}
67
{%- if spec.name and spec.version %}{{ spec.name }} {{ spec.version }}
78
{% elif spec.version %}{{ spec.bin }} {{ spec.version }}
89
{% endif -%}
910
{%- if spec.about %}{{ spec.about }}
1011

1112
{% endif -%}
13+
{%- else -%}
14+
{%- if cmd.help %}{{ cmd.help }}
15+
16+
{% endif -%}
17+
{%- endif -%}
1218
Usage: {{ (spec.bin ~ " " ~ cmd.usage) | trim }}
1319

1420
{%- if cmd.subcommands %}

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.