diff --git a/clap_generate/src/generators/shells/bash.rs b/clap_generate/src/generators/shells/bash.rs index fc9471ea..bb428b50 100644 --- a/clap_generate/src/generators/shells/bash.rs +++ b/clap_generate/src/generators/shells/bash.rs @@ -147,30 +147,34 @@ fn option_details_for_path(app: &App, path: &str) -> String { let mut opts = String::new(); for o in p.get_opts() { - if let Some(l) = o.get_long() { - opts = format!( - "{} + if let Some(longs) = o.get_long_and_visible_aliases() { + for long in longs { + opts = format!( + "{} --{}) COMPREPLY=({}) return 0 ;;", - opts, - l, - vals_for(o) - ); + opts, + long, + vals_for(o) + ); + } } - if let Some(s) = o.get_short() { - opts = format!( - "{} - -{}) + if let Some(shorts) = o.get_short_and_visible_aliases() { + for short in shorts { + opts = format!( + "{} + -{}) COMPREPLY=({}) return 0 ;;", - opts, - s, - vals_for(o) - ); + opts, + short, + vals_for(o) + ); + } } } diff --git a/clap_generate/src/generators/shells/elvish.rs b/clap_generate/src/generators/shells/elvish.rs index 9668c2cc..5d282a29 100644 --- a/clap_generate/src/generators/shells/elvish.rs +++ b/clap_generate/src/generators/shells/elvish.rs @@ -78,61 +78,37 @@ fn generate_inner<'help>( let preamble = String::from("\n cand "); for option in p.get_opts() { - if let Some(data) = option.get_short() { - let tooltip = get_tooltip(option.get_about(), data); - - completions.push_str(&preamble); - completions.push_str(format!("-{} '{}'", data, tooltip).as_str()); - - if let Some(short_aliases) = option.get_visible_short_aliases() { - for data in short_aliases { - completions.push_str(&preamble); - completions.push_str(format!("-{} '{}'", data, tooltip).as_str()); - } + if let Some(shorts) = option.get_short_and_visible_aliases() { + let tooltip = get_tooltip(option.get_about(), shorts[0]); + for short in shorts { + completions.push_str(&preamble); + completions.push_str(format!("-{} '{}'", short, tooltip).as_str()); } } - if let Some(data) = option.get_long() { - let tooltip = get_tooltip(option.get_about(), data); - - completions.push_str(&preamble); - completions.push_str(format!("--{} '{}'", data, tooltip).as_str()); - - if let Some(aliases) = option.get_visible_aliases() { - for data in aliases { - completions.push_str(&preamble); - completions.push_str(format!("--{} '{}'", data, tooltip).as_str()); - } + if let Some(longs) = option.get_long_and_visible_aliases() { + let tooltip = get_tooltip(option.get_about(), longs[0]); + for long in longs { + completions.push_str(&preamble); + completions.push_str(format!("--{} '{}'", long, tooltip).as_str()); } } } for flag in Elvish::flags(p) { - if let Some(data) = flag.get_short() { - let tooltip = get_tooltip(flag.get_about(), data); - - completions.push_str(&preamble); - completions.push_str(format!("-{} '{}'", data, tooltip).as_str()); - - if let Some(short_aliases) = flag.get_visible_short_aliases() { - for data in short_aliases { - completions.push_str(&preamble); - completions.push_str(format!("-{} '{}'", data, tooltip).as_str()); - } + if let Some(shorts) = flag.get_short_and_visible_aliases() { + let tooltip = get_tooltip(flag.get_about(), shorts[0]); + for short in shorts { + completions.push_str(&preamble); + completions.push_str(format!("-{} '{}'", short, tooltip).as_str()); } } - if let Some(data) = flag.get_long() { - let tooltip = get_tooltip(flag.get_about(), data); - - completions.push_str(&preamble); - completions.push_str(format!("--{} '{}'", data, tooltip).as_str()); - - if let Some(aliases) = flag.get_visible_aliases() { - for data in aliases { - completions.push_str(&preamble); - completions.push_str(format!("--{} '{}'", data, tooltip).as_str()); - } + if let Some(longs) = flag.get_long_and_visible_aliases() { + let tooltip = get_tooltip(flag.get_about(), longs[0]); + for long in longs { + completions.push_str(&preamble); + completions.push_str(format!("--{} '{}'", long, tooltip).as_str()); } } } diff --git a/clap_generate/src/generators/shells/fish.rs b/clap_generate/src/generators/shells/fish.rs index 29284d0b..859212bc 100644 --- a/clap_generate/src/generators/shells/fish.rs +++ b/clap_generate/src/generators/shells/fish.rs @@ -59,23 +59,15 @@ fn gen_fish_inner(root_command: &str, app: &App, buffer: &mut String) { for option in app.get_opts() { let mut template = basic_template.clone(); - if let Some(data) = option.get_short() { - template.push_str(format!(" -s {}", data).as_str()); - - if let Some(short_aliases) = option.get_visible_short_aliases() { - for data in short_aliases { - template.push_str(format!(" -s {}", data).as_str()); - } + if let Some(shorts) = option.get_short_and_visible_aliases() { + for short in shorts { + template.push_str(format!(" -s {}", short).as_str()); } } - if let Some(data) = option.get_long() { - template.push_str(format!(" -l {}", data).as_str()); - - if let Some(aliases) = option.get_visible_aliases() { - for data in aliases { - template.push_str(format!(" -l {}", data).as_str()); - } + if let Some(longs) = option.get_long_and_visible_aliases() { + for long in longs { + template.push_str(format!(" -l {}", escape_string(long)).as_str()); } } @@ -92,23 +84,15 @@ fn gen_fish_inner(root_command: &str, app: &App, buffer: &mut String) { for flag in Fish::flags(app) { let mut template = basic_template.clone(); - if let Some(data) = flag.get_short() { - template.push_str(format!(" -s {}", data).as_str()); - - if let Some(short_aliases) = flag.get_visible_short_aliases() { - for data in short_aliases { - template.push_str(format!(" -s {}", data).as_str()); - } + if let Some(shorts) = flag.get_short_and_visible_aliases() { + for short in shorts { + template.push_str(format!(" -s {}", short).as_str()); } } - if let Some(data) = flag.get_long() { - template.push_str(format!(" -l {}", data).as_str()); - - if let Some(aliases) = flag.get_visible_aliases() { - for data in aliases { - template.push_str(format!(" -l {}", data).as_str()); - } + if let Some(longs) = flag.get_long_and_visible_aliases() { + for long in longs { + template.push_str(format!(" -l {}", escape_string(long)).as_str()); } } diff --git a/clap_generate/src/generators/shells/powershell.rs b/clap_generate/src/generators/shells/powershell.rs index 6bbcd028..3b750a7e 100644 --- a/clap_generate/src/generators/shells/powershell.rs +++ b/clap_generate/src/generators/shells/powershell.rs @@ -85,109 +85,61 @@ fn generate_inner<'help>( let preamble = String::from("\n [CompletionResult]::new("); for option in p.get_opts() { - if let Some(data) = option.get_short() { - let tooltip = get_tooltip(option.get_about(), data); - - completions.push_str(&preamble); - completions.push_str( - format!( - "'-{}', '{}', {}, '{}')", - data, data, "[CompletionResultType]::ParameterName", tooltip - ) - .as_str(), - ); - - if let Some(short_aliases) = option.get_visible_short_aliases() { - for data in short_aliases { - completions.push_str(&preamble); - completions.push_str( - format!( - "'-{}', '{}', {}, '{}')", - data, data, "[CompletionResultType]::ParameterName", tooltip - ) - .as_str(), - ); - } + if let Some(shorts) = option.get_short_and_visible_aliases() { + let tooltip = get_tooltip(option.get_about(), shorts[0]); + for short in shorts { + completions.push_str(&preamble); + completions.push_str( + format!( + "'-{}', '{}', {}, '{}')", + short, short, "[CompletionResultType]::ParameterName", tooltip + ) + .as_str(), + ); } } - if let Some(data) = option.get_long() { - let tooltip = get_tooltip(option.get_about(), data); - - completions.push_str(&preamble); - completions.push_str( - format!( - "'--{}', '{}', {}, '{}')", - data, data, "[CompletionResultType]::ParameterName", tooltip - ) - .as_str(), - ); - - if let Some(aliases) = option.get_visible_aliases() { - for data in aliases { - completions.push_str(&preamble); - completions.push_str( - format!( - "'--{}', '{}', {}, '{}')", - data, data, "[CompletionResultType]::ParameterName", tooltip - ) - .as_str(), - ); - } + if let Some(longs) = option.get_long_and_visible_aliases() { + let tooltip = get_tooltip(option.get_about(), longs[0]); + for long in longs { + completions.push_str(&preamble); + completions.push_str( + format!( + "'--{}', '{}', {}, '{}')", + long, long, "[CompletionResultType]::ParameterName", tooltip + ) + .as_str(), + ); } } } for flag in PowerShell::flags(p) { - if let Some(data) = flag.get_short() { - let tooltip = get_tooltip(flag.get_about(), data); - - completions.push_str(&preamble); - completions.push_str( - format!( - "'-{}', '{}', {}, '{}')", - data, data, "[CompletionResultType]::ParameterName", tooltip - ) - .as_str(), - ); - - if let Some(short_aliases) = flag.get_visible_short_aliases() { - for data in short_aliases { - completions.push_str(&preamble); - completions.push_str( - format!( - "'-{}', '{}', {}, '{}')", - data, data, "[CompletionResultType]::ParameterName", tooltip - ) - .as_str(), - ); - } + if let Some(shorts) = flag.get_short_and_visible_aliases() { + let tooltip = get_tooltip(flag.get_about(), shorts[0]); + for short in shorts { + completions.push_str(&preamble); + completions.push_str( + format!( + "'-{}', '{}', {}, '{}')", + short, short, "[CompletionResultType]::ParameterName", tooltip + ) + .as_str(), + ); } } - if let Some(data) = flag.get_long() { - let tooltip = get_tooltip(flag.get_about(), data); - - completions.push_str(&preamble); - completions.push_str( - format!( - "'--{}', '{}', {}, '{}')", - data, data, "[CompletionResultType]::ParameterName", tooltip - ) - .as_str(), - ); - - if let Some(aliases) = flag.get_visible_aliases() { - for data in aliases { - completions.push_str(&preamble); - completions.push_str( - format!( - "'--{}', '{}', {}, '{}')", - data, data, "[CompletionResultType]::ParameterName", tooltip - ) - .as_str(), - ); - } + if let Some(longs) = flag.get_long_and_visible_aliases() { + let tooltip = get_tooltip(flag.get_about(), longs[0]); + for long in longs { + completions.push_str(&preamble); + completions.push_str( + format!( + "'--{}', '{}', {}, '{}')", + long, long, "[CompletionResultType]::ParameterName", tooltip + ) + .as_str(), + ); } } } diff --git a/clap_generate/src/generators/shells/zsh.rs b/clap_generate/src/generators/shells/zsh.rs index 8a4cd6a9..77e3af69 100644 --- a/clap_generate/src/generators/shells/zsh.rs +++ b/clap_generate/src/generators/shells/zsh.rs @@ -422,63 +422,34 @@ fn write_opts_of(p: &App, p_global: Option<&App>) -> String { None => "".to_string(), }; - if let Some(short) = o.get_short() { - let s = format!( - "'{conflicts}{multiple}-{arg}+[{help}]{value_completion}' \\", - conflicts = conflicts, - multiple = multiple, - arg = short, - value_completion = vc, - help = help - ); + if let Some(shorts) = o.get_short_and_visible_aliases() { + for short in shorts { + let s = format!( + "'{conflicts}{multiple}-{arg}+[{help}]{value_completion}' \\", + conflicts = conflicts, + multiple = multiple, + arg = short, + value_completion = vc, + help = help + ); - debug!("write_opts_of:iter: Wrote...{}", &*s); - ret.push(s); - - if let Some(short_aliases) = o.get_visible_short_aliases() { - for alias in short_aliases { - let s = format!( - "'{conflicts}{multiple}-{arg}+[{help}]{value_completion}' \\", - conflicts = conflicts, - multiple = multiple, - arg = alias, - value_completion = vc, - help = help - ); - - debug!("write_opts_of:iter: Wrote...{}", &*s); - ret.push(s); - } + debug!("write_opts_of:iter: Wrote...{}", &*s); + ret.push(s); } } + if let Some(longs) = o.get_long_and_visible_aliases() { + for long in longs { + let l = format!( + "'{conflicts}{multiple}--{arg}=[{help}]{value_completion}' \\", + conflicts = conflicts, + multiple = multiple, + arg = long, + value_completion = vc, + help = help + ); - if let Some(long) = o.get_long() { - let l = format!( - "'{conflicts}{multiple}--{arg}=[{help}]{value_completion}' \\", - conflicts = conflicts, - multiple = multiple, - arg = long, - value_completion = vc, - help = help - ); - - debug!("write_opts_of:iter: Wrote...{}", &*l); - ret.push(l); - - if let Some(aliases) = o.get_visible_aliases() { - for alias in aliases { - let l = format!( - "'{conflicts}{multiple}--{arg}=[{help}]{value_completion}' \\", - conflicts = conflicts, - multiple = multiple, - arg = alias, - value_completion = vc, - help = help - ); - - debug!("write_opts_of:iter: Wrote...{}", &*l); - ret.push(l); - } + debug!("write_opts_of:iter: Wrote...{}", &*l); + ret.push(l); } } } diff --git a/clap_generate/tests/completions/bash.rs b/clap_generate/tests/completions/bash.rs index 2cb23c6d..29db63a5 100644 --- a/clap_generate/tests/completions/bash.rs +++ b/clap_generate/tests/completions/bash.rs @@ -315,7 +315,15 @@ static BASH_ALIASES: &str = r#"_cmd() { COMPREPLY=($(compgen -f "${cur}")) return 0 ;; - -o) + --opt) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + -o) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + -O) COMPREPLY=($(compgen -f "${cur}")) return 0 ;; diff --git a/src/build/arg/mod.rs b/src/build/arg/mod.rs index fc27442c..5c7067c5 100644 --- a/src/build/arg/mod.rs +++ b/src/build/arg/mod.rs @@ -160,6 +160,19 @@ impl<'help> Arg<'help> { } } + /// Get the short option name and its visible aliases, if any + #[inline] + pub fn get_short_and_visible_aliases(&self) -> Option> { + let mut shorts = match self.short { + Some(short) => vec![short], + None => return None, + }; + if let Some(aliases) = self.get_visible_short_aliases() { + shorts.extend(aliases); + } + Some(shorts) + } + /// Get the long option name for this argument, if any #[inline] pub fn get_long(&self) -> Option<&str> { @@ -182,6 +195,19 @@ impl<'help> Arg<'help> { } } + /// Get the long option name and its visible aliases, if any + #[inline] + pub fn get_long_and_visible_aliases(&self) -> Option> { + let mut longs = match self.long { + Some(long) => vec![long], + None => return None, + }; + if let Some(aliases) = self.get_visible_aliases() { + longs.extend(aliases); + } + Some(longs) + } + /// Get the list of the possible values for this argument, if any #[inline] pub fn get_possible_values(&self) -> Option<&[&str]> {