clap/clap_complete/tests/value_hints.rs
Ed Page 272f840178 feat: Replace core set of AppSettings with functions
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.
2022-02-11 12:35:09 -06:00

160 lines
4.3 KiB
Rust

mod completions;
use clap::{App, Arg, ValueHint};
use clap_complete::shells::*;
use completions::common;
pub fn build_app_with_value_hints() -> App<'static> {
App::new("my_app")
.trailing_var_arg(true)
.arg(
Arg::new("choice")
.long("choice")
.possible_values(["bash", "fish", "zsh"]),
)
.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")
.takes_value(true)
.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' \
'--help[Print help information]' \
'*::command_with_args:_cmdambivalent' \
&& ret=0
}
(( $+functions[_my_app_commands] )) ||
_my_app_commands() {
local commands; commands=()
_describe -t commands 'my_app commands' commands "$@"
}
_my_app "$@"
"#;
static FISH_VALUE_HINTS: &str = r#"complete -c my_app -l choice -r -f -a "{bash ,fish ,zsh }"
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
complete -c my_app -l help -d 'Print help information'
"#;
#[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);
}