From 4395c31cc57f9d039cc7920ec135d48024bf3b2b Mon Sep 17 00:00:00 2001 From: shannmu Date: Wed, 10 Jul 2024 22:52:24 +0800 Subject: [PATCH 1/2] test(clap_complete): Add test case for hiding possible values --- clap_complete/tests/testsuite/dynamic.rs | 25 ++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/clap_complete/tests/testsuite/dynamic.rs b/clap_complete/tests/testsuite/dynamic.rs index 2863da32034..f11cccbd5f5 100644 --- a/clap_complete/tests/testsuite/dynamic.rs +++ b/clap_complete/tests/testsuite/dynamic.rs @@ -122,6 +122,31 @@ hello-world-foo" ); } +#[test] +fn suggest_hidden_possible_value() { + let mut cmd = Command::new("exhaustive").arg( + clap::Arg::new("possible_value").long("test").value_parser([ + PossibleValue::new("test-visible").help("Say hello to the world"), + PossibleValue::new("test-hidden") + .help("Say hello to the moon") + .hide(true), + ]), + ); + + assert_data_eq!( + complete!(cmd, "--test=test"), + snapbox::str![ + "--test=test-visible\tSay hello to the world +--test=test-hidden\tSay hello to the moon" + ] + ); + + assert_data_eq!( + complete!(cmd, "--test=test-h"), + snapbox::str!["--test=test-hidden\tSay hello to the moon"] + ) +} + #[test] fn suggest_hidden_long_flag_aliases() { let mut cmd = Command::new("exhaustive") From 9220bbdeba105fa9dbe7d16d30eace5d4adb8980 Mon Sep 17 00:00:00 2001 From: shannmu Date: Wed, 10 Jul 2024 22:52:53 +0800 Subject: [PATCH 2/2] feat(clap_complete): Support hiding possible values --- clap_complete/src/dynamic/completer.rs | 38 ++++++++++++++---------- clap_complete/tests/testsuite/dynamic.rs | 5 +--- 2 files changed, 23 insertions(+), 20 deletions(-) diff --git a/clap_complete/src/dynamic/completer.rs b/clap_complete/src/dynamic/completer.rs index 6a650f746bc..19966f47665 100644 --- a/clap_complete/src/dynamic/completer.rs +++ b/clap_complete/src/dynamic/completer.rs @@ -110,15 +110,14 @@ fn complete_arg( completions.extend( complete_arg_value(value.to_str().ok_or(value), arg, current_dir) .into_iter() - .map(|(os, help)| { - // HACK: Need better `OsStr` manipulation + .map(|comp| { CompletionCandidate::new(format!( "--{}={}", flag, - os.to_string_lossy() + comp.get_content().to_string_lossy() )) - .help(help) - .visible(true) + .help(comp.get_help().cloned()) + .visible(comp.is_visible()) }), ); } @@ -169,11 +168,7 @@ fn complete_arg( .get_positionals() .find(|p| p.get_index() == Some(pos_index)) { - completions.extend( - complete_arg_value(arg.to_value(), positional, current_dir) - .into_iter() - .map(|(os, help)| CompletionCandidate::new(os).help(help).visible(true)), - ); + completions.extend(complete_arg_value(arg.to_value(), positional, current_dir).into_iter()); } if let Ok(value) = arg.to_value() { @@ -191,7 +186,7 @@ fn complete_arg_value( value: Result<&str, &OsStr>, arg: &clap::Arg, current_dir: Option<&std::path::Path>, -) -> Vec<(OsString, Option)> { +) -> Vec { let mut values = Vec::new(); debug!("complete_arg_value: arg={arg:?}, value={value:?}"); @@ -199,8 +194,11 @@ fn complete_arg_value( if let Ok(value) = value { values.extend(possible_values.into_iter().filter_map(|p| { let name = p.get_name(); - name.starts_with(value) - .then(|| (OsString::from(name), p.get_help().cloned())) + name.starts_with(value).then(|| { + CompletionCandidate::new(OsString::from(name)) + .help(p.get_help().cloned()) + .visible(!p.is_hide_set()) + }) })); } } else { @@ -249,7 +247,7 @@ fn complete_path( value_os: &OsStr, current_dir: Option<&std::path::Path>, is_wanted: impl Fn(&std::path::Path) -> bool, -) -> Vec<(OsString, Option)> { +) -> Vec { let mut completions = Vec::new(); let current_dir = match current_dir { @@ -281,12 +279,20 @@ fn complete_path( let path = entry.path(); let mut suggestion = pathdiff::diff_paths(&path, current_dir).unwrap_or(path); suggestion.push(""); // Ensure trailing `/` - completions.push((suggestion.as_os_str().to_owned(), None)); + completions.push( + CompletionCandidate::new(suggestion.as_os_str().to_owned()) + .help(None) + .visible(true), + ); } else { let path = entry.path(); if is_wanted(&path) { let suggestion = pathdiff::diff_paths(&path, current_dir).unwrap_or(path); - completions.push((suggestion.as_os_str().to_owned(), None)); + completions.push( + CompletionCandidate::new(suggestion.as_os_str().to_owned()) + .help(None) + .visible(true), + ); } } } diff --git a/clap_complete/tests/testsuite/dynamic.rs b/clap_complete/tests/testsuite/dynamic.rs index f11cccbd5f5..6eff7f29c18 100644 --- a/clap_complete/tests/testsuite/dynamic.rs +++ b/clap_complete/tests/testsuite/dynamic.rs @@ -135,10 +135,7 @@ fn suggest_hidden_possible_value() { assert_data_eq!( complete!(cmd, "--test=test"), - snapbox::str![ - "--test=test-visible\tSay hello to the world ---test=test-hidden\tSay hello to the moon" - ] + snapbox::str!["--test=test-visible\tSay hello to the world"] ); assert_data_eq!(