Skip to content

Commit

Permalink
Merge pull request #4174 from ModProg/4153-value_help_comma
Browse files Browse the repository at this point in the history
value help comma
  • Loading branch information
epage committed Sep 2, 2022
2 parents 5e69f92 + 8ab649e commit cdb83ae
Show file tree
Hide file tree
Showing 16 changed files with 297 additions and 284 deletions.
25 changes: 16 additions & 9 deletions clap_complete/src/shells/fish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,13 @@ impl Generator for Fish {
}

// Escape string inside single quotes
fn escape_string(string: &str) -> String {
string.replace('\\', "\\\\").replace('\'', "\\'")
fn escape_string(string: &str, escape_comma: bool) -> String {
let string = string.replace('\\', "\\\\").replace('\'', "\\'");
if escape_comma {
string.replace(',', "\\,")
} else {
string
}
}

fn gen_fish_inner(
Expand Down Expand Up @@ -88,12 +93,13 @@ fn gen_fish_inner(

if let Some(longs) = option.get_long_and_visible_aliases() {
for long in longs {
template.push_str(format!(" -l {}", escape_string(long)).as_str());
template.push_str(format!(" -l {}", escape_string(long, false)).as_str());
}
}

if let Some(data) = option.get_help() {
template.push_str(format!(" -d '{}'", escape_string(&data.to_string())).as_str());
template
.push_str(format!(" -d '{}'", escape_string(&data.to_string(), false)).as_str());
}

template.push_str(value_completion(option).as_str());
Expand All @@ -113,12 +119,13 @@ fn gen_fish_inner(

if let Some(longs) = flag.get_long_and_visible_aliases() {
for long in longs {
template.push_str(format!(" -l {}", escape_string(long)).as_str());
template.push_str(format!(" -l {}", escape_string(long, false)).as_str());
}
}

if let Some(data) = flag.get_help() {
template.push_str(format!(" -d '{}'", escape_string(&data.to_string())).as_str());
template
.push_str(format!(" -d '{}'", escape_string(&data.to_string(), false)).as_str());
}

buffer.push_str(template.as_str());
Expand All @@ -132,7 +139,7 @@ fn gen_fish_inner(
template.push_str(format!(" -a \"{}\"", &subcommand.get_name()).as_str());

if let Some(data) = subcommand.get_about() {
template.push_str(format!(" -d '{}'", escape_string(&data.to_string())).as_str())
template.push_str(format!(" -d '{}'", escape_string(&data.to_string(), false)).as_str())
}

buffer.push_str(template.as_str());
Expand Down Expand Up @@ -163,8 +170,8 @@ fn value_completion(option: &Arg) -> String {
} else {
Some(format!(
"{}\t{}",
escape_string(value.get_name()).as_str(),
escape_string(&value.get_help().unwrap_or_default().to_string())
escape_string(value.get_name(), true).as_str(),
escape_string(&value.get_help().unwrap_or_default().to_string(), true)
))
})
.collect::<Vec<_>>()
Expand Down
11 changes: 8 additions & 3 deletions clap_complete/tests/common.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use clap::builder::PossibleValue;

pub fn basic_command(name: &'static str) -> clap::Command {
clap::Command::new(name)
.arg(
Expand Down Expand Up @@ -154,9 +156,11 @@ pub fn sub_subcommands_command(name: &'static str) -> clap::Command {
clap::Arg::new("config")
.long("config")
.action(clap::ArgAction::Set)
.value_parser([clap::builder::PossibleValue::new(
"Lest quotes aren't escaped.",
)])
.value_parser([
PossibleValue::new("Lest quotes, aren't escaped.")
.help("help,with,comma"),
PossibleValue::new("Second to trigger display of options"),
])
.help("the other case to test"),
),
),
Expand Down Expand Up @@ -258,5 +262,6 @@ pub fn assert_matches_path(

snapbox::Assert::new()
.action_env("SNAPSHOTS")
.normalize_paths(false)
.matches_path(expected_path, buf);
}
28 changes: 14 additions & 14 deletions clap_complete/tests/snapshots/aliases.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,20 @@ _my-app() {
fi

local context curcontext="$curcontext" state line
_arguments "${_arguments_options[@]}" /
'*-o+[cmd option]: : ' /
'*-O+[cmd option]: : ' /
'*--option=[cmd option]: : ' /
'*--opt=[cmd option]: : ' /
'*-f[cmd flag]' /
'*-F[cmd flag]' /
'*--flag[cmd flag]' /
'*--flg[cmd flag]' /
'*-h[Print help information]' /
'*--help[Print help information]' /
'*-V[Print version information]' /
'*--version[Print version information]' /
'::positional:' /
_arguments "${_arguments_options[@]}" \
'*-o+[cmd option]: : ' \
'*-O+[cmd option]: : ' \
'*--option=[cmd option]: : ' \
'*--opt=[cmd option]: : ' \
'*-f[cmd flag]' \
'*-F[cmd flag]' \
'*--flag[cmd flag]' \
'*--flg[cmd flag]' \
'*-h[Print help information]' \
'*--help[Print help information]' \
'*-V[Print version information]' \
'*--version[Print version information]' \
'::positional:' \
&& ret=0
}

Expand Down
42 changes: 21 additions & 21 deletions clap_complete/tests/snapshots/basic.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ _my-app() {
fi

local context curcontext="$curcontext" state line
_arguments "${_arguments_options[@]}" /
'*-c[]' /
'(-c)*-v[]' /
'*-h[Print help information]' /
'*--help[Print help information]' /
":: :_my-app_commands" /
"*::: :->my-app" /
_arguments "${_arguments_options[@]}" \
'*-c[]' \
'(-c)*-v[]' \
'*-h[Print help information]' \
'*--help[Print help information]' \
":: :_my-app_commands" \
"*::: :->my-app" \
&& ret=0
case $state in
(my-app)
Expand All @@ -29,17 +29,17 @@ _my-app() {
curcontext="${curcontext%:*:*}:my-app-command-$line[1]:"
case $line[1] in
(test)
_arguments "${_arguments_options[@]}" /
'*-d[]' /
'*-c[]' /
'*-h[Print help information]' /
'*--help[Print help information]' /
_arguments "${_arguments_options[@]}" \
'*-d[]' \
'*-c[]' \
'*-h[Print help information]' \
'*--help[Print help information]' \
&& ret=0
;;
(help)
_arguments "${_arguments_options[@]}" /
":: :_my-app__help_commands" /
"*::: :->help" /
_arguments "${_arguments_options[@]}" \
":: :_my-app__help_commands" \
"*::: :->help" \
&& ret=0

case $state in
Expand All @@ -49,11 +49,11 @@ _arguments "${_arguments_options[@]}" /
curcontext="${curcontext%:*:*}:my-app-help-command-$line[1]:"
case $line[1] in
(test)
_arguments "${_arguments_options[@]}" /
_arguments "${_arguments_options[@]}" \
&& ret=0
;;
(help)
_arguments "${_arguments_options[@]}" /
_arguments "${_arguments_options[@]}" \
&& ret=0
;;
esac
Expand All @@ -68,16 +68,16 @@ esac
(( $+functions[_my-app_commands] )) ||
_my-app_commands() {
local commands; commands=(
'test:Subcommand' /
'help:Print this message or the help of the given subcommand(s)' /
'test:Subcommand' \
'help:Print this message or the help of the given subcommand(s)' \
)
_describe -t commands 'my-app commands' commands "$@"
}
(( $+functions[_my-app__help_commands] )) ||
_my-app__help_commands() {
local commands; commands=(
'test:Subcommand' /
'help:Print this message or the help of the given subcommand(s)' /
'test:Subcommand' \
'help:Print this message or the help of the given subcommand(s)' \
)
_describe -t commands 'my-app help commands' commands "$@"
}
Expand Down
56 changes: 28 additions & 28 deletions clap_complete/tests/snapshots/feature_sample.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@ _my-app() {
fi

local context curcontext="$curcontext" state line
_arguments "${_arguments_options[@]}" /
'*-c[some config file]' /
'*-C[some config file]' /
'*--config[some config file]' /
'*--conf[some config file]' /
'*-h[Print help information]' /
'*--help[Print help information]' /
'*-V[Print version information]' /
'*--version[Print version information]' /
'::file -- some input file:_files' /
'::choice:(first second)' /
":: :_my-app_commands" /
"*::: :->my-app" /
_arguments "${_arguments_options[@]}" \
'*-c[some config file]' \
'*-C[some config file]' \
'*--config[some config file]' \
'*--conf[some config file]' \
'*-h[Print help information]' \
'*--help[Print help information]' \
'*-V[Print version information]' \
'*--version[Print version information]' \
'::file -- some input file:_files' \
'::choice:(first second)' \
":: :_my-app_commands" \
"*::: :->my-app" \
&& ret=0
case $state in
(my-app)
Expand All @@ -35,18 +35,18 @@ _my-app() {
curcontext="${curcontext%:*:*}:my-app-command-$line[3]:"
case $line[3] in
(test)
_arguments "${_arguments_options[@]}" /
'*--case=[the case to test]: : ' /
'*-h[Print help information]' /
'*--help[Print help information]' /
'*-V[Print version information]' /
'*--version[Print version information]' /
_arguments "${_arguments_options[@]}" \
'*--case=[the case to test]: : ' \
'*-h[Print help information]' \
'*--help[Print help information]' \
'*-V[Print version information]' \
'*--version[Print version information]' \
&& ret=0
;;
(help)
_arguments "${_arguments_options[@]}" /
":: :_my-app__help_commands" /
"*::: :->help" /
_arguments "${_arguments_options[@]}" \
":: :_my-app__help_commands" \
"*::: :->help" \
&& ret=0

case $state in
Expand All @@ -56,11 +56,11 @@ _arguments "${_arguments_options[@]}" /
curcontext="${curcontext%:*:*}:my-app-help-command-$line[1]:"
case $line[1] in
(test)
_arguments "${_arguments_options[@]}" /
_arguments "${_arguments_options[@]}" \
&& ret=0
;;
(help)
_arguments "${_arguments_options[@]}" /
_arguments "${_arguments_options[@]}" \
&& ret=0
;;
esac
Expand All @@ -75,16 +75,16 @@ esac
(( $+functions[_my-app_commands] )) ||
_my-app_commands() {
local commands; commands=(
'test:tests things' /
'help:Print this message or the help of the given subcommand(s)' /
'test:tests things' \
'help:Print this message or the help of the given subcommand(s)' \
)
_describe -t commands 'my-app commands' commands "$@"
}
(( $+functions[_my-app__help_commands] )) ||
_my-app__help_commands() {
local commands; commands=(
'test:tests things' /
'help:Print this message or the help of the given subcommand(s)' /
'test:tests things' \
'help:Print this message or the help of the given subcommand(s)' \
)
_describe -t commands 'my-app help commands' commands "$@"
}
Expand Down
6 changes: 3 additions & 3 deletions clap_complete/tests/snapshots/quoting.elvish
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ set edit:completion:arg-completer[my-app] = {|@words|
cand --single-quotes 'Can be ''always'', ''auto'', or ''never'''
cand --double-quotes 'Can be "always", "auto", or "never"'
cand --backticks 'For more information see `echo test`'
cand --backslash 'Avoid ''/n'''
cand --backslash 'Avoid ''\n'''
cand --brackets 'List packages [filter]'
cand --expansions 'Execute the shell command with $SHELL'
cand -h 'Print help information'
Expand All @@ -31,7 +31,7 @@ set edit:completion:arg-completer[my-app] = {|@words|
cand cmd-single-quotes 'Can be ''always'', ''auto'', or ''never'''
cand cmd-double-quotes 'Can be "always", "auto", or "never"'
cand cmd-backticks 'For more information see `echo test`'
cand cmd-backslash 'Avoid ''/n'''
cand cmd-backslash 'Avoid ''\n'''
cand cmd-brackets 'List packages [filter]'
cand cmd-expansions 'Execute the shell command with $SHELL'
cand help 'Print this message or the help of the given subcommand(s)'
Expand Down Expand Up @@ -64,7 +64,7 @@ set edit:completion:arg-completer[my-app] = {|@words|
cand cmd-single-quotes 'Can be ''always'', ''auto'', or ''never'''
cand cmd-double-quotes 'Can be "always", "auto", or "never"'
cand cmd-backticks 'For more information see `echo test`'
cand cmd-backslash 'Avoid ''/n'''
cand cmd-backslash 'Avoid ''\n'''
cand cmd-brackets 'List packages [filter]'
cand cmd-expansions 'Execute the shell command with $SHELL'
cand help 'Print this message or the help of the given subcommand(s)'
Expand Down
12 changes: 6 additions & 6 deletions clap_complete/tests/snapshots/quoting.fish
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
complete -c my-app -n "__fish_use_subcommand" -l single-quotes -d 'Can be /'always/', /'auto/', or /'never/''
complete -c my-app -n "__fish_use_subcommand" -l single-quotes -d 'Can be \'always\', \'auto\', or \'never\''
complete -c my-app -n "__fish_use_subcommand" -l double-quotes -d 'Can be "always", "auto", or "never"'
complete -c my-app -n "__fish_use_subcommand" -l backticks -d 'For more information see `echo test`'
complete -c my-app -n "__fish_use_subcommand" -l backslash -d 'Avoid /'//n/''
complete -c my-app -n "__fish_use_subcommand" -l backslash -d 'Avoid \'\\n\''
complete -c my-app -n "__fish_use_subcommand" -l brackets -d 'List packages [filter]'
complete -c my-app -n "__fish_use_subcommand" -l expansions -d 'Execute the shell command with $SHELL'
complete -c my-app -n "__fish_use_subcommand" -s h -l help -d 'Print help information'
complete -c my-app -n "__fish_use_subcommand" -s V -l version -d 'Print version information'
complete -c my-app -n "__fish_use_subcommand" -f -a "cmd-single-quotes" -d 'Can be /'always/', /'auto/', or /'never/''
complete -c my-app -n "__fish_use_subcommand" -f -a "cmd-single-quotes" -d 'Can be \'always\', \'auto\', or \'never\''
complete -c my-app -n "__fish_use_subcommand" -f -a "cmd-double-quotes" -d 'Can be "always", "auto", or "never"'
complete -c my-app -n "__fish_use_subcommand" -f -a "cmd-backticks" -d 'For more information see `echo test`'
complete -c my-app -n "__fish_use_subcommand" -f -a "cmd-backslash" -d 'Avoid /'//n/''
complete -c my-app -n "__fish_use_subcommand" -f -a "cmd-backslash" -d 'Avoid \'\\n\''
complete -c my-app -n "__fish_use_subcommand" -f -a "cmd-brackets" -d 'List packages [filter]'
complete -c my-app -n "__fish_use_subcommand" -f -a "cmd-expansions" -d 'Execute the shell command with $SHELL'
complete -c my-app -n "__fish_use_subcommand" -f -a "help" -d 'Print this message or the help of the given subcommand(s)'
Expand All @@ -19,10 +19,10 @@ complete -c my-app -n "__fish_seen_subcommand_from cmd-backticks" -s h -l help -
complete -c my-app -n "__fish_seen_subcommand_from cmd-backslash" -s h -l help -d 'Print help information'
complete -c my-app -n "__fish_seen_subcommand_from cmd-brackets" -s h -l help -d 'Print help information'
complete -c my-app -n "__fish_seen_subcommand_from cmd-expansions" -s h -l help -d 'Print help information'
complete -c my-app -n "__fish_seen_subcommand_from help; and not __fish_seen_subcommand_from cmd-single-quotes; and not __fish_seen_subcommand_from cmd-double-quotes; and not __fish_seen_subcommand_from cmd-backticks; and not __fish_seen_subcommand_from cmd-backslash; and not __fish_seen_subcommand_from cmd-brackets; and not __fish_seen_subcommand_from cmd-expansions; and not __fish_seen_subcommand_from help" -f -a "cmd-single-quotes" -d 'Can be /'always/', /'auto/', or /'never/''
complete -c my-app -n "__fish_seen_subcommand_from help; and not __fish_seen_subcommand_from cmd-single-quotes; and not __fish_seen_subcommand_from cmd-double-quotes; and not __fish_seen_subcommand_from cmd-backticks; and not __fish_seen_subcommand_from cmd-backslash; and not __fish_seen_subcommand_from cmd-brackets; and not __fish_seen_subcommand_from cmd-expansions; and not __fish_seen_subcommand_from help" -f -a "cmd-single-quotes" -d 'Can be \'always\', \'auto\', or \'never\''
complete -c my-app -n "__fish_seen_subcommand_from help; and not __fish_seen_subcommand_from cmd-single-quotes; and not __fish_seen_subcommand_from cmd-double-quotes; and not __fish_seen_subcommand_from cmd-backticks; and not __fish_seen_subcommand_from cmd-backslash; and not __fish_seen_subcommand_from cmd-brackets; and not __fish_seen_subcommand_from cmd-expansions; and not __fish_seen_subcommand_from help" -f -a "cmd-double-quotes" -d 'Can be "always", "auto", or "never"'
complete -c my-app -n "__fish_seen_subcommand_from help; and not __fish_seen_subcommand_from cmd-single-quotes; and not __fish_seen_subcommand_from cmd-double-quotes; and not __fish_seen_subcommand_from cmd-backticks; and not __fish_seen_subcommand_from cmd-backslash; and not __fish_seen_subcommand_from cmd-brackets; and not __fish_seen_subcommand_from cmd-expansions; and not __fish_seen_subcommand_from help" -f -a "cmd-backticks" -d 'For more information see `echo test`'
complete -c my-app -n "__fish_seen_subcommand_from help; and not __fish_seen_subcommand_from cmd-single-quotes; and not __fish_seen_subcommand_from cmd-double-quotes; and not __fish_seen_subcommand_from cmd-backticks; and not __fish_seen_subcommand_from cmd-backslash; and not __fish_seen_subcommand_from cmd-brackets; and not __fish_seen_subcommand_from cmd-expansions; and not __fish_seen_subcommand_from help" -f -a "cmd-backslash" -d 'Avoid /'//n/''
complete -c my-app -n "__fish_seen_subcommand_from help; and not __fish_seen_subcommand_from cmd-single-quotes; and not __fish_seen_subcommand_from cmd-double-quotes; and not __fish_seen_subcommand_from cmd-backticks; and not __fish_seen_subcommand_from cmd-backslash; and not __fish_seen_subcommand_from cmd-brackets; and not __fish_seen_subcommand_from cmd-expansions; and not __fish_seen_subcommand_from help" -f -a "cmd-backslash" -d 'Avoid \'\\n\''
complete -c my-app -n "__fish_seen_subcommand_from help; and not __fish_seen_subcommand_from cmd-single-quotes; and not __fish_seen_subcommand_from cmd-double-quotes; and not __fish_seen_subcommand_from cmd-backticks; and not __fish_seen_subcommand_from cmd-backslash; and not __fish_seen_subcommand_from cmd-brackets; and not __fish_seen_subcommand_from cmd-expansions; and not __fish_seen_subcommand_from help" -f -a "cmd-brackets" -d 'List packages [filter]'
complete -c my-app -n "__fish_seen_subcommand_from help; and not __fish_seen_subcommand_from cmd-single-quotes; and not __fish_seen_subcommand_from cmd-double-quotes; and not __fish_seen_subcommand_from cmd-backticks; and not __fish_seen_subcommand_from cmd-backslash; and not __fish_seen_subcommand_from cmd-brackets; and not __fish_seen_subcommand_from cmd-expansions; and not __fish_seen_subcommand_from help" -f -a "cmd-expansions" -d 'Execute the shell command with $SHELL'
complete -c my-app -n "__fish_seen_subcommand_from help; and not __fish_seen_subcommand_from cmd-single-quotes; and not __fish_seen_subcommand_from cmd-double-quotes; and not __fish_seen_subcommand_from cmd-backticks; and not __fish_seen_subcommand_from cmd-backslash; and not __fish_seen_subcommand_from cmd-brackets; and not __fish_seen_subcommand_from cmd-expansions; and not __fish_seen_subcommand_from help" -f -a "help" -d 'Print this message or the help of the given subcommand(s)'

0 comments on commit cdb83ae

Please sign in to comment.