diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:19:13 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:19:13 +0000 |
commit | 218caa410aa38c29984be31a5229b9fa717560ee (patch) | |
tree | c54bd55eeb6e4c508940a30e94c0032fbd45d677 /vendor/clap_complete/src/generator | |
parent | Releasing progress-linux version 1.67.1+dfsg1-1~progress7.99u1. (diff) | |
download | rustc-218caa410aa38c29984be31a5229b9fa717560ee.tar.xz rustc-218caa410aa38c29984be31a5229b9fa717560ee.zip |
Merging upstream version 1.68.2+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/clap_complete/src/generator')
-rw-r--r-- | vendor/clap_complete/src/generator/mod.rs | 12 | ||||
-rw-r--r-- | vendor/clap_complete/src/generator/utils.rs | 49 |
2 files changed, 35 insertions, 26 deletions
diff --git a/vendor/clap_complete/src/generator/mod.rs b/vendor/clap_complete/src/generator/mod.rs index 2d00c281d..c025697ed 100644 --- a/vendor/clap_complete/src/generator/mod.rs +++ b/vendor/clap_complete/src/generator/mod.rs @@ -83,10 +83,8 @@ pub trait Generator { /// /// ``` /// // src/cli.rs -/// -/// use clap::{Command, Arg}; -/// -/// pub fn build_cli() -> Command<'static> { +/// # use clap::{Command, Arg, ArgAction}; +/// pub fn build_cli() -> Command { /// Command::new("compl") /// .about("Tests completions") /// .arg(Arg::new("file") @@ -95,7 +93,7 @@ pub trait Generator { /// .about("tests things") /// .arg(Arg::new("case") /// .long("case") -/// .takes_value(true) +/// .action(ArgAction::Set) /// .help("the case to test"))) /// } /// ``` @@ -195,7 +193,7 @@ where /// /// # Examples /// -/// Assuming a separate `cli.rs` like the [example above](generate_to()), +/// Assuming a separate `cli.rs` like the [`generate_to` example](generate_to()), /// we can let users generate a completion script using a command: /// /// ```ignore @@ -236,7 +234,7 @@ where G: Generator, S: Into<String>, { - cmd._build_all(); + cmd.build(); gen.generate(cmd, buf) } diff --git a/vendor/clap_complete/src/generator/utils.rs b/vendor/clap_complete/src/generator/utils.rs index b8aaa4bd5..ca76d189b 100644 --- a/vendor/clap_complete/src/generator/utils.rs +++ b/vendor/clap_complete/src/generator/utils.rs @@ -19,10 +19,7 @@ pub fn all_subcommands(cmd: &Command) -> Vec<(String, String)> { /// Finds the subcommand [`clap::Command`] from the given [`clap::Command`] with the given path. /// /// **NOTE:** `path` should not contain the root `bin_name`. -pub fn find_subcommand_with_path<'help, 'cmd>( - p: &'cmd Command<'help>, - path: Vec<&str>, -) -> &'cmd Command<'help> { +pub fn find_subcommand_with_path<'cmd>(p: &'cmd Command, path: Vec<&str>) -> &'cmd Command { let mut cmd = p; for sc in path { @@ -42,10 +39,6 @@ pub fn subcommands(p: &Command) -> Vec<(String, String)> { let mut subcmds = vec![]; - if !p.has_subcommands() { - return subcmds; - } - for sc in p.get_subcommands() { let sc_bin_name = sc.get_bin_name().unwrap(); @@ -62,7 +55,7 @@ pub fn subcommands(p: &Command) -> Vec<(String, String)> { } /// Gets all the short options, their visible aliases and flags of a [`clap::Command`]. -/// Includes `h` and `V` depending on the [`clap::AppSettings`]. +/// Includes `h` and `V` depending on the [`clap::Command`] settings. pub fn shorts_and_visible_aliases(p: &Command) -> Vec<char> { debug!("shorts: name={}", p.get_name()); @@ -87,7 +80,7 @@ pub fn shorts_and_visible_aliases(p: &Command) -> Vec<char> { } /// Gets all the long options, their visible aliases and flags of a [`clap::Command`]. -/// Includes `help` and `version` depending on the [`clap::AppSettings`]. +/// Includes `help` and `version` depending on the [`clap::Command`] settings. pub fn longs_and_visible_aliases(p: &Command) -> Vec<String> { debug!("longs: name={}", p.get_name()); @@ -117,22 +110,33 @@ pub fn longs_and_visible_aliases(p: &Command) -> Vec<String> { } /// Gets all the flags of a [`clap::Command`](Command). -/// Includes `help` and `version` depending on the [`clap::AppSettings`]. -pub fn flags<'help>(p: &Command<'help>) -> Vec<Arg<'help>> { +/// Includes `help` and `version` depending on the [`clap::Command`] settings. +pub fn flags(p: &Command) -> Vec<Arg> { debug!("flags: name={}", p.get_name()); p.get_arguments() - .filter(|a| !a.is_takes_value_set() && !a.is_positional()) + .filter(|a| !a.get_num_args().expect("built").takes_values() && !a.is_positional()) .cloned() .collect() } +/// Get the possible values for completion +pub fn possible_values(a: &Arg) -> Option<Vec<clap::builder::PossibleValue>> { + if !a.get_num_args().expect("built").takes_values() { + None + } else { + a.get_value_parser() + .possible_values() + .map(|pvs| pvs.collect()) + } +} + #[cfg(test)] mod tests { use super::*; use clap::Arg; - use pretty_assertions::assert_eq; + use clap::ArgAction; - fn common_app() -> Command<'static> { + fn common_app() -> Command { Command::new("myapp") .subcommand( Command::new("test").subcommand(Command::new("config")).arg( @@ -141,6 +145,7 @@ mod tests { .short_alias('c') .visible_short_alias('p') .long("file") + .action(ArgAction::SetTrue) .visible_alias("path"), ), ) @@ -148,17 +153,17 @@ mod tests { .bin_name("my-cmd") } - fn built() -> Command<'static> { + fn built() -> Command { let mut cmd = common_app(); - cmd._build_all(); + cmd.build(); cmd } - fn built_with_version() -> Command<'static> { + fn built_with_version() -> Command { let mut cmd = common_app().version("3.0"); - cmd._build_all(); + cmd.build(); cmd } @@ -188,6 +193,12 @@ mod tests { ("help".to_string(), "my-cmd help".to_string()), ("config".to_string(), "my-cmd test config".to_string()), ("help".to_string(), "my-cmd test help".to_string()), + ("config".to_string(), "my-cmd test help config".to_string()), + ("help".to_string(), "my-cmd test help help".to_string()), + ("test".to_string(), "my-cmd help test".to_string()), + ("hello".to_string(), "my-cmd help hello".to_string()), + ("help".to_string(), "my-cmd help help".to_string()), + ("config".to_string(), "my-cmd help test config".to_string()), ] ); } |