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(spec): a clap-derived subcommand should know where it sits · jdx/usage@b0f7233

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • lib/src/spec
    • mod.rs
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎lib/src/spec/mod.rs‎

Copy file name to clipboardExpand all lines: lib/src/spec/mod.rs
+34-2Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,7 @@ impl FromStr for Spec {
569569
#[cfg(feature = "clap")]
570570
impl From<&clap::Command> for Spec {
571571
fn from(cmd: &clap::Command) -> Self {
572-
Spec {
572+
let mut spec = Spec {
573573
name: cmd.get_name().to_string(),
574574
bin: cmd.get_bin_name().unwrap_or(cmd.get_name()).to_string(),
575575
cmd: cmd.into(),
@@ -581,7 +581,13 @@ impl From<&clap::Command> for Spec {
581581
// this at the top level, which is the field a reader puts it back into.
582582
unknown_flags: crate::spec::cmd::SpecCommand::from(cmd).unknown_flags,
583583
..Default::default()
584-
}
584+
};
585+
// The same pass the KDL parser makes, and for the same reason: a command has to know
586+
// where it sits. Without it every subcommand of a clap-derived spec had `full_cmd`
587+
// empty — and `SpecCommand::usage()` joins `full_cmd`, so their usage lines came out
588+
// blank. Now they say what a user would type.
589+
set_subcommand_ancestors(&mut spec.cmd, &[]);
590+
spec
585591
}
586592
}
587593

@@ -688,6 +694,32 @@ source_code_link_template "https://github.com/jdx/mise/blob/main/src/cli/{{path}
688694
"#);
689695
}
690696

697+
#[test]
698+
#[cfg(feature = "clap")]
699+
fn a_clap_subcommand_knows_where_it_sits() {
700+
// The KDL parser makes this pass; the clap conversion did not, so every subcommand of
701+
// a clap-derived spec had an empty `full_cmd`. Two things read it: `usage()`, which
702+
// joins it and so produced a usage line with no command in it, and help rendering,
703+
// which uses it to tell a subcommand's page from the program's.
704+
let cmd = clap::Command::new("ex").subcommand(
705+
clap::Command::new("go")
706+
.about("Go somewhere")
707+
.subcommand(clap::Command::new("fast").about("Quickly")),
708+
);
709+
let spec = Spec::from(&cmd);
710+
711+
let go = spec.cmd.subcommands.get("go").expect("go");
712+
assert_eq!(go.full_cmd, ["go"]);
713+
// `usage()` names the command and then what it takes — `go` has a subcommand, so it
714+
// says so. The point is that the command's own name is in there at all.
715+
assert_eq!(go.usage, "go <SUBCOMMAND>");
716+
717+
// And all the way down, which is what makes it a walk rather than one level.
718+
let fast = go.subcommands.get("fast").expect("fast");
719+
assert_eq!(fast.full_cmd, ["go", "fast"]);
720+
assert_eq!(fast.usage, "go fast");
721+
}
722+
691723
#[test]
692724
#[cfg(feature = "clap")]
693725
fn a_delimited_default_becomes_the_values_clap_would_split_it_into() {

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.