2020-12-27 19:47:30 +00:00
|
|
|
use super::*;
|
|
|
|
|
2020-12-27 22:50:20 +00:00
|
|
|
fn build_app() -> App<'static> {
|
|
|
|
build_app_with_name("myapp")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn build_app_with_name(s: &'static str) -> App<'static> {
|
|
|
|
App::new(s)
|
2021-10-08 17:05:40 +00:00
|
|
|
.version("3.0")
|
2021-10-12 18:59:56 +00:00
|
|
|
.setting(AppSettings::PropagateVersion)
|
2020-12-27 22:50:20 +00:00
|
|
|
.about("Tests completions")
|
|
|
|
.arg(
|
|
|
|
Arg::new("file")
|
|
|
|
.value_hint(ValueHint::FilePath)
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("some input file"),
|
2020-12-27 22:50:20 +00:00
|
|
|
)
|
2021-10-14 18:41:11 +00:00
|
|
|
.arg(Arg::new("choice").possible_values(["first", "second"]))
|
2020-12-27 22:50:20 +00:00
|
|
|
.subcommand(
|
|
|
|
App::new("test").about("tests things").arg(
|
|
|
|
Arg::new("case")
|
|
|
|
.long("case")
|
|
|
|
.takes_value(true)
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("the case to test"),
|
2020-12-27 22:50:20 +00:00
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-12-27 19:47:30 +00:00
|
|
|
#[test]
|
|
|
|
fn bash() {
|
|
|
|
let mut app = build_app();
|
2021-08-18 18:25:43 +00:00
|
|
|
common(Bash, &mut app, "myapp", BASH);
|
2020-12-27 19:47:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2021-11-28 17:15:08 +00:00
|
|
|
"$1")
|
2020-12-27 19:47:30 +00:00
|
|
|
cmd="myapp"
|
|
|
|
;;
|
|
|
|
help)
|
|
|
|
cmd+="__help"
|
|
|
|
;;
|
|
|
|
test)
|
|
|
|
cmd+="__test"
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
case "${cmd}" in
|
|
|
|
myapp)
|
2021-10-14 21:51:40 +00:00
|
|
|
opts="-h -V --help --version <file> first second test help"
|
2020-12-27 19:47:30 +00:00
|
|
|
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)
|
2021-12-13 21:59:12 +00:00
|
|
|
opts=""
|
2020-12-27 19:47:30 +00:00
|
|
|
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)
|
2021-10-14 21:51:40 +00:00
|
|
|
opts="-h -V --case --help --version"
|
2020-12-27 19:47:30 +00:00
|
|
|
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();
|
2021-08-18 18:25:43 +00:00
|
|
|
common(Bash, &mut app, "my_app", BASH_SPECIAL_CMDS);
|
2020-12-27 19:47:30 +00:00
|
|
|
}
|
|
|
|
|
2020-12-27 22:50:20 +00:00
|
|
|
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)
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("the other case to test"),
|
2020-12-27 22:50:20 +00:00
|
|
|
),
|
|
|
|
)
|
2021-10-19 01:38:22 +00:00
|
|
|
.subcommand(App::new("some-cmd-with-hyphens").alias("hyphen"))
|
2020-12-27 22:50:20 +00:00
|
|
|
}
|
|
|
|
|
2020-12-27 19:47:30 +00:00
|
|
|
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
|
2021-11-28 17:15:08 +00:00
|
|
|
"$1")
|
2020-12-27 19:47:30 +00:00
|
|
|
cmd="my_app"
|
|
|
|
;;
|
|
|
|
help)
|
|
|
|
cmd+="__help"
|
|
|
|
;;
|
2021-10-19 01:38:22 +00:00
|
|
|
some-cmd-with-hyphens)
|
|
|
|
cmd+="__some__cmd__with__hyphens"
|
2020-12-27 19:47:30 +00:00
|
|
|
;;
|
|
|
|
some_cmd)
|
|
|
|
cmd+="__some_cmd"
|
|
|
|
;;
|
|
|
|
test)
|
|
|
|
cmd+="__test"
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
case "${cmd}" in
|
|
|
|
my_app)
|
2021-10-19 01:38:22 +00:00
|
|
|
opts="-h -V --help --version <file> first second test some_cmd some-cmd-with-hyphens help"
|
2020-12-27 19:47:30 +00:00
|
|
|
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)
|
2021-12-13 21:59:12 +00:00
|
|
|
opts=""
|
2020-12-27 19:47:30 +00:00
|
|
|
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
|
|
|
|
;;
|
2021-10-19 01:38:22 +00:00
|
|
|
my_app__some__cmd__with__hyphens)
|
2021-10-14 21:51:40 +00:00
|
|
|
opts="-h -V --help --version"
|
2020-12-27 19:47:30 +00:00
|
|
|
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)
|
2021-10-14 21:51:40 +00:00
|
|
|
opts="-h -V --config --help --version"
|
2020-12-27 19:47:30 +00:00
|
|
|
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)
|
2021-10-14 21:51:40 +00:00
|
|
|
opts="-h -V --case --help --version"
|
2020-12-27 19:47:30 +00:00
|
|
|
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
|
|
|
|
"#;
|
2021-02-15 08:35:25 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn bash_with_aliases() {
|
|
|
|
let mut app = build_app_with_aliases();
|
2021-08-18 18:25:43 +00:00
|
|
|
common(Bash, &mut app, "cmd", BASH_ALIASES);
|
2021-02-15 08:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn build_app_with_aliases() -> App<'static> {
|
|
|
|
App::new("cmd")
|
2021-10-08 17:05:40 +00:00
|
|
|
.version("3.0")
|
2021-02-15 08:35:25 +00:00
|
|
|
.about("testing bash completions")
|
|
|
|
.arg(
|
|
|
|
Arg::new("flag")
|
|
|
|
.short('f')
|
|
|
|
.visible_short_alias('F')
|
|
|
|
.long("flag")
|
|
|
|
.visible_alias("flg")
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("cmd flag"),
|
2021-02-15 08:35:25 +00:00
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::new("option")
|
|
|
|
.short('o')
|
|
|
|
.visible_short_alias('O')
|
|
|
|
.long("option")
|
|
|
|
.visible_alias("opt")
|
2021-11-18 16:17:15 +00:00
|
|
|
.help("cmd option")
|
2021-02-15 08:35:25 +00:00
|
|
|
.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
|
2021-11-28 17:15:08 +00:00
|
|
|
"$1")
|
2021-02-15 08:35:25 +00:00
|
|
|
cmd="cmd"
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
case "${cmd}" in
|
|
|
|
cmd)
|
2021-10-14 21:51:40 +00:00
|
|
|
opts="-h -V -F -f -O -o --help --version --flg --flag --opt --option <positional>"
|
2021-02-15 08:35:25 +00:00
|
|
|
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
|
|
|
|
;;
|
2021-02-22 08:34:18 +00:00
|
|
|
--opt)
|
|
|
|
COMPREPLY=($(compgen -f "${cur}"))
|
|
|
|
return 0
|
|
|
|
;;
|
|
|
|
-o)
|
|
|
|
COMPREPLY=($(compgen -f "${cur}"))
|
|
|
|
return 0
|
|
|
|
;;
|
|
|
|
-O)
|
2021-02-15 08:35:25 +00:00
|
|
|
COMPREPLY=($(compgen -f "${cur}"))
|
|
|
|
return 0
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
COMPREPLY=()
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
|
|
|
|
return 0
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
}
|
|
|
|
|
|
|
|
complete -F _cmd -o bashdefault -o default cmd
|
|
|
|
"#;
|