@@ -569,7 +569,7 @@ impl FromStr for Spec { |
569 | 569 | #[cfg(feature = "clap")] |
570 | 570 | impl From<&clap::Command> for Spec { |
571 | 571 | fn from(cmd: &clap::Command) -> Self { |
572 | | - Spec { |
| 572 | + let mut spec = Spec { |
573 | 573 | name: cmd.get_name().to_string(), |
574 | 574 | bin: cmd.get_bin_name().unwrap_or(cmd.get_name()).to_string(), |
575 | 575 | cmd: cmd.into(), |
@@ -581,7 +581,13 @@ impl From<&clap::Command> for Spec { |
581 | 581 | // this at the top level, which is the field a reader puts it back into. |
582 | 582 | unknown_flags: crate::spec::cmd::SpecCommand::from(cmd).unknown_flags, |
583 | 583 | ..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 |
585 | 591 | } |
586 | 592 | } |
587 | 593 |
|
@@ -688,6 +694,32 @@ source_code_link_template "https://github.com/jdx/mise/blob/main/src/cli/{{path} |
688 | 694 | "#); |
689 | 695 | } |
690 | 696 |
|
| 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 | + |
691 | 723 | #[test] |
692 | 724 | #[cfg(feature = "clap")] |
693 | 725 | fn a_delimited_default_becomes_the_values_clap_would_split_it_into() { |
|