clap/tests/common.rs

108 lines
3.2 KiB
Rust
Raw Normal View History

2022-10-22 15:27:04 +00:00
use clap::{builder::PossibleValue, Arg, ArgAction, Command, ValueHint};
pub fn basic_command(name: &'static str) -> Command {
Command::new(name)
.arg(
Arg::new("config")
.short('c')
.global(true)
.action(ArgAction::SetTrue),
)
.arg(
Arg::new("v")
.short('v')
.conflicts_with("config")
.action(ArgAction::SetTrue),
)
.subcommand(
Command::new("test")
.about("Subcommand")
.arg(Arg::new("debug").short('d').action(ArgAction::Count)),
)
}
pub fn feature_sample_command(name: &'static str) -> Command {
Command::new(name)
.version("3.0")
.propagate_version(true)
.about("Tests completions")
.arg(
Arg::new("file")
.value_hint(ValueHint::FilePath)
.help("some input file"),
)
.arg(
Arg::new("config")
.action(ArgAction::Count)
.help("some config file")
.short('c')
.visible_short_alias('C')
.long("config")
.visible_alias("conf"),
)
.arg(Arg::new("choice").value_parser(["first", "second"]))
.subcommand(
Command::new("test").about("tests things").arg(
Arg::new("case")
.long("case")
.action(ArgAction::Set)
.help("the case to test"),
),
)
}
2022-10-31 10:32:40 +00:00
pub fn aliases_command(name: &'static str) -> Command {
Command::new(name)
.version("3.0")
.about("testing nushell completions")
.arg(
Arg::new("flag")
.short('f')
.visible_short_alias('F')
.long("flag")
.action(ArgAction::SetTrue)
.visible_alias("flg")
.help("cmd flag"),
)
.arg(
Arg::new("option")
.short('o')
.visible_short_alias('O')
.long("option")
.visible_alias("opt")
.help("cmd option")
.action(ArgAction::Set),
)
.arg(Arg::new("positional"))
}
2022-10-22 15:27:04 +00:00
pub fn sub_subcommands_command(name: &'static str) -> Command {
feature_sample_command(name).subcommand(
Command::new("some_cmd")
.about("top level subcommand")
.subcommand(
Command::new("sub_cmd").about("sub-subcommand").arg(
Arg::new("config")
.long("config")
.action(ArgAction::Set)
.value_parser([PossibleValue::new("Lest quotes aren't escaped.")])
.help("the other case to test"),
),
),
)
}
pub fn assert_matches_path(
expected_path: impl AsRef<std::path::Path>,
gen: impl clap_complete::Generator,
mut cmd: Command,
name: &'static str,
) {
let mut buf = vec![];
clap_complete::generate(gen, &mut cmd, name, &mut buf);
snapbox::Assert::new()
.action_env("SNAPSHOTS")
.matches_path(expected_path, buf);
}