GeezerVison is showing github.com — feat(help): list the flags a command inherits (#913) · jdx/usage@fa7c0a6
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/complete.rs +4-3Lines changed: 4 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -198,7 +198,8 @@ pub fn for_name<'a>( | 198 | 198 | // Tree order last, and only as a fallback: two sibling commands may take a `tool` and mean | 199 | 199 | // different things by it, and the one the line reached is the one being asked about. | 200 | 200 | let reached = walk(spec.root.cmd, ctx.command_words_start()); | 201 | | - let reached_meta = crate::help::find(spec, reached.cmd).map(|(_, meta)| meta); | | 201 | + let reached_meta = | | 202 | + crate::help::find(spec, reached.cmd).and_then(|(_, chain)| chain.last().copied()); | 202 | 203 | let owner = if on(spec.root, name).is_some() { | 203 | 204 | spec.root | 204 | 205 | } else if let Some(meta) = reached_meta.filter(|meta| on(meta, name).is_some()) { | @@ -407,7 +408,7 @@ fn files_for(name: &str) -> Option<Files> { | 407 | 408 | /// for a word nothing is known about, and the `run=` completions a spec can declare. | 408 | 409 | pub fn complete<'a>(spec: &'a Spec<'a>, split: &Split) -> Completions<'a> { | 409 | 410 | let position = walk(spec.root.cmd, split.argv()); | 410 | | - let meta = crate::help::find(spec, position.cmd).map(|(_, meta)| meta); | | 411 | + let meta = crate::help::find(spec, position.cmd).and_then(|(_, chain)| chain.last().copied()); | 411 | 412 | let token = split.prefix.as_str(); | 412 | 413 | let candidates = candidates(spec, split); | 413 | 414 |
| @@ -476,7 +477,7 @@ pub fn complete<'a>(spec: &'a Spec<'a>, split: &Split) -> Completions<'a> { | 476 | 477 | /// Just the candidates this CLI knows about, without the question of paths. | 477 | 478 | pub fn candidates<'a>(spec: &'a Spec<'a>, split: &Split) -> Vec<Candidate<'a>> { | 478 | 479 | let position = walk(spec.root.cmd, split.argv()); | 479 | | - let meta = crate::help::find(spec, position.cmd).map(|(_, meta)| meta); | | 480 | + let meta = crate::help::find(spec, position.cmd).and_then(|(_, chain)| chain.last().copied()); | 480 | 481 | let token = split.prefix.as_str(); | 481 | 482 |
| 482 | 483 | let mut out = if position.flags_possible && token == "-" { | |
Collapse file Copy file name to clipboardExpand all lines: argv/src/help.rs +249-43Lines changed: 249 additions & 43 deletions Large diffs are not rendered by default. Collapse file Copy file name to clipboardExpand all lines: benches/gate/tests/help.rs +20-13Lines changed: 20 additions & 13 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -23,15 +23,20 @@ fn mise_spec() -> LibSpec { | 23 | 23 | /// Every command in the tree, as (path, metadata) — the path being what a user types. | 24 | 24 | fn walk<'a>( | 25 | 25 | path: Vec<&'a str>, | | 26 | + chain: Vec<&'a CommandMeta<'a>>, | 26 | 27 | meta: &'a CommandMeta<'a>, | 27 | | - out: &mut Vec<(Vec<&'a str>, &'a CommandMeta<'a>)>, | | 28 | + out: &mut Vec<(Vec<&'a str>, Vec<&'a CommandMeta<'a>>)>, | 28 | 29 | ) { | 29 | | - out.push((path.clone(), meta)); | | 30 | + // The chain and not just the command: a page lists what it inherits, which only the | | 31 | + // ancestors know. | | 32 | + let mut chain = chain; | | 33 | + chain.push(meta); | | 34 | + out.push((path.clone(), chain.clone())); | 30 | 35 | for sub in meta.subcommands { | 31 | 36 | // Aliases live in the parse table beside the canonical name; help names the command. | 32 | 37 | let mut child = path.clone(); | 33 | 38 | child.push(sub.cmd.name); | 34 | | - walk(child, sub, out); | | 39 | + walk(child, chain.clone(), sub, out); | 35 | 40 | } | 36 | 41 | } | 37 | 42 |
| @@ -50,15 +55,17 @@ fn every_usage_line_matches_the_reference() { | 50 | 55 | let root = shadow_mise::Cli::spec().root; | 51 | 56 |
| 52 | 57 | let mut commands = Vec::new(); | 53 | | - walk(vec!["mise"], root, &mut commands); | | 58 | + walk(vec!["mise"], Vec::new(), root, &mut commands); | 54 | 59 | assert!( | 55 | 60 | commands.len() > 200, | 56 | 61 | "the shadow should cover mise's whole tree, found {}", | 57 | 62 | commands.len() | 58 | 63 | ); | 59 | 64 |
| 60 | 65 | let mut differences = Vec::new(); | 61 | | - for (path, meta) in &commands { | | 66 | + for (path, chain) in &commands { | | 67 | + // The usage line is about the command itself; the chain is for what it inherits. | | 68 | + let meta = chain.last().expect("a command"); | 62 | 69 | let ours = usage_line(path, meta); | 63 | 70 | // usage-lib's `usage()` omits the binary and starts at the command path, so the | 64 | 71 | // comparison puts it back — the same string the template writes after `Usage: `. | @@ -129,7 +136,7 @@ fn every_short_help_matches_the_reference() { | 129 | 136 | let root = shadow_mise::Cli::spec(); | 130 | 137 |
| 131 | 138 | let mut commands = Vec::new(); | 132 | | - walk(vec!["mise"], root.root, &mut commands); | | 139 | + walk(vec!["mise"], Vec::new(), root.root, &mut commands); | 133 | 140 |
| 134 | 141 | let mut differences = Vec::new(); | 135 | 142 | for (path, meta) in &commands { | @@ -173,7 +180,7 @@ fn every_long_help_matches_the_reference() { | 173 | 180 | let root = shadow_mise::Cli::spec(); | 174 | 181 |
| 175 | 182 | let mut commands = Vec::new(); | 176 | | - walk(vec!["mise"], root.root, &mut commands); | | 183 | + walk(vec!["mise"], Vec::new(), root.root, &mut commands); | 177 | 184 |
| 178 | 185 | let mut differences = Vec::new(); | 179 | 186 | for (path, meta) in &commands { | @@ -276,12 +283,12 @@ fn the_text_around_a_page_is_rendered_where_the_reference_puts_it() { | 276 | 283 | }; | 277 | 284 |
| 278 | 285 | assert_eq!( | 279 | | - short_help(&SPEC, &["ex", "go"], &GO_META), | | 286 | + short_help(&SPEC, &["ex", "go"], &[&GO_META]), | 280 | 287 | usage::docs::cli::render_help(&spec, go, false), | 281 | 288 | "short form" | 282 | 289 | ); | 283 | 290 | assert_eq!( | 284 | | - long_help(&SPEC, &["ex", "go"], &GO_META), | | 291 | + long_help(&SPEC, &["ex", "go"], &[&GO_META]), | 285 | 292 | usage::docs::cli::render_help(&spec, go, true), | 286 | 293 | "long form" | 287 | 294 | ); | @@ -330,9 +337,9 @@ fn a_spec_can_surround_every_page_at_once() { | 330 | 337 |
| 331 | 338 | for long in [false, true] { | 332 | 339 | let ours = if long { | 333 | | - long_help(&SPEC, &["ex", "go"], &GO_META) | | 340 | + long_help(&SPEC, &["ex", "go"], &[&GO_META]) | 334 | 341 | } else { | 335 | | - short_help(&SPEC, &["ex", "go"], &GO_META) | | 342 | + short_help(&SPEC, &["ex", "go"], &[&GO_META]) | 336 | 343 | }; | 337 | 344 | assert_eq!( | 338 | 345 | ours, | @@ -442,9 +449,9 @@ fn a_specs_examples_reach_a_page_that_has_none() { | 442 | 449 | let cmd = spec.cmd.subcommands.get(name).expect("in the spec"); | 443 | 450 | for long in [false, true] { | 444 | 451 | let ours = if long { | 445 | | - long_help(&SPEC, &["ex", name], meta) | | 452 | + long_help(&SPEC, &["ex", name], &[meta]) | 446 | 453 | } else { | 447 | | - short_help(&SPEC, &["ex", name], meta) | | 454 | + short_help(&SPEC, &["ex", name], &[meta]) | 448 | 455 | }; | 449 | 456 | assert_eq!( | 450 | 457 | ours, | |
|