Repository: clap-rs/clap
Pull request #3469 (2022-02-14 21:00:20)

fix(complete): escape fish possible values
I _think_ this is the right thing to do, but if anyone who actually understands fish's quoting could confirm, that would be useful.

---

#### Commits _(oldest to newest)_

0fbabaa3 test(completion): add regression test for fish quoting issue


<br/>

6f36ca3a fix(complete): escape fish possible values

Fixes: https://github.com/clap-rs/clap/issues/3467

<br/>


Patch:
diff --git a/clap_complete/src/shells/fish.rs b/clap_complete/src/shells/fish.rs
index 06342296..3327dbc1 100644
--- a/clap_complete/src/shells/fish.rs
+++ b/clap_complete/src/shells/fish.rs
@@ -158,8 +158,8 @@ fn value_completion(option: &Arg) -> String {
                 } else {
                     Some(format!(
                         "{}\t{}",
-                        value.get_name(),
-                        value.get_help().unwrap_or_default()
+                        escape_string(value.get_name()).as_str(),
+                        escape_string(value.get_help().unwrap_or_default()).as_str()
                     ))
                 })
                 .collect::<Vec<_>>()


Test patch:
diff --git a/clap_complete/tests/completions/fish.rs b/clap_complete/tests/completions/fish.rs
index 0da349c2..beb0ae39 100644
--- a/clap_complete/tests/completions/fish.rs
+++ b/clap_complete/tests/completions/fish.rs
@@ -1,3 +1,5 @@
+use clap::PossibleValue;
+
 use super::*;
 
 fn build_app() -> App<'static> {
@@ -172,6 +174,7 @@ fn build_app_sub_subcommands() -> App<'static> {
                     Arg::new("config")
                         .long("--config")
                         .takes_value(true)
+                        .possible_values([PossibleValue::new("Lest quotes aren't escaped.")])
                         .help("the other case to test"),
                 ),
             ),
@@ -190,7 +193,7 @@ 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
+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" -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" -s h -l help -d 'Print help information'