fix: Tests/clippy/errors bash dynamic completion

This commit is contained in:
Roland Fredenhagen 2022-09-02 20:11:11 +02:00 committed by Ed Page
parent 4e703d1e2e
commit a2cf7f1dea
4 changed files with 41 additions and 33 deletions

View file

@ -18,7 +18,7 @@ _FEATURES_default =
_FEATURES_wasm = --features "deprecated derive cargo env unicode perf unstable-replace unstable-grouped"
_FEATURES_full = --features "deprecated derive cargo env unicode perf unstable-replace unstable-grouped wrap_help"
_FEATURES_next = ${_FEATURES_full} --features unstable-v5
_FEATURES_debug = ${_FEATURES_full} --features debug
_FEATURES_debug = ${_FEATURES_full} --features debug --features clap_complete/debug
_FEATURES_release = ${_FEATURES_full} --release
check-%:

View file

@ -5,19 +5,18 @@ fn command() -> clap::Command {
let cmd = clap::Command::new("dynamic")
.arg(
clap::Arg::new("input")
.long("--input")
.long("input")
.short('i')
.value_hint(clap::ValueHint::FilePath),
)
.arg(
clap::Arg::new("format")
.long("--format")
.long("format")
.short('F')
.value_parser(["json", "yaml", "toml"]),
)
.args_conflicts_with_subcommands(true);
let cmd = clap_complete::dynamic::bash::CompleteCommand::augment_subcommands(cmd);
cmd
clap_complete::dynamic::bash::CompleteCommand::augment_subcommands(cmd)
}
fn main() {
@ -34,6 +33,5 @@ fn main() {
#[test]
fn verify_cli() {
use clap::CommandFactory;
command().command().debug_assert();
command().debug_assert();
}

View file

@ -74,17 +74,15 @@ pub mod bash {
if let Some(out_path) = args.register.as_deref() {
let mut buf = Vec::new();
let name = cmd.get_name();
let bin = cmd.get_bin_name().unwrap_or(cmd.get_name());
let bin = cmd.get_bin_name().unwrap_or_else(|| cmd.get_name());
register(name, [bin], bin, &Behavior::default(), &mut buf)?;
if out_path == std::path::Path::new("-") {
std::io::stdout().write(&buf)?;
std::io::stdout().write_all(&buf)?;
} else if out_path.is_dir() {
let out_path = out_path.join(file_name(name));
std::fs::write(out_path, buf)?;
} else {
if out_path.is_dir() {
let out_path = out_path.join(file_name(name));
std::fs::write(out_path, buf)?;
} else {
std::fs::write(out_path, buf)?;
}
std::fs::write(out_path, buf)?;
}
} else {
let index = args.index.unwrap_or_default();
@ -115,7 +113,7 @@ pub mod bash {
}
write!(&mut buf, "{}", completion.to_string_lossy())?;
}
std::io::stdout().write(&buf)?;
std::io::stdout().write_all(&buf)?;
}
Ok(())
@ -151,7 +149,7 @@ pub mod bash {
behavior: &Behavior,
buf: &mut dyn Write,
) -> Result<(), std::io::Error> {
let escaped_name = name.replace("-", "_");
let escaped_name = name.replace('-', "_");
debug_assert!(
escaped_name.chars().all(|c| c.is_xid_continue()),
"`name` must be an identifier, got `{}`",

View file

@ -11,7 +11,7 @@ fn suggest_subcommand_subset() {
let args = [name, "he"];
let arg_index = 1;
let args = IntoIterator::into_iter(args)
.map(|s| std::ffi::OsString::from(s))
.map(std::ffi::OsString::from)
.collect::<Vec<_>>();
let comp_type = clap_complete::dynamic::bash::CompType::default();
let trailing_space = true;
@ -40,24 +40,24 @@ fn suggest_long_flag_subset() {
let mut cmd = clap::Command::new(name)
.arg(
clap::Arg::new("hello-world")
.long("--hello-world")
.multiple_occurrences(true),
.long("hello-world")
.action(clap::ArgAction::Count),
)
.arg(
clap::Arg::new("hello-moon")
.long("--hello-moon")
.multiple_occurrences(true),
.long("hello-moon")
.action(clap::ArgAction::Count),
)
.arg(
clap::Arg::new("goodbye-world")
.long("--goodbye-world")
.multiple_occurrences(true),
.long("goodbye-world")
.action(clap::ArgAction::Count),
);
let args = [name, "--he"];
let arg_index = 1;
let args = IntoIterator::into_iter(args)
.map(|s| std::ffi::OsString::from(s))
.map(std::ffi::OsString::from)
.collect::<Vec<_>>();
let comp_type = clap_complete::dynamic::bash::CompType::default();
let trailing_space = true;
@ -77,13 +77,13 @@ fn suggest_long_flag_subset() {
.map(|s| s.to_string_lossy().into_owned())
.collect::<Vec<_>>();
assert_eq!(completions, ["--help", "--hello-world", "--hello-moon"]);
assert_eq!(completions, ["--hello-world", "--hello-moon", "--help"]);
}
#[test]
fn suggest_possible_value_subset() {
let name = "test";
let mut cmd = clap::Command::new(name).arg(clap::Arg::new("hello-world").possible_values([
let mut cmd = clap::Command::new(name).arg(clap::Arg::new("hello-world").value_parser([
"hello-world",
"hello-moon",
"goodbye-world",
@ -92,7 +92,7 @@ fn suggest_possible_value_subset() {
let args = [name, "hello"];
let arg_index = 1;
let args = IntoIterator::into_iter(args)
.map(|s| std::ffi::OsString::from(s))
.map(std::ffi::OsString::from)
.collect::<Vec<_>>();
let comp_type = clap_complete::dynamic::bash::CompType::default();
let trailing_space = true;
@ -119,14 +119,26 @@ fn suggest_possible_value_subset() {
fn suggest_additional_short_flags() {
let name = "test";
let mut cmd = clap::Command::new(name)
.arg(clap::Arg::new("a").short('a').multiple_occurrences(true))
.arg(clap::Arg::new("b").short('b').multiple_occurrences(true))
.arg(clap::Arg::new("c").short('c').multiple_occurrences(true));
.arg(
clap::Arg::new("a")
.short('a')
.action(clap::ArgAction::Count),
)
.arg(
clap::Arg::new("b")
.short('b')
.action(clap::ArgAction::Count),
)
.arg(
clap::Arg::new("c")
.short('c')
.action(clap::ArgAction::Count),
);
let args = [name, "-a"];
let arg_index = 1;
let args = IntoIterator::into_iter(args)
.map(|s| std::ffi::OsString::from(s))
.map(std::ffi::OsString::from)
.collect::<Vec<_>>();
let comp_type = clap_complete::dynamic::bash::CompType::default();
let trailing_space = true;
@ -146,5 +158,5 @@ fn suggest_additional_short_flags() {
.map(|s| s.to_string_lossy().into_owned())
.collect::<Vec<_>>();
assert_eq!(completions, ["-ah", "-aa", "-ab", "-ac"]);
assert_eq!(completions, ["-aa", "-ab", "-ac", "-ah"]);
}