mirror of
https://github.com/clap-rs/clap
synced 2024-12-14 06:42:33 +00:00
272f840178
This is a part of #2717 Some settings didn't get getters because - They are transient parse settings (e.g. ignore errors) - They get propagated to args and should be checked there `is_allow_hyphen_values_set` is a curious case. In some cases, we only check the app and not an arg. This seems suspicious.
331 lines
9 KiB
Rust
331 lines
9 KiB
Rust
use super::*;
|
|
|
|
fn build_app() -> App<'static> {
|
|
build_app_with_name("myapp")
|
|
}
|
|
|
|
fn build_app_with_name(s: &'static str) -> App<'static> {
|
|
App::new(s)
|
|
.version("3.0")
|
|
.propagate_version(true)
|
|
.about("Tests completions")
|
|
.arg(
|
|
Arg::new("file")
|
|
.value_hint(ValueHint::FilePath)
|
|
.help("some input file"),
|
|
)
|
|
.arg(Arg::new("choice").possible_values(["first", "second"]))
|
|
.subcommand(
|
|
App::new("test").about("tests things").arg(
|
|
Arg::new("case")
|
|
.long("case")
|
|
.takes_value(true)
|
|
.help("the case to test"),
|
|
),
|
|
)
|
|
}
|
|
|
|
#[test]
|
|
fn bash() {
|
|
let mut app = build_app();
|
|
common(Bash, &mut app, "myapp", BASH);
|
|
}
|
|
|
|
static BASH: &str = r#"_myapp() {
|
|
local i cur prev opts cmds
|
|
COMPREPLY=()
|
|
cur="${COMP_WORDS[COMP_CWORD]}"
|
|
prev="${COMP_WORDS[COMP_CWORD-1]}"
|
|
cmd=""
|
|
opts=""
|
|
|
|
for i in ${COMP_WORDS[@]}
|
|
do
|
|
case "${i}" in
|
|
"$1")
|
|
cmd="myapp"
|
|
;;
|
|
help)
|
|
cmd+="__help"
|
|
;;
|
|
test)
|
|
cmd+="__test"
|
|
;;
|
|
*)
|
|
;;
|
|
esac
|
|
done
|
|
|
|
case "${cmd}" in
|
|
myapp)
|
|
opts="-h -V --help --version <file> first second test help"
|
|
if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then
|
|
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
|
|
return 0
|
|
fi
|
|
case "${prev}" in
|
|
*)
|
|
COMPREPLY=()
|
|
;;
|
|
esac
|
|
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
|
|
return 0
|
|
;;
|
|
myapp__help)
|
|
opts="<SUBCOMMAND>..."
|
|
if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then
|
|
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
|
|
return 0
|
|
fi
|
|
case "${prev}" in
|
|
*)
|
|
COMPREPLY=()
|
|
;;
|
|
esac
|
|
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
|
|
return 0
|
|
;;
|
|
myapp__test)
|
|
opts="-h -V --case --help --version"
|
|
if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then
|
|
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
|
|
return 0
|
|
fi
|
|
case "${prev}" in
|
|
--case)
|
|
COMPREPLY=($(compgen -f "${cur}"))
|
|
return 0
|
|
;;
|
|
*)
|
|
COMPREPLY=()
|
|
;;
|
|
esac
|
|
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
|
|
return 0
|
|
;;
|
|
esac
|
|
}
|
|
|
|
complete -F _myapp -o bashdefault -o default myapp
|
|
"#;
|
|
|
|
#[test]
|
|
fn bash_with_special_commands() {
|
|
let mut app = build_app_special_commands();
|
|
common(Bash, &mut app, "my_app", BASH_SPECIAL_CMDS);
|
|
}
|
|
|
|
fn build_app_special_commands() -> App<'static> {
|
|
build_app_with_name("my_app")
|
|
.subcommand(
|
|
App::new("some_cmd").about("tests other things").arg(
|
|
Arg::new("config")
|
|
.long("--config")
|
|
.takes_value(true)
|
|
.help("the other case to test"),
|
|
),
|
|
)
|
|
.subcommand(App::new("some-cmd-with-hyphens").alias("hyphen"))
|
|
}
|
|
|
|
static BASH_SPECIAL_CMDS: &str = r#"_my_app() {
|
|
local i cur prev opts cmds
|
|
COMPREPLY=()
|
|
cur="${COMP_WORDS[COMP_CWORD]}"
|
|
prev="${COMP_WORDS[COMP_CWORD-1]}"
|
|
cmd=""
|
|
opts=""
|
|
|
|
for i in ${COMP_WORDS[@]}
|
|
do
|
|
case "${i}" in
|
|
"$1")
|
|
cmd="my_app"
|
|
;;
|
|
help)
|
|
cmd+="__help"
|
|
;;
|
|
some-cmd-with-hyphens)
|
|
cmd+="__some__cmd__with__hyphens"
|
|
;;
|
|
some_cmd)
|
|
cmd+="__some_cmd"
|
|
;;
|
|
test)
|
|
cmd+="__test"
|
|
;;
|
|
*)
|
|
;;
|
|
esac
|
|
done
|
|
|
|
case "${cmd}" in
|
|
my_app)
|
|
opts="-h -V --help --version <file> first second test some_cmd some-cmd-with-hyphens help"
|
|
if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then
|
|
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
|
|
return 0
|
|
fi
|
|
case "${prev}" in
|
|
*)
|
|
COMPREPLY=()
|
|
;;
|
|
esac
|
|
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
|
|
return 0
|
|
;;
|
|
my_app__help)
|
|
opts="<SUBCOMMAND>..."
|
|
if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then
|
|
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
|
|
return 0
|
|
fi
|
|
case "${prev}" in
|
|
*)
|
|
COMPREPLY=()
|
|
;;
|
|
esac
|
|
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
|
|
return 0
|
|
;;
|
|
my_app__some__cmd__with__hyphens)
|
|
opts="-h -V --help --version"
|
|
if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then
|
|
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
|
|
return 0
|
|
fi
|
|
case "${prev}" in
|
|
*)
|
|
COMPREPLY=()
|
|
;;
|
|
esac
|
|
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
|
|
return 0
|
|
;;
|
|
my_app__some_cmd)
|
|
opts="-h -V --config --help --version"
|
|
if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then
|
|
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
|
|
return 0
|
|
fi
|
|
case "${prev}" in
|
|
--config)
|
|
COMPREPLY=($(compgen -f "${cur}"))
|
|
return 0
|
|
;;
|
|
*)
|
|
COMPREPLY=()
|
|
;;
|
|
esac
|
|
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
|
|
return 0
|
|
;;
|
|
my_app__test)
|
|
opts="-h -V --case --help --version"
|
|
if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then
|
|
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
|
|
return 0
|
|
fi
|
|
case "${prev}" in
|
|
--case)
|
|
COMPREPLY=($(compgen -f "${cur}"))
|
|
return 0
|
|
;;
|
|
*)
|
|
COMPREPLY=()
|
|
;;
|
|
esac
|
|
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
|
|
return 0
|
|
;;
|
|
esac
|
|
}
|
|
|
|
complete -F _my_app -o bashdefault -o default my_app
|
|
"#;
|
|
|
|
#[test]
|
|
fn bash_with_aliases() {
|
|
let mut app = build_app_with_aliases();
|
|
common(Bash, &mut app, "cmd", BASH_ALIASES);
|
|
}
|
|
|
|
fn build_app_with_aliases() -> App<'static> {
|
|
App::new("cmd")
|
|
.version("3.0")
|
|
.about("testing bash completions")
|
|
.arg(
|
|
Arg::new("flag")
|
|
.short('f')
|
|
.visible_short_alias('F')
|
|
.long("flag")
|
|
.visible_alias("flg")
|
|
.help("cmd flag"),
|
|
)
|
|
.arg(
|
|
Arg::new("option")
|
|
.short('o')
|
|
.visible_short_alias('O')
|
|
.long("option")
|
|
.visible_alias("opt")
|
|
.help("cmd option")
|
|
.takes_value(true),
|
|
)
|
|
.arg(Arg::new("positional"))
|
|
}
|
|
|
|
static BASH_ALIASES: &str = r#"_cmd() {
|
|
local i cur prev opts cmds
|
|
COMPREPLY=()
|
|
cur="${COMP_WORDS[COMP_CWORD]}"
|
|
prev="${COMP_WORDS[COMP_CWORD-1]}"
|
|
cmd=""
|
|
opts=""
|
|
|
|
for i in ${COMP_WORDS[@]}
|
|
do
|
|
case "${i}" in
|
|
"$1")
|
|
cmd="cmd"
|
|
;;
|
|
*)
|
|
;;
|
|
esac
|
|
done
|
|
|
|
case "${cmd}" in
|
|
cmd)
|
|
opts="-h -V -F -f -O -o --help --version --flg --flag --opt --option <positional>"
|
|
if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then
|
|
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
|
|
return 0
|
|
fi
|
|
case "${prev}" in
|
|
--option)
|
|
COMPREPLY=($(compgen -f "${cur}"))
|
|
return 0
|
|
;;
|
|
--opt)
|
|
COMPREPLY=($(compgen -f "${cur}"))
|
|
return 0
|
|
;;
|
|
-o)
|
|
COMPREPLY=($(compgen -f "${cur}"))
|
|
return 0
|
|
;;
|
|
-O)
|
|
COMPREPLY=($(compgen -f "${cur}"))
|
|
return 0
|
|
;;
|
|
*)
|
|
COMPREPLY=()
|
|
;;
|
|
esac
|
|
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
|
|
return 0
|
|
;;
|
|
esac
|
|
}
|
|
|
|
complete -F _cmd -o bashdefault -o default cmd
|
|
"#;
|