Skip to content

Commit

Permalink
fix: Tests/clippy/errors bash dynamic completion
Browse files Browse the repository at this point in the history
  • Loading branch information
ModProg committed Sep 2, 2022
1 parent 5e69f92 commit 6a20bc9
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 28 deletions.
3 changes: 1 addition & 2 deletions clap_complete/examples/dynamic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ fn command() -> clap::Command {
.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() {
Expand Down
18 changes: 8 additions & 10 deletions clap_complete/src/dynamic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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(())
Expand Down Expand Up @@ -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 `{}`",
Expand Down
44 changes: 28 additions & 16 deletions clap_complete/tests/dynamic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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::Append),
)
.arg(
clap::Arg::new("hello-moon")
.long("--hello-moon")
.multiple_occurrences(true),
.long("hello-moon")
.action(clap::ArgAction::Append),
)
.arg(
clap::Arg::new("goodbye-world")
.long("--goodbye-world")
.multiple_occurrences(true),
.long("goodbye-world")
.action(clap::ArgAction::Append),
);

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;
Expand All @@ -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",
Expand All @@ -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;
Expand All @@ -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::Append),
)
.arg(
clap::Arg::new("b")
.short('b')
.action(clap::ArgAction::Append),
)
.arg(
clap::Arg::new("c")
.short('c')
.action(clap::ArgAction::Append),
);

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;
Expand All @@ -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"]);
}

0 comments on commit 6a20bc9

Please sign in to comment.