Skip to content

Commit

Permalink
fix(ZSH Completions): escapes square brackets in ZSH completions
Browse files Browse the repository at this point in the history
Since ZSH completions use `[ and ]` for descriptions, but clap args use `[ and ]` for possible
values and other auxillary arg information, this was creating a conflict. clap now escapes any
square brackets before writing the help, i.e. `\\[ and \\]` which removes conflicts.

Alternatives would be to switch `[ and ]` for `( and )` but this would create a slight difference
in help messages and completions.

Closes #771
  • Loading branch information
kbknapp committed Dec 8, 2016
1 parent ff7febf commit 7e17d5a
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/completions/zsh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ fn subcommands_and_args_of(p: &Parser) -> String {
fn add_sc(sc: &App, n: &str, ret: &mut Vec<String>) {
let s = format!("\"{name}:{help}\" \\",
name = n,
help = sc.p.meta.about.unwrap_or(""));
help = sc.p.meta.about.unwrap_or("").replace("[", "\\[").replace("]", "\\]"));
if !s.is_empty() {
ret.push(s);
}
Expand All @@ -151,7 +151,7 @@ fn subcommands_and_args_of(p: &Parser) -> String {
debugln!("iter;arg={}", arg.b.name);
let a = format!("\"{name}:{help}\" \\",
name = arg.b.name.to_ascii_uppercase(),
help = arg.b.help.unwrap_or(""));
help = arg.b.help.unwrap_or("").replace("[", "\\[").replace("]", "\\]"));

if !a.is_empty() {
ret.push(a);
Expand Down Expand Up @@ -299,7 +299,7 @@ fn write_opts_of(p: &Parser) -> String {
let mut ret = vec![];
for o in p.opts() {
debugln!("iter;o={}", o.name());
let help = o.help().unwrap_or("");
let help = o.help().unwrap_or("").replace("[", "\\[").replace("]", "\\]");
let mut conflicts = get_zsh_arg_conflicts!(p, o, INTERNAL_ERROR_MSG);
conflicts = if conflicts.is_empty() {
String::new()
Expand Down Expand Up @@ -349,7 +349,7 @@ fn write_flags_of(p: &Parser) -> String {
let mut ret = vec![];
for f in p.flags() {
debugln!("iter;f={}", f.name());
let help = f.help().unwrap_or("");
let help = f.help().unwrap_or("").replace("[", "\\[").replace("]", "\\]");
let mut conflicts = get_zsh_arg_conflicts!(p, f, INTERNAL_ERROR_MSG);
conflicts = if conflicts.is_empty() {
String::new()
Expand Down

0 comments on commit 7e17d5a

Please sign in to comment.