2022-04-27 20:38:08 +00:00
|
|
|
#![cfg(feature = "unstable-dynamic")]
|
|
|
|
|
2023-08-03 18:13:49 +00:00
|
|
|
use std::path::Path;
|
|
|
|
|
2023-08-07 07:01:16 +00:00
|
|
|
use clap::{builder::PossibleValue, Command};
|
2023-08-03 18:13:49 +00:00
|
|
|
|
|
|
|
macro_rules! complete {
|
|
|
|
($cmd:expr, $input:expr$(, current_dir = $current_dir:expr)? $(,)?) => {
|
|
|
|
{
|
|
|
|
#[allow(unused)]
|
|
|
|
let current_dir = None;
|
|
|
|
$(let current_dir = $current_dir;)?
|
|
|
|
complete(&mut $cmd, $input, current_dir)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-27 20:38:08 +00:00
|
|
|
#[test]
|
2022-04-27 20:40:24 +00:00
|
|
|
fn suggest_subcommand_subset() {
|
2023-08-03 18:13:49 +00:00
|
|
|
let mut cmd = Command::new("exhaustive")
|
|
|
|
.subcommand(Command::new("hello-world"))
|
|
|
|
.subcommand(Command::new("hello-moon"))
|
|
|
|
.subcommand(Command::new("goodbye-world"));
|
|
|
|
|
|
|
|
snapbox::assert_eq(
|
|
|
|
"hello-moon
|
|
|
|
hello-world
|
|
|
|
help\tPrint this message or the help of the given subcommand(s)",
|
|
|
|
complete!(cmd, "he"),
|
|
|
|
);
|
2022-04-27 20:40:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2022-04-27 20:40:49 +00:00
|
|
|
fn suggest_long_flag_subset() {
|
2023-08-03 18:13:49 +00:00
|
|
|
let mut cmd = Command::new("exhaustive")
|
2022-04-27 20:40:49 +00:00
|
|
|
.arg(
|
|
|
|
clap::Arg::new("hello-world")
|
2022-09-02 18:11:11 +00:00
|
|
|
.long("hello-world")
|
|
|
|
.action(clap::ArgAction::Count),
|
2022-04-27 20:40:49 +00:00
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
clap::Arg::new("hello-moon")
|
2022-09-02 18:11:11 +00:00
|
|
|
.long("hello-moon")
|
|
|
|
.action(clap::ArgAction::Count),
|
2022-04-27 20:40:49 +00:00
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
clap::Arg::new("goodbye-world")
|
2022-09-02 18:11:11 +00:00
|
|
|
.long("goodbye-world")
|
|
|
|
.action(clap::ArgAction::Count),
|
2022-04-27 20:40:49 +00:00
|
|
|
);
|
|
|
|
|
2023-08-03 18:13:49 +00:00
|
|
|
snapbox::assert_eq(
|
|
|
|
"--hello-world
|
|
|
|
--hello-moon
|
|
|
|
--help\tPrint help",
|
|
|
|
complete!(cmd, "--he"),
|
|
|
|
);
|
2022-04-27 20:40:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2022-04-27 20:53:02 +00:00
|
|
|
fn suggest_possible_value_subset() {
|
2023-07-28 18:54:02 +00:00
|
|
|
let name = "exhaustive";
|
2023-08-03 18:13:49 +00:00
|
|
|
let mut cmd = Command::new(name).arg(clap::Arg::new("hello-world").value_parser([
|
2023-08-07 07:01:16 +00:00
|
|
|
PossibleValue::new("hello-world").help("Say hello to the world"),
|
|
|
|
"hello-moon".into(),
|
|
|
|
"goodbye-world".into(),
|
2022-04-27 20:53:02 +00:00
|
|
|
]));
|
|
|
|
|
2023-08-03 18:13:49 +00:00
|
|
|
snapbox::assert_eq(
|
2023-08-07 07:01:16 +00:00
|
|
|
"hello-world\tSay hello to the world
|
2023-08-03 18:13:49 +00:00
|
|
|
hello-moon",
|
|
|
|
complete!(cmd, "hello"),
|
|
|
|
);
|
2022-04-27 20:53:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2022-04-27 20:38:08 +00:00
|
|
|
fn suggest_additional_short_flags() {
|
2023-08-03 18:13:49 +00:00
|
|
|
let mut cmd = Command::new("exhaustive")
|
2022-09-02 18:11:11 +00:00
|
|
|
.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),
|
|
|
|
);
|
2022-04-27 20:38:08 +00:00
|
|
|
|
2023-08-03 18:13:49 +00:00
|
|
|
snapbox::assert_eq(
|
|
|
|
"-aa
|
|
|
|
-ab
|
|
|
|
-ac
|
|
|
|
-ah\tPrint help",
|
|
|
|
complete!(cmd, "-a"),
|
|
|
|
);
|
|
|
|
}
|
2022-04-27 20:38:08 +00:00
|
|
|
|
2023-09-25 21:10:42 +00:00
|
|
|
#[test]
|
|
|
|
fn suggest_subcommand_positional() {
|
|
|
|
let mut cmd = Command::new("exhaustive").subcommand(Command::new("hello-world").arg(
|
|
|
|
clap::Arg::new("hello-world").value_parser([
|
|
|
|
PossibleValue::new("hello-world").help("Say hello to the world"),
|
|
|
|
"hello-moon".into(),
|
|
|
|
"goodbye-world".into(),
|
|
|
|
]),
|
|
|
|
));
|
|
|
|
|
|
|
|
snapbox::assert_eq(
|
|
|
|
"--help\tPrint help (see more with '--help')
|
|
|
|
-h\tPrint help (see more with '--help')",
|
|
|
|
complete!(cmd, "hello-world [TAB]"),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-08-03 18:13:49 +00:00
|
|
|
fn complete(cmd: &mut Command, args: impl AsRef<str>, current_dir: Option<&Path>) -> String {
|
|
|
|
let input = args.as_ref();
|
|
|
|
let mut args = vec![std::ffi::OsString::from(cmd.get_name())];
|
|
|
|
let arg_index;
|
|
|
|
|
|
|
|
if let Some((prior, after)) = input.split_once("[TAB]") {
|
|
|
|
args.extend(prior.split_whitespace().map(From::from));
|
|
|
|
if prior.ends_with(char::is_whitespace) {
|
|
|
|
args.push(std::ffi::OsString::default())
|
|
|
|
}
|
|
|
|
arg_index = args.len() - 1;
|
|
|
|
// HACK: this cannot handle in-word '[TAB]'
|
|
|
|
args.extend(after.split_whitespace().map(From::from));
|
|
|
|
} else {
|
|
|
|
args.extend(input.split_whitespace().map(From::from));
|
|
|
|
if input.ends_with(char::is_whitespace) {
|
|
|
|
args.push(std::ffi::OsString::default())
|
|
|
|
}
|
|
|
|
arg_index = args.len() - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
clap_complete::dynamic::complete(cmd, args, arg_index, current_dir)
|
|
|
|
.unwrap()
|
2022-04-27 20:38:08 +00:00
|
|
|
.into_iter()
|
2023-08-03 18:13:49 +00:00
|
|
|
.map(|(compl, help)| {
|
|
|
|
let compl = compl.to_str().unwrap();
|
|
|
|
if let Some(help) = help {
|
|
|
|
format!("{compl}\t{help}")
|
|
|
|
} else {
|
|
|
|
compl.to_owned()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.join("\n")
|
2022-04-27 20:38:08 +00:00
|
|
|
}
|