2020-04-07 23:34:53 +00:00
|
|
|
use clap::{App, AppSettings, Arg, ValueHint};
|
|
|
|
use clap_generate::generators::*;
|
|
|
|
use completions::common;
|
|
|
|
|
|
|
|
mod completions;
|
|
|
|
|
|
|
|
pub fn build_app_with_value_hints() -> App<'static> {
|
|
|
|
App::new("my_app")
|
2020-10-10 14:53:06 +00:00
|
|
|
.setting(AppSettings::DisableVersionFlag)
|
2020-04-07 23:34:53 +00:00
|
|
|
.setting(AppSettings::TrailingVarArg)
|
|
|
|
.arg(
|
|
|
|
Arg::new("choice")
|
|
|
|
.long("choice")
|
2021-09-19 10:29:09 +00:00
|
|
|
.possible_values(["bash", "fish", "zsh"]),
|
2020-04-07 23:34:53 +00:00
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::new("unknown")
|
|
|
|
.long("unknown")
|
|
|
|
.value_hint(ValueHint::Unknown),
|
|
|
|
)
|
|
|
|
.arg(Arg::new("other").long("other").value_hint(ValueHint::Other))
|
|
|
|
.arg(
|
|
|
|
Arg::new("path")
|
|
|
|
.long("path")
|
|
|
|
.short('p')
|
|
|
|
.value_hint(ValueHint::AnyPath),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::new("file")
|
|
|
|
.long("file")
|
|
|
|
.short('f')
|
|
|
|
.value_hint(ValueHint::FilePath),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::new("dir")
|
|
|
|
.long("dir")
|
|
|
|
.short('d')
|
|
|
|
.value_hint(ValueHint::DirPath),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::new("exe")
|
|
|
|
.long("exe")
|
|
|
|
.short('e')
|
|
|
|
.value_hint(ValueHint::ExecutablePath),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::new("cmd_name")
|
|
|
|
.long("cmd-name")
|
|
|
|
.value_hint(ValueHint::CommandName),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::new("cmd")
|
|
|
|
.long("cmd")
|
|
|
|
.short('c')
|
|
|
|
.value_hint(ValueHint::CommandString),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::new("command_with_args")
|
2021-02-24 15:07:57 +00:00
|
|
|
.takes_value(true)
|
2020-04-07 23:34:53 +00:00
|
|
|
.multiple_values(true)
|
|
|
|
.value_hint(ValueHint::CommandWithArguments),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::new("user")
|
|
|
|
.short('u')
|
|
|
|
.long("user")
|
|
|
|
.value_hint(ValueHint::Username),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::new("host")
|
|
|
|
.short('h')
|
|
|
|
.long("host")
|
|
|
|
.value_hint(ValueHint::Hostname),
|
|
|
|
)
|
|
|
|
.arg(Arg::new("url").long("url").value_hint(ValueHint::Url))
|
|
|
|
.arg(
|
|
|
|
Arg::new("email")
|
|
|
|
.long("email")
|
|
|
|
.value_hint(ValueHint::EmailAddress),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
static ZSH_VALUE_HINTS: &str = r#"#compdef my_app
|
|
|
|
|
|
|
|
autoload -U is-at-least
|
|
|
|
|
|
|
|
_my_app() {
|
|
|
|
typeset -A opt_args
|
|
|
|
typeset -a _arguments_options
|
|
|
|
local ret=1
|
|
|
|
|
|
|
|
if is-at-least 5.2; then
|
|
|
|
_arguments_options=(-s -S -C)
|
|
|
|
else
|
|
|
|
_arguments_options=(-s -C)
|
|
|
|
fi
|
|
|
|
|
|
|
|
local context curcontext="$curcontext" state line
|
|
|
|
_arguments "${_arguments_options[@]}" \
|
|
|
|
'--choice=[]: :(bash fish zsh)' \
|
|
|
|
'--unknown=[]' \
|
|
|
|
'--other=[]: :( )' \
|
|
|
|
'-p+[]: :_files' \
|
|
|
|
'--path=[]: :_files' \
|
|
|
|
'-f+[]: :_files' \
|
|
|
|
'--file=[]: :_files' \
|
|
|
|
'-d+[]: :_files -/' \
|
|
|
|
'--dir=[]: :_files -/' \
|
|
|
|
'-e+[]: :_absolute_command_paths' \
|
|
|
|
'--exe=[]: :_absolute_command_paths' \
|
|
|
|
'--cmd-name=[]: :_command_names -e' \
|
|
|
|
'-c+[]: :_cmdstring' \
|
|
|
|
'--cmd=[]: :_cmdstring' \
|
|
|
|
'-u+[]: :_users' \
|
|
|
|
'--user=[]: :_users' \
|
|
|
|
'-h+[]: :_hosts' \
|
|
|
|
'--host=[]: :_hosts' \
|
|
|
|
'--url=[]: :_urls' \
|
|
|
|
'--email=[]: :_email_addresses' \
|
2021-07-30 03:23:25 +00:00
|
|
|
'--help[Print help information]' \
|
2020-04-07 23:34:53 +00:00
|
|
|
'*::command_with_args:_cmdambivalent' \
|
|
|
|
&& ret=0
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
(( $+functions[_my_app_commands] )) ||
|
|
|
|
_my_app_commands() {
|
2020-12-28 00:02:30 +00:00
|
|
|
local commands; commands=()
|
2020-04-07 23:34:53 +00:00
|
|
|
_describe -t commands 'my_app commands' commands "$@"
|
|
|
|
}
|
|
|
|
|
|
|
|
_my_app "$@""#;
|
|
|
|
|
2021-08-20 22:53:10 +00:00
|
|
|
static FISH_VALUE_HINTS: &str = r#"complete -c my_app -l choice -r -f -a "{bash ,fish ,zsh }"
|
2021-02-22 14:35:07 +00:00
|
|
|
complete -c my_app -l unknown -r
|
|
|
|
complete -c my_app -l other -r -f
|
|
|
|
complete -c my_app -s p -l path -r -F
|
|
|
|
complete -c my_app -s f -l file -r -F
|
|
|
|
complete -c my_app -s d -l dir -r -f -a "(__fish_complete_directories)"
|
|
|
|
complete -c my_app -s e -l exe -r -F
|
|
|
|
complete -c my_app -l cmd-name -r -f -a "(__fish_complete_command)"
|
|
|
|
complete -c my_app -s c -l cmd -r -f -a "(__fish_complete_command)"
|
|
|
|
complete -c my_app -s u -l user -r -f -a "(__fish_complete_users)"
|
|
|
|
complete -c my_app -s h -l host -r -f -a "(__fish_print_hostnames)"
|
|
|
|
complete -c my_app -l url -r -f
|
|
|
|
complete -c my_app -l email -r -f
|
2021-07-30 03:23:25 +00:00
|
|
|
complete -c my_app -l help -d 'Print help information'
|
2020-04-07 23:34:53 +00:00
|
|
|
"#;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn zsh_with_value_hints() {
|
|
|
|
let mut app = build_app_with_value_hints();
|
|
|
|
common::<Zsh>(&mut app, "my_app", ZSH_VALUE_HINTS);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn fish_with_value_hints() {
|
|
|
|
let mut app = build_app_with_value_hints();
|
|
|
|
common::<Fish>(&mut app, "my_app", FISH_VALUE_HINTS);
|
|
|
|
}
|