mirror of
https://github.com/clap-rs/clap
synced 2024-12-14 14:52:33 +00:00
[clap_generate] [zsh] sort out multiple occurrence vs multiple_value.
This commit is contained in:
parent
6d046c7aac
commit
25e337adb1
4 changed files with 97 additions and 15 deletions
|
@ -431,19 +431,23 @@ fn write_opts_of(p: &App, p_global: Option<&App>) -> String {
|
|||
let help = o.get_about().map_or(String::new(), escape_help);
|
||||
let conflicts = arg_conflicts(p, o, p_global);
|
||||
|
||||
// @TODO @soundness should probably be either multiple occurrences or multiple values and
|
||||
// not both
|
||||
let multiple = if o.is_set(ArgSettings::MultipleOccurrences)
|
||||
|| o.is_set(ArgSettings::MultipleValues)
|
||||
{
|
||||
let multiple = if o.is_set(ArgSettings::MultipleOccurrences) {
|
||||
"*"
|
||||
} else {
|
||||
""
|
||||
};
|
||||
|
||||
let vn = match o.get_value_names() {
|
||||
None => " ".to_string(),
|
||||
Some(val) => val[0].to_string(),
|
||||
};
|
||||
let vc = match value_completion(o) {
|
||||
Some(val) => format!(": :{}", val),
|
||||
None => "".to_string(),
|
||||
Some(val) => format!(":{}:{}", vn, val),
|
||||
None => format!(":{}: ", vn),
|
||||
};
|
||||
let vc = match o.get_num_vals() {
|
||||
Some(num_vals) => vc.repeat(num_vals),
|
||||
None => vc,
|
||||
};
|
||||
|
||||
if let Some(shorts) = o.get_short_and_visible_aliases() {
|
||||
|
|
|
@ -62,7 +62,7 @@ _myapp() {
|
|||
case $line[2] in
|
||||
(test)
|
||||
_arguments "${_arguments_options[@]}" \
|
||||
'--case=[the case to test]' \
|
||||
'--case=[the case to test]: : ' \
|
||||
'-h[Print help information]' \
|
||||
'--help[Print help information]' \
|
||||
'-V[Print version information]' \
|
||||
|
@ -159,7 +159,7 @@ _my_app() {
|
|||
case $line[2] in
|
||||
(test)
|
||||
_arguments "${_arguments_options[@]}" \
|
||||
'--case=[the case to test]' \
|
||||
'--case=[the case to test]: : ' \
|
||||
'-h[Print help information]' \
|
||||
'--help[Print help information]' \
|
||||
'-V[Print version information]' \
|
||||
|
@ -168,7 +168,7 @@ _arguments "${_arguments_options[@]}" \
|
|||
;;
|
||||
(some_cmd)
|
||||
_arguments "${_arguments_options[@]}" \
|
||||
'--config=[the other case to test]' \
|
||||
'--config=[the other case to test]: : ' \
|
||||
'-h[Print help information]' \
|
||||
'--help[Print help information]' \
|
||||
'-V[Print version information]' \
|
||||
|
@ -474,10 +474,10 @@ _cmd() {
|
|||
|
||||
local context curcontext="$curcontext" state line
|
||||
_arguments "${_arguments_options[@]}" \
|
||||
'-o+[cmd option]' \
|
||||
'-O+[cmd option]' \
|
||||
'--option=[cmd option]' \
|
||||
'--opt=[cmd option]' \
|
||||
'-o+[cmd option]: : ' \
|
||||
'-O+[cmd option]: : ' \
|
||||
'--option=[cmd option]: : ' \
|
||||
'--opt=[cmd option]: : ' \
|
||||
'-h[Print help information]' \
|
||||
'--help[Print help information]' \
|
||||
'-V[Print version information]' \
|
||||
|
@ -497,3 +497,65 @@ _cmd_commands() {
|
|||
}
|
||||
|
||||
_cmd "$@""#;
|
||||
|
||||
#[test]
|
||||
fn zsh_with_files_and_dirs() {
|
||||
let mut app = build_app_with_files_and_dirs();
|
||||
common(Zsh, &mut app, "my_app", ZSH_PATHS);
|
||||
}
|
||||
|
||||
fn build_app_with_files_and_dirs() -> App<'static> {
|
||||
App::new("my_app")
|
||||
.version("3.0")
|
||||
.about("testing zsh completions")
|
||||
.arg(
|
||||
Arg::new("directory")
|
||||
.long("dir")
|
||||
.about("specify a directory")
|
||||
.value_name("DIR")
|
||||
.number_of_values(3)
|
||||
.value_hint(ValueHint::DirPath),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("file")
|
||||
.long("inputfiles")
|
||||
.value_name("FILE")
|
||||
.multiple_occurrences(true)
|
||||
.about("specify a file")
|
||||
.value_hint(ValueHint::FilePath),
|
||||
)
|
||||
}
|
||||
|
||||
static ZSH_PATHS: &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[@]}" \
|
||||
'--dir=[specify a directory]:DIR:_files -/:DIR:_files -/:DIR:_files -/' \
|
||||
'*--inputfiles=[specify a file]:FILE:_files' \
|
||||
'-h[Print help information]' \
|
||||
'--help[Print help information]' \
|
||||
'-V[Print version information]' \
|
||||
'--version[Print version information]' \
|
||||
&& ret=0
|
||||
}
|
||||
|
||||
(( $+functions[_my_app_commands] )) ||
|
||||
_my_app_commands() {
|
||||
local commands; commands=()
|
||||
_describe -t commands 'my_app commands' commands "$@"
|
||||
}
|
||||
|
||||
_my_app "$@""#;
|
||||
|
|
|
@ -97,7 +97,7 @@ _my_app() {
|
|||
local context curcontext="$curcontext" state line
|
||||
_arguments "${_arguments_options[@]}" \
|
||||
'--choice=[]: :(bash fish zsh)' \
|
||||
'--unknown=[]' \
|
||||
'--unknown=[]: : ' \
|
||||
'--other=[]: :( )' \
|
||||
'-p+[]: :_files' \
|
||||
'--path=[]: :_files' \
|
||||
|
|
|
@ -239,6 +239,22 @@ impl<'help> Arg<'help> {
|
|||
}
|
||||
}
|
||||
|
||||
/// Get the names of values for this argument.
|
||||
#[inline]
|
||||
pub fn get_value_names(&self) -> Option<&[&str]> {
|
||||
if self.val_names.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(&self.val_names)
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the number of values for this argument.
|
||||
#[inline]
|
||||
pub fn get_num_vals(&self) -> Option<usize> {
|
||||
self.num_vals
|
||||
}
|
||||
|
||||
/// Get the index of this argument, if any
|
||||
#[inline]
|
||||
pub fn get_index(&self) -> Option<usize> {
|
||||
|
|
Loading…
Reference in a new issue