Skip to content

Commit

Permalink
fix(command): zsh completion broken for options without aliases (#515)
Browse files Browse the repository at this point in the history
  • Loading branch information
c4spar authored Dec 19, 2022
1 parent 8d753ac commit f65465a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 15 deletions.
10 changes: 6 additions & 4 deletions command/completions/_zsh_completions_generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,18 +289,20 @@ function _${replaceSpecialChars(path)}() {` +

const collect: string = option.collect ? "*" : "";
const equalsSign = option.equalsSign ? "=" : "";
const flags = option.flags.map((flag) => `${flag}${equalsSign}`).join(",");
const flags = option.flags.map((flag) => `${flag}${equalsSign}`);

if (option.standalone) {
return `'(- *)'{${collect}${flags}}'[${description}]${args}'`;
return `'(- *)'{${collect}${flags.join(",")}}'[${description}]${args}'`;
} else {
const excluded: string = excludedFlags.length
? `'(${excludedFlags.join(" ")})'`
: "";
if (collect || flags.length > 1) {
return `${excluded}{${collect}${flags}}'[${description}]${args}'`;
return `${excluded}{${collect}${
flags.join(",")
}}'[${description}]${args}'`;
} else {
return `${excluded}${flags}'[${description}]${args}'`;
return `${excluded}${flags.join(",")}'[${description}]${args}'`;
}
}
}
Expand Down
20 changes: 10 additions & 10 deletions command/test/integration/__snapshots__/test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ _completions_test() {
}
__completions_test() {
opts=(-h --help -V --version -g --global -m --main -c --color -C --colors foo bar validation-error help completions)
opts=(-h --help -V --version -g --global -m --main --color -C --colors foo bar validation-error help completions)
_completions_test_complete color
if [[ \${cur} == -* || \${COMP_CWORD} -eq 1 ]] ; then
return 0
Expand All @@ -97,7 +97,7 @@ _completions_test() {
-V|--version) opts=() ;;
-g|--global) opts=(); _completions_test_complete boolean ;;
-m|--main) opts=(); _completions_test_complete boolean ;;
-c|--color) opts=(); _completions_test_complete color ;;
--color) opts=(); _completions_test_complete color ;;
-C|--colors) opts=(); _completions_test_complete color ;;
esac
}
Expand Down Expand Up @@ -373,7 +373,7 @@ complete -c completions-test -n '__fish_completions_test_using_command __complet
complete -c completions-test -n '__fish_completions_test_using_command __completions_test' -s V -l version -x -k -f -d 'Show the version number for this program.'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test' -s g -l global -k -f -r -a '(completions-test completions complete boolean )' -d 'Foo option.'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test' -s m -l main -k -f -r -a '(completions-test completions complete boolean )' -d 'Bar option.'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test' -s c -l color -k -f -r -a '(completions-test completions complete color )' -d 'Color option.'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test' -l color -k -f -r -a '(completions-test completions complete color )' -d 'Color option.'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test' -s C -l colors -k -f -r -a '(completions-test completions complete color )' -d 'Color option.'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test' -k -f -a foo -d 'Foo command.'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test_foo' -s h -l help -x -k -f -d 'Show this help.'
Expand Down Expand Up @@ -492,7 +492,7 @@ function _completions_test() {
'(- *)'{-V,--version}'[Show the version number for this program.]' \\\\
'(-h --help -V --version -g --global)'{-g,--global}'[Foo option.]:val:->val-boolean' \\\\
'(-h --help -V --version -m --main)'{-m,--main}'[Bar option.]:val:->val-boolean' \\\\
'(-h --help -V --version -c --color)'{-c=,--color=}'[Color option.]:val:->val-color' \\\\
'(-h --help -V --version --color)'--color='[Color option.]:val:->val-color' \\\\
'(-h --help -V --version -C --colors)'{-C,--colors}'[Color option.]:val:->val-color' \\\\
'1:command:_commands'\\\\
'2::path:_files'\\\\
Expand Down Expand Up @@ -906,7 +906,7 @@ snapshot[`command integration > should output command help with help command 1`]
\\x1b[34m-V\\x1b[39m, \\x1b[34m--version\\x1b[39m \\x1b[31m\\x1b[1m-\\x1b[22m\\x1b[39m Show the version number for this program.
\\x1b[34m-g\\x1b[39m, \\x1b[34m--global\\x1b[39m \\x1b[33m<\\x1b[39m\\x1b[35mval\\x1b[39m\\x1b[33m>\\x1b[39m \\x1b[31m\\x1b[1m-\\x1b[22m\\x1b[39m Foo option.
\\x1b[34m-m\\x1b[39m, \\x1b[34m--main\\x1b[39m \\x1b[33m<\\x1b[39m\\x1b[35mval\\x1b[39m\\x1b[33m>\\x1b[39m \\x1b[31m\\x1b[1m-\\x1b[22m\\x1b[39m Bar option.
\\x1b[34m-c\\x1b[39m, \\x1b[34m--color\\x1b[39m \\x1b[33m<\\x1b[39m\\x1b[35mval\\x1b[39m\\x1b[33m>\\x1b[39m \\x1b[31m\\x1b[1m-\\x1b[22m\\x1b[39m Color option. (\\x1b[1mValues: \\x1b[22m\\x1b[32m"blue"\\x1b[39m, \\x1b[32m"yellow"\\x1b[39m, \\x1b[32m"red"\\x1b[39m)
\\x1b[34m--color\\x1b[39m \\x1b[33m<\\x1b[39m\\x1b[35mval\\x1b[39m\\x1b[33m>\\x1b[39m \\x1b[31m\\x1b[1m-\\x1b[22m\\x1b[39m Color option. (\\x1b[1mValues: \\x1b[22m\\x1b[32m"blue"\\x1b[39m, \\x1b[32m"yellow"\\x1b[39m, \\x1b[32m"red"\\x1b[39m)
\\x1b[34m-C\\x1b[39m, \\x1b[34m--colors\\x1b[39m \\x1b[33m<\\x1b[39m\\x1b[35mval...\\x1b[39m\\x1b[33m>\\x1b[39m \\x1b[31m\\x1b[1m-\\x1b[22m\\x1b[39m Color option. (\\x1b[1mValues: \\x1b[22m\\x1b[32m"blue"\\x1b[39m, \\x1b[32m"yellow"\\x1b[39m, \\x1b[32m"red"\\x1b[39m)
\\x1b[1mCommands:\\x1b[22m
Expand Down Expand Up @@ -965,7 +965,7 @@ snapshot[`command integration > should output short help with -h flag 1`] = `
\\x1b[34m-V\\x1b[39m, \\x1b[34m--version\\x1b[39m \\x1b[31m\\x1b[1m-\\x1b[22m\\x1b[39m Show the version number for this program.
\\x1b[34m-g\\x1b[39m, \\x1b[34m--global\\x1b[39m \\x1b[33m<\\x1b[39m\\x1b[35mval\\x1b[39m\\x1b[33m>\\x1b[39m \\x1b[31m\\x1b[1m-\\x1b[22m\\x1b[39m Foo option.
\\x1b[34m-m\\x1b[39m, \\x1b[34m--main\\x1b[39m \\x1b[33m<\\x1b[39m\\x1b[35mval\\x1b[39m\\x1b[33m>\\x1b[39m \\x1b[31m\\x1b[1m-\\x1b[22m\\x1b[39m Bar option.
\\x1b[34m-c\\x1b[39m, \\x1b[34m--color\\x1b[39m \\x1b[33m<\\x1b[39m\\x1b[35mval\\x1b[39m\\x1b[33m>\\x1b[39m \\x1b[31m\\x1b[1m-\\x1b[22m\\x1b[39m Color option. (\\x1b[1mValues: \\x1b[22m\\x1b[32m"blue"\\x1b[39m, \\x1b[32m"yellow"\\x1b[39m, \\x1b[32m"red"\\x1b[39m)
\\x1b[34m--color\\x1b[39m \\x1b[33m<\\x1b[39m\\x1b[35mval\\x1b[39m\\x1b[33m>\\x1b[39m \\x1b[31m\\x1b[1m-\\x1b[22m\\x1b[39m Color option. (\\x1b[1mValues: \\x1b[22m\\x1b[32m"blue"\\x1b[39m, \\x1b[32m"yellow"\\x1b[39m, \\x1b[32m"red"\\x1b[39m)
\\x1b[34m-C\\x1b[39m, \\x1b[34m--colors\\x1b[39m \\x1b[33m<\\x1b[39m\\x1b[35mval...\\x1b[39m\\x1b[33m>\\x1b[39m \\x1b[31m\\x1b[1m-\\x1b[22m\\x1b[39m Color option. (\\x1b[1mValues: \\x1b[22m\\x1b[32m"blue"\\x1b[39m, \\x1b[32m"yellow"\\x1b[39m, \\x1b[32m"red"\\x1b[39m)
\\x1b[1mCommands:\\x1b[22m
Expand Down Expand Up @@ -1005,7 +1005,7 @@ snapshot[`command integration > should output long help with --help flag 1`] = `
foo bar baz.
foo bar baz.
\\x1b[34m-c\\x1b[39m, \\x1b[34m--color\\x1b[39m \\x1b[33m<\\x1b[39m\\x1b[35mval\\x1b[39m\\x1b[33m>\\x1b[39m \\x1b[31m\\x1b[1m-\\x1b[22m\\x1b[39m Color option. (\\x1b[1mValues: \\x1b[22m\\x1b[32m"blue"\\x1b[39m, \\x1b[32m"yellow"\\x1b[39m, \\x1b[32m"red"\\x1b[39m)
\\x1b[34m--color\\x1b[39m \\x1b[33m<\\x1b[39m\\x1b[35mval\\x1b[39m\\x1b[33m>\\x1b[39m \\x1b[31m\\x1b[1m-\\x1b[22m\\x1b[39m Color option. (\\x1b[1mValues: \\x1b[22m\\x1b[32m"blue"\\x1b[39m, \\x1b[32m"yellow"\\x1b[39m, \\x1b[32m"red"\\x1b[39m)
\\x1b[34m-C\\x1b[39m, \\x1b[34m--colors\\x1b[39m \\x1b[33m<\\x1b[39m\\x1b[35mval...\\x1b[39m\\x1b[33m>\\x1b[39m \\x1b[31m\\x1b[1m-\\x1b[22m\\x1b[39m Color option. (\\x1b[1mValues: \\x1b[22m\\x1b[32m"blue"\\x1b[39m, \\x1b[32m"yellow"\\x1b[39m, \\x1b[32m"red"\\x1b[39m)
\\x1b[1mCommands:\\x1b[22m
Expand Down Expand Up @@ -1041,7 +1041,7 @@ snapshot[`command integration > should output short version with -V flag 1`] = `
\\x1b[34m-V\\x1b[39m, \\x1b[34m--version\\x1b[39m \\x1b[31m\\x1b[1m-\\x1b[22m\\x1b[39m Show the version number for this program.
\\x1b[34m-g\\x1b[39m, \\x1b[34m--global\\x1b[39m \\x1b[33m<\\x1b[39m\\x1b[35mval\\x1b[39m\\x1b[33m>\\x1b[39m \\x1b[31m\\x1b[1m-\\x1b[22m\\x1b[39m Foo option.
\\x1b[34m-m\\x1b[39m, \\x1b[34m--main\\x1b[39m \\x1b[33m<\\x1b[39m\\x1b[35mval\\x1b[39m\\x1b[33m>\\x1b[39m \\x1b[31m\\x1b[1m-\\x1b[22m\\x1b[39m Bar option.
\\x1b[34m-c\\x1b[39m, \\x1b[34m--color\\x1b[39m \\x1b[33m<\\x1b[39m\\x1b[35mval\\x1b[39m\\x1b[33m>\\x1b[39m \\x1b[31m\\x1b[1m-\\x1b[22m\\x1b[39m Color option. (\\x1b[1mValues: \\x1b[22m\\x1b[32m"blue"\\x1b[39m, \\x1b[32m"yellow"\\x1b[39m, \\x1b[32m"red"\\x1b[39m)
\\x1b[34m--color\\x1b[39m \\x1b[33m<\\x1b[39m\\x1b[35mval\\x1b[39m\\x1b[33m>\\x1b[39m \\x1b[31m\\x1b[1m-\\x1b[22m\\x1b[39m Color option. (\\x1b[1mValues: \\x1b[22m\\x1b[32m"blue"\\x1b[39m, \\x1b[32m"yellow"\\x1b[39m, \\x1b[32m"red"\\x1b[39m)
\\x1b[34m-C\\x1b[39m, \\x1b[34m--colors\\x1b[39m \\x1b[33m<\\x1b[39m\\x1b[35mval...\\x1b[39m\\x1b[33m>\\x1b[39m \\x1b[31m\\x1b[1m-\\x1b[22m\\x1b[39m Color option. (\\x1b[1mValues: \\x1b[22m\\x1b[32m"blue"\\x1b[39m, \\x1b[32m"yellow"\\x1b[39m, \\x1b[32m"red"\\x1b[39m)
\\x1b[1mCommands:\\x1b[22m
Expand Down Expand Up @@ -1081,7 +1081,7 @@ snapshot[`command integration > should output long version with --version flag 1
foo bar baz.
foo bar baz.
\\x1b[34m-c\\x1b[39m, \\x1b[34m--color\\x1b[39m \\x1b[33m<\\x1b[39m\\x1b[35mval\\x1b[39m\\x1b[33m>\\x1b[39m \\x1b[31m\\x1b[1m-\\x1b[22m\\x1b[39m Color option. (\\x1b[1mValues: \\x1b[22m\\x1b[32m"blue"\\x1b[39m, \\x1b[32m"yellow"\\x1b[39m, \\x1b[32m"red"\\x1b[39m)
\\x1b[34m--color\\x1b[39m \\x1b[33m<\\x1b[39m\\x1b[35mval\\x1b[39m\\x1b[33m>\\x1b[39m \\x1b[31m\\x1b[1m-\\x1b[22m\\x1b[39m Color option. (\\x1b[1mValues: \\x1b[22m\\x1b[32m"blue"\\x1b[39m, \\x1b[32m"yellow"\\x1b[39m, \\x1b[32m"red"\\x1b[39m)
\\x1b[34m-C\\x1b[39m, \\x1b[34m--colors\\x1b[39m \\x1b[33m<\\x1b[39m\\x1b[35mval...\\x1b[39m\\x1b[33m>\\x1b[39m \\x1b[31m\\x1b[1m-\\x1b[22m\\x1b[39m Color option. (\\x1b[1mValues: \\x1b[22m\\x1b[32m"blue"\\x1b[39m, \\x1b[32m"yellow"\\x1b[39m, \\x1b[32m"red"\\x1b[39m)
\\x1b[1mCommands:\\x1b[22m
Expand Down Expand Up @@ -1137,7 +1137,7 @@ snapshot[`command integration > should print error message for unknown option wi
\\x1b[34m-V\\x1b[39m, \\x1b[34m--version\\x1b[39m \\x1b[31m\\x1b[1m-\\x1b[22m\\x1b[39m Show the version number for this program.
\\x1b[34m-g\\x1b[39m, \\x1b[34m--global\\x1b[39m \\x1b[33m<\\x1b[39m\\x1b[35mval\\x1b[39m\\x1b[33m>\\x1b[39m \\x1b[31m\\x1b[1m-\\x1b[22m\\x1b[39m Foo option.
\\x1b[34m-m\\x1b[39m, \\x1b[34m--main\\x1b[39m \\x1b[33m<\\x1b[39m\\x1b[35mval\\x1b[39m\\x1b[33m>\\x1b[39m \\x1b[31m\\x1b[1m-\\x1b[22m\\x1b[39m Bar option.
\\x1b[34m-c\\x1b[39m, \\x1b[34m--color\\x1b[39m \\x1b[33m<\\x1b[39m\\x1b[35mval\\x1b[39m\\x1b[33m>\\x1b[39m \\x1b[31m\\x1b[1m-\\x1b[22m\\x1b[39m Color option. (\\x1b[1mValues: \\x1b[22m\\x1b[32m"blue"\\x1b[39m, \\x1b[32m"yellow"\\x1b[39m, \\x1b[32m"red"\\x1b[39m)
\\x1b[34m--color\\x1b[39m \\x1b[33m<\\x1b[39m\\x1b[35mval\\x1b[39m\\x1b[33m>\\x1b[39m \\x1b[31m\\x1b[1m-\\x1b[22m\\x1b[39m Color option. (\\x1b[1mValues: \\x1b[22m\\x1b[32m"blue"\\x1b[39m, \\x1b[32m"yellow"\\x1b[39m, \\x1b[32m"red"\\x1b[39m)
\\x1b[34m-C\\x1b[39m, \\x1b[34m--colors\\x1b[39m \\x1b[33m<\\x1b[39m\\x1b[35mval...\\x1b[39m\\x1b[33m>\\x1b[39m \\x1b[31m\\x1b[1m-\\x1b[22m\\x1b[39m Color option. (\\x1b[1mValues: \\x1b[22m\\x1b[32m"blue"\\x1b[39m, \\x1b[32m"yellow"\\x1b[39m, \\x1b[32m"red"\\x1b[39m)
\\x1b[1mCommands:\\x1b[22m
Expand Down
2 changes: 1 addition & 1 deletion command/test/integration/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const cmd = new Command()
foo bar baz.
`,
)
.option("-c, --color=<val:color>", "Color option.")
.option("--color=<val:color>", "Color option.")
.option("-C, --colors <val...:color>", "Color option.")
.arguments("<color:color> [path...:file]")
.default("help")
Expand Down

0 comments on commit f65465a

Please sign in to comment.