GeezerVison is showing github.com — fix(help): a command's page should say what that command does · jdx/usage@0b891c7
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: argv/src/help.rs +35-18Lines changed: 35 additions & 18 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -228,17 +228,22 @@ pub fn short_help(spec: &Spec<'_>, path: &[&str], meta: &CommandMeta<'_>) -> Str | 228 | 228 | let _ = writeln!(out, "{before}\n"); | 229 | 229 | } | 230 | 230 |
| 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 | + } | 240 | 244 | } | 241 | | - if let Some(about) = spec.about { | | 245 | + let about = if root { spec.about } else { meta.about }; | | 246 | + if let Some(about) = about { | 242 | 247 | let _ = writeln!(out, "{about}\n"); | 243 | 248 | } | 244 | 249 | let _ = writeln!(out, "Usage: {}", usage_line(path, meta)); | @@ -465,15 +470,27 @@ pub fn long_help(spec: &Spec<'_>, path: &[&str], meta: &CommandMeta<'_>) -> Stri | 465 | 470 | let _ = writeln!(out, "{before}\n"); | 466 | 471 | } | 467 | 472 |
| 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 | + } | 475 | 487 | } | 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 { | 477 | 494 | let _ = writeln!(out, "{about}\n"); | 478 | 495 | } | 479 | 496 | let _ = writeln!(out, "Usage: {}", usage_line(path, meta)); | |
Collapse file Copy file name to clipboardExpand all lines: conformance/tests/help_request.rs +78Lines changed: 78 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -313,3 +313,81 @@ fn the_page_advertises_exactly_where_the_word_works() { | 313 | 313 | "a leaf promises nothing: {leaf_page}" | 314 | 314 | ); | 315 | 315 | } | | 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 Copy file name to clipboardExpand all lines: lib/src/docs/cli/mod.rs +4Lines changed: 4 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -11,6 +11,10 @@ pub fn render_help(spec: &Spec, cmd: &SpecCommand, long: bool) -> String { | 11 | 11 | ctx.insert("spec", &docs_spec); | 12 | 12 | ctx.insert("cmd", &docs_cmd); | 13 | 13 | 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()); | 14 | 18 | let template = if long { | 15 | 19 | "spec_template_long.tera" | 16 | 20 | } else { | |
Collapse file 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 number | Diff line number | Diff line change |
|---|
| 7 | 7 | {% elif spec.before_help %}{{ spec.before_help }} | 8 | 8 |
| 9 | 9 | {% endif -%} | | 10 | +{%- if root %} | 10 | 11 | {%- if spec.name and spec.version %}{{ spec.name }} {{ spec.version }} | 11 | 12 | {% elif spec.version %}{{ spec.bin }} {{ spec.version }} | 12 | 13 | {% endif -%} | | 15 | 16 | {% elif spec.about %}{{ spec.about }} | 16 | 17 |
| 17 | 18 | {% endif -%} | | 19 | +{%- else -%} | | 20 | +{%- if cmd.help_long %}{{ cmd.help_long }} | | 21 | + | | 22 | +{% elif cmd.help %}{{ cmd.help }} | | 23 | + | | 24 | +{% endif -%} | | 25 | +{%- endif -%} | 18 | 26 | Usage: {{ (spec.bin ~ " " ~ cmd.usage) | trim }} | 19 | 27 |
| 20 | 28 | {%- if cmd.subcommands %} | |
Collapse file 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 number | Diff line number | Diff line change |
|---|
| 3 | 3 | {% elif spec.before_help %}{{ spec.before_help }} | 4 | 4 |
| 5 | 5 | {% endif -%} | | 6 | +{%- if root %} | 6 | 7 | {%- if spec.name and spec.version %}{{ spec.name }} {{ spec.version }} | 7 | 8 | {% elif spec.version %}{{ spec.bin }} {{ spec.version }} | 8 | 9 | {% endif -%} | 9 | 10 | {%- if spec.about %}{{ spec.about }} | 10 | 11 |
| 11 | 12 | {% endif -%} | | 13 | +{%- else -%} | | 14 | +{%- if cmd.help %}{{ cmd.help }} | | 15 | + | | 16 | +{% endif -%} | | 17 | +{%- endif -%} | 12 | 18 | Usage: {{ (spec.bin ~ " " ~ cmd.usage) | trim }} | 13 | 19 |
| 14 | 20 | {%- if cmd.subcommands %} | |
|