Skip to content

Commit

Permalink
fix(command): fix shell completion bug which occurs when an option ha…
Browse files Browse the repository at this point in the history
…s only one flag
  • Loading branch information
c4spar committed Aug 30, 2020
1 parent 2dfa8b1 commit 9fbef68
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions command/completions/zsh-completions-generator.ts
Expand Up @@ -17,9 +17,7 @@ export class ZshCompletionsGenerator {

private constructor(protected cmd: Command) {}

/**
* Generates zsh completions code.
*/
/** Generates zsh completions code. */
private generate(): string {
const version: string | undefined = this.cmd.getVersion();
const versionStr = version
Expand Down Expand Up @@ -71,9 +69,7 @@ compdef _${replaceSpecialChars(this.cmd.getPath())} ${this.cmd.getPath()}
`.trim();
}

/**
* Generates zsh completions method for given command and child commands.
*/
/** Generates zsh completions method for given command and child commands. */
private generateCompletions(command: Command, path = ""): string {
if (
!command.hasCommands(false) && !command.hasOptions(false) &&
Expand Down Expand Up @@ -231,6 +227,7 @@ function _${replaceSpecialChars(path)}() {` +
completionsPath: string,
excludedOptions: string[],
): string {
const flags = option.flags.split(/[, ] */g);
let excludedFlags = option.conflicts?.length
? [
...excludedOptions,
Expand All @@ -257,15 +254,18 @@ function _${replaceSpecialChars(path)}() {` +
"\n",
).shift();
const collect: string = option.collect ? "*" : "";
const flags: string = option.flags.replace(/ +/g, "");

if (option.standalone) {
return `'(- *)'{${collect}${flags}}'[${description}]${args}'`;
} else {
const excluded: string = excludedFlags.length
? `'(${excludedFlags.join(" ")})'`
: "";
return `${excluded}{${collect}${flags}}'[${description}]${args}'`;
if (collect || flags.length > 1) {
return `${excluded}{${collect}${flags}}'[${description}]${args}'`;
} else {
return `${excluded}${flags}'[${description}]${args}'`;
}
}
}

Expand Down

0 comments on commit 9fbef68

Please sign in to comment.