mirror of
https://github.com/clap-rs/clap
synced 2024-11-10 06:44:16 +00:00
feat(complete): Complete value_parser possible values
This commit is contained in:
parent
0088b1643c
commit
f3d8ef248f
7 changed files with 19 additions and 6 deletions
|
@ -438,7 +438,7 @@ complete OPTIONS -F _clap_complete_NAME EXECUTABLES
|
|||
let mut values = Vec::new();
|
||||
debug!("complete_arg_value: arg={:?}, value={:?}", arg, value);
|
||||
|
||||
if let Some(possible_values) = arg.get_possible_values() {
|
||||
if let Some(possible_values) = crate::generator::utils::possible_values(arg) {
|
||||
if let Ok(value) = value {
|
||||
values.extend(possible_values.into_iter().filter_map(|p| {
|
||||
let name = p.get_name();
|
||||
|
|
|
@ -126,6 +126,19 @@ pub fn flags<'help>(p: &Command<'help>) -> Vec<Arg<'help>> {
|
|||
.collect()
|
||||
}
|
||||
|
||||
/// Get the possible values for completion
|
||||
pub fn possible_values<'help>(a: &Arg<'help>) -> Option<Vec<clap::PossibleValue<'help>>> {
|
||||
if let Some(pvs) = a.get_possible_values() {
|
||||
// Check old first in case the user explicitly set possible values and the derive inferred
|
||||
// a `ValueParser` with some.
|
||||
Some(pvs.iter().map(|pv| pv.clone()).collect())
|
||||
} else if let Some(pvs) = a.get_value_parser().possible_values() {
|
||||
Some(pvs.collect())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
|
|
@ -174,7 +174,7 @@ fn option_details_for_path(cmd: &Command, path: &str) -> String {
|
|||
fn vals_for(o: &Arg) -> String {
|
||||
debug!("vals_for: o={}", o.get_id());
|
||||
|
||||
if let Some(vals) = o.get_possible_values() {
|
||||
if let Some(vals) = crate::generator::utils::possible_values(o) {
|
||||
format!(
|
||||
"$(compgen -W \"{}\" -- \"${{cur}}\")",
|
||||
vals.iter()
|
||||
|
|
|
@ -152,7 +152,7 @@ fn value_completion(option: &Arg) -> String {
|
|||
return "".to_string();
|
||||
}
|
||||
|
||||
if let Some(data) = option.get_possible_values() {
|
||||
if let Some(data) = crate::generator::utils::possible_values(option) {
|
||||
// We return the possible values with their own empty description e.g. {a\t,b\t}
|
||||
// this makes sure that a and b don't get the description of the option or argument
|
||||
format!(
|
||||
|
|
|
@ -359,7 +359,7 @@ fn get_args_of(parent: &Command, p_global: Option<&Command>) -> String {
|
|||
|
||||
// Uses either `possible_vals` or `value_hint` to give hints about possible argument values
|
||||
fn value_completion(arg: &Arg) -> Option<String> {
|
||||
if let Some(values) = &arg.get_possible_values() {
|
||||
if let Some(values) = crate::generator::utils::possible_values(arg) {
|
||||
if values
|
||||
.iter()
|
||||
.any(|value| !value.is_hide_set() && value.get_help().is_some())
|
||||
|
|
|
@ -350,7 +350,7 @@ fn gen_args(arg: &Arg, indent: usize) -> String {
|
|||
));
|
||||
}
|
||||
|
||||
if let Some(data) = arg.get_possible_values() {
|
||||
if let Some(data) = generator::utils::possible_values(arg) {
|
||||
buffer.push_str(&format!(
|
||||
"{:indent$}suggestions: [\n",
|
||||
"",
|
||||
|
|
|
@ -4633,7 +4633,7 @@ impl<'help> Arg<'help> {
|
|||
|
||||
/// Get the list of the possible values for this argument, if any
|
||||
#[inline]
|
||||
pub fn get_possible_values(&self) -> Option<&[PossibleValue]> {
|
||||
pub fn get_possible_values(&self) -> Option<&[PossibleValue<'help>]> {
|
||||
if self.possible_vals.is_empty() {
|
||||
None
|
||||
} else {
|
||||
|
|
Loading…
Reference in a new issue