mirror of
https://github.com/clap-rs/clap
synced 2024-12-14 06:42:33 +00:00
refactor(complete): Pull out common candidate code
This commit is contained in:
parent
1448791940
commit
59a61e188f
1 changed files with 38 additions and 45 deletions
|
@ -426,13 +426,7 @@ fn longs_and_visible_aliases(p: &clap::Command) -> Vec<CompletionCandidate> {
|
||||||
.filter_map(|a| {
|
.filter_map(|a| {
|
||||||
a.get_long_and_visible_aliases().map(|longs| {
|
a.get_long_and_visible_aliases().map(|longs| {
|
||||||
longs.into_iter().map(|s| {
|
longs.into_iter().map(|s| {
|
||||||
CompletionCandidate::new(format!("--{}", s))
|
populate_arg_candidate(CompletionCandidate::new(format!("--{}", s)), a)
|
||||||
.help(a.get_help().cloned())
|
|
||||||
.id(Some(format!("arg::{}", a.get_id())))
|
|
||||||
.tag(Some(
|
|
||||||
a.get_help_heading().unwrap_or("Options").to_owned().into(),
|
|
||||||
))
|
|
||||||
.hide(a.is_hide_set())
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -448,12 +442,7 @@ fn hidden_longs_aliases(p: &clap::Command) -> Vec<CompletionCandidate> {
|
||||||
.filter_map(|a| {
|
.filter_map(|a| {
|
||||||
a.get_aliases().map(|longs| {
|
a.get_aliases().map(|longs| {
|
||||||
longs.into_iter().map(|s| {
|
longs.into_iter().map(|s| {
|
||||||
CompletionCandidate::new(format!("--{}", s))
|
populate_arg_candidate(CompletionCandidate::new(format!("--{}", s)), a)
|
||||||
.help(a.get_help().cloned())
|
|
||||||
.id(Some(format!("arg::{}", a.get_id())))
|
|
||||||
.tag(Some(
|
|
||||||
a.get_help_heading().unwrap_or("Options").to_owned().into(),
|
|
||||||
))
|
|
||||||
.hide(true)
|
.hide(true)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -471,17 +460,11 @@ fn shorts_and_visible_aliases(p: &clap::Command) -> Vec<CompletionCandidate> {
|
||||||
.filter_map(|a| {
|
.filter_map(|a| {
|
||||||
a.get_short_and_visible_aliases().map(|shorts| {
|
a.get_short_and_visible_aliases().map(|shorts| {
|
||||||
shorts.into_iter().map(|s| {
|
shorts.into_iter().map(|s| {
|
||||||
CompletionCandidate::new(s.to_string())
|
populate_arg_candidate(CompletionCandidate::new(s.to_string()), a).help(
|
||||||
.help(
|
a.get_help()
|
||||||
a.get_help()
|
.cloned()
|
||||||
.cloned()
|
.or_else(|| a.get_long().map(|long| format!("--{long}").into())),
|
||||||
.or_else(|| a.get_long().map(|long| format!("--{long}").into())),
|
)
|
||||||
)
|
|
||||||
.id(Some(format!("arg::{}", a.get_id())))
|
|
||||||
.tag(Some(
|
|
||||||
a.get_help_heading().unwrap_or("Options").to_owned().into(),
|
|
||||||
))
|
|
||||||
.hide(a.is_hide_set())
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -489,6 +472,19 @@ fn shorts_and_visible_aliases(p: &clap::Command) -> Vec<CompletionCandidate> {
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn populate_arg_candidate(candidate: CompletionCandidate, arg: &clap::Arg) -> CompletionCandidate {
|
||||||
|
candidate
|
||||||
|
.help(arg.get_help().cloned())
|
||||||
|
.id(Some(format!("arg::{}", arg.get_id())))
|
||||||
|
.tag(Some(
|
||||||
|
arg.get_help_heading()
|
||||||
|
.unwrap_or("Options")
|
||||||
|
.to_owned()
|
||||||
|
.into(),
|
||||||
|
))
|
||||||
|
.hide(arg.is_hide_set())
|
||||||
|
}
|
||||||
|
|
||||||
/// Get the possible values for completion
|
/// Get the possible values for completion
|
||||||
fn possible_values(a: &clap::Arg) -> Option<Vec<clap::builder::PossibleValue>> {
|
fn possible_values(a: &clap::Arg) -> Option<Vec<clap::builder::PossibleValue>> {
|
||||||
if !a.get_num_args().expect("built").takes_values() {
|
if !a.get_num_args().expect("built").takes_values() {
|
||||||
|
@ -511,34 +507,31 @@ fn subcommands(p: &clap::Command) -> Vec<CompletionCandidate> {
|
||||||
.flat_map(|sc| {
|
.flat_map(|sc| {
|
||||||
sc.get_name_and_visible_aliases()
|
sc.get_name_and_visible_aliases()
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|s| {
|
.map(|s| populate_command_candidate(CompletionCandidate::new(s.to_string()), p, sc))
|
||||||
CompletionCandidate::new(s.to_string())
|
|
||||||
.help(sc.get_about().cloned())
|
|
||||||
.id(Some(format!("command::{}", sc.get_name())))
|
|
||||||
.tag(Some(
|
|
||||||
p.get_subcommand_help_heading()
|
|
||||||
.unwrap_or("Commands")
|
|
||||||
.to_owned()
|
|
||||||
.into(),
|
|
||||||
))
|
|
||||||
.hide(sc.is_hide_set())
|
|
||||||
})
|
|
||||||
.chain(sc.get_aliases().map(|s| {
|
.chain(sc.get_aliases().map(|s| {
|
||||||
CompletionCandidate::new(s.to_string())
|
populate_command_candidate(CompletionCandidate::new(s.to_string()), p, sc)
|
||||||
.help(sc.get_about().cloned())
|
|
||||||
.id(Some(format!("command::{}", sc.get_name())))
|
|
||||||
.tag(Some(
|
|
||||||
p.get_subcommand_help_heading()
|
|
||||||
.unwrap_or("Commands")
|
|
||||||
.to_owned()
|
|
||||||
.into(),
|
|
||||||
))
|
|
||||||
.hide(true)
|
.hide(true)
|
||||||
}))
|
}))
|
||||||
})
|
})
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn populate_command_candidate(
|
||||||
|
candidate: CompletionCandidate,
|
||||||
|
cmd: &clap::Command,
|
||||||
|
subcommand: &clap::Command,
|
||||||
|
) -> CompletionCandidate {
|
||||||
|
candidate
|
||||||
|
.help(subcommand.get_about().cloned())
|
||||||
|
.id(Some(format!("command::{}", subcommand.get_name())))
|
||||||
|
.tag(Some(
|
||||||
|
cmd.get_subcommand_help_heading()
|
||||||
|
.unwrap_or("Commands")
|
||||||
|
.to_owned()
|
||||||
|
.into(),
|
||||||
|
))
|
||||||
|
.hide(subcommand.is_hide_set())
|
||||||
|
}
|
||||||
/// Parse the short flags and find the first `takes_values` option.
|
/// Parse the short flags and find the first `takes_values` option.
|
||||||
fn parse_shortflags<'c, 's>(
|
fn parse_shortflags<'c, 's>(
|
||||||
cmd: &'c clap::Command,
|
cmd: &'c clap::Command,
|
||||||
|
|
Loading…
Reference in a new issue