Skip to content

Commit

Permalink
fix(complete): Escape , in completions for fish
Browse files Browse the repository at this point in the history
  • Loading branch information
ModProg committed Sep 2, 2022
1 parent 028a4cd commit 8ab649e
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 22 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
10 changes: 7 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
2 changes: 1 addition & 1 deletion clap_complete/tests/snapshots/sub_subcommands.bash
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ _my-app() {
fi
case "${prev}" in
--config)
COMPREPLY=($(compgen -W "Lest quotes aren't escaped." -- "${cur}"))
COMPREPLY=($(compgen -W "Lest quotes, aren't escaped. Second to trigger display of options" -- "${cur}"))
return 0
;;
*)
Expand Down
4 changes: 2 additions & 2 deletions clap_complete/tests/snapshots/sub_subcommands.elvish
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ set edit:completion:arg-completer[my-app] = {|@words|
}
&'my-app;some_cmd;sub_cmd'= {
cand --config 'the other case to test'
cand -h 'Print help information'
cand --help 'Print help information'
cand -h 'Print help information (use `--help` for more detail)'
cand --help 'Print help information (use `--help` for more detail)'
cand -V 'Print version information'
cand --version 'Print version information'
}
Expand Down
4 changes: 2 additions & 2 deletions clap_complete/tests/snapshots/sub_subcommands.fish
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ complete -c my-app -n "__fish_seen_subcommand_from some_cmd; and not __fish_seen
complete -c my-app -n "__fish_seen_subcommand_from some_cmd; and not __fish_seen_subcommand_from sub_cmd; and not __fish_seen_subcommand_from help" -s V -l version -d 'Print version information'
complete -c my-app -n "__fish_seen_subcommand_from some_cmd; and not __fish_seen_subcommand_from sub_cmd; and not __fish_seen_subcommand_from help" -f -a "sub_cmd" -d 'sub-subcommand'
complete -c my-app -n "__fish_seen_subcommand_from some_cmd; and not __fish_seen_subcommand_from sub_cmd; and not __fish_seen_subcommand_from help" -f -a "help" -d 'Print this message or the help of the given subcommand(s)'
complete -c my-app -n "__fish_seen_subcommand_from some_cmd; and __fish_seen_subcommand_from sub_cmd" -l config -d 'the other case to test' -r -f -a "{Lest quotes aren\'t escaped. }"
complete -c my-app -n "__fish_seen_subcommand_from some_cmd; and __fish_seen_subcommand_from sub_cmd" -s h -l help -d 'Print help information'
complete -c my-app -n "__fish_seen_subcommand_from some_cmd; and __fish_seen_subcommand_from sub_cmd" -l config -d 'the other case to test' -r -f -a "{Lest quotes\, aren\'t escaped. help\,with\,comma,Second to trigger display of options }"
complete -c my-app -n "__fish_seen_subcommand_from some_cmd; and __fish_seen_subcommand_from sub_cmd" -s h -l help -d 'Print help information (use `--help` for more detail)'
complete -c my-app -n "__fish_seen_subcommand_from some_cmd; and __fish_seen_subcommand_from sub_cmd" -s V -l version -d 'Print version information'
complete -c my-app -n "__fish_seen_subcommand_from some_cmd; and __fish_seen_subcommand_from help; and not __fish_seen_subcommand_from sub_cmd; and not __fish_seen_subcommand_from help" -f -a "sub_cmd" -d 'sub-subcommand'
complete -c my-app -n "__fish_seen_subcommand_from some_cmd; and __fish_seen_subcommand_from help; and not __fish_seen_subcommand_from sub_cmd; and not __fish_seen_subcommand_from help" -f -a "help" -d 'Print this message or the help of the given subcommand(s)'
Expand Down
4 changes: 2 additions & 2 deletions clap_complete/tests/snapshots/sub_subcommands.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ Register-ArgumentCompleter -Native -CommandName 'my-app' -ScriptBlock {
}
'my-app;some_cmd;sub_cmd' {
[CompletionResult]::new('--config', 'config', [CompletionResultType]::ParameterName, 'the other case to test')
[CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help information')
[CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help information')
[CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help information (use `--help` for more detail)')
[CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help information (use `--help` for more detail)')
[CompletionResult]::new('-V', 'V', [CompletionResultType]::ParameterName, 'Print version information')
[CompletionResult]::new('--version', 'version', [CompletionResultType]::ParameterName, 'Print version information')
break
Expand Down
7 changes: 4 additions & 3 deletions clap_complete/tests/snapshots/sub_subcommands.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,10 @@ _arguments "${_arguments_options[@]}" \
case $line[1] in
(sub_cmd)
_arguments "${_arguments_options[@]}" \
'*--config=[the other case to test]: :(Lest quotes aren't escaped.)' \
'*-h[Print help information]' \
'*--help[Print help information]' \
'*--config=[the other case to test]: :((Lest\ quotes,\ aren'\''t\ escaped.\:"help,with,comma"
Second\ to\ trigger\ display\ of\ options\:""))' \
'*-h[Print help information (use `--help` for more detail)]' \
'*--help[Print help information (use `--help` for more detail)]' \
'*-V[Print version information]' \
'*--version[Print version information]' \
&& ret=0
Expand Down

0 comments on commit 8ab649e

Please sign in to comment.