Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The completion for zsh does not align the option and its alias. #2268

Closed
tasshi-me opened this issue Nov 21, 2022 · 1 comment · Fixed by #2269
Closed

The completion for zsh does not align the option and its alias. #2268

tasshi-me opened this issue Nov 21, 2022 · 1 comment · Fixed by #2269

Comments

@tasshi-me
Copy link
Contributor

Hi there, and thanks for the great package.
I encountered following unexpected behavior.

Environment

Summary

When we generate the completion script for zsh, with a option with an alias, the completion shown looks incorrect.
That's because the description of key from alias is missing in the generated completion script for zsh.

 -- values --
--bar          -- Bar option
--foo          -- Foo option
--help         -- Show help
--version      -- Show version number
-B         -F  --

Reproduction

I created the minimum reproduction in https://github.com/tasshi-playground/repro-yargs-generate-broken-completion-with-alias .

# If you don't enable zsh-completion, you need to enable it first.
# Clone and configure repository
$ git clone git@github.com:tasshi-playground/repro-yargs-generate-broken-completion-with-alias.git
$ cd repro-yargs-generate-broken-completion-with-alias
$ npm ci
$ npm run build
$ ./cli.js completion > /path/to/your/fpath/_cli.js
# Reload the shell

In the repository, I specified the options and aliases as follows.

yargs
  .option("foo", {
    describe: "Foo option",
    alias: "F",
    type: "string",
  })
  .option("bar", {
    describe: "Bar option",
    alias: "B",
    type: "string",
  })
  .completion()
  .help().argv;

Expected Behavior

The output when I strike the TAB should be the following.

./cli.js - #Strike TAB
 -- values --
--bar      -B  -- Bar option
--foo      -F  -- Foo option
--help         -- Show help
--version      -- Show version number

Actual Behavior

The actual output is the following.
The aliases (-B and -F) are aligned to same line with no descriptions.

./cli.js - #Strike TAB
 -- values --
--bar          -- Bar option
--foo          -- Foo option
--help         -- Show help
--version      -- Show version number
-B         -F  --

Cause

When I request a completion with striking the TAB key, completion function calls the original file with --get-yargs-completions option.
It is specified in completion script as follows.

_cli.js_yargs_completions()
{
  local reply
  local si=$IFS
  IFS=$'
' reply=($(COMP_CWORD="$((CURRENT-1))" COMP_LINE="$BUFFER" COMP_POINT="$CURSOR" ./cli.js --get-yargs-completions "${words[@]}"))
  IFS=$si
  _describe 'values' reply
}
compdef _cli.js_yargs_completions cli.js

However, the output of --get-yargs-completions is incorrect.
With current version of yargs, the output is the following.

$ node ./lib/index.js --get-yargs-completions -
--version:Show version number
--foo:Foo option
-F:
--bar:Bar option
-B:
--help:Show help

The key -F and -B are specified without description.

According to the document of zsh-completion, the two keys have the same description are collected together.
https://github.com/zsh-users/zsh-completions/blob/master/zsh-completions-howto.org#writing-simple-completion-functions-using-_describe

If two candidates have the same description, _describe collects them together on the same row and ensures that descriptions are aligned in neatly in columns.

So if we want to align the option and its alias, we have to add the description of option to the alias as follows.

$ node ./lib/index.js --get-yargs-completions -
--version:Show version number
--foo:Foo option
-F:Foo option
--bar:Bar option
-B:Bar option
--help:Show help
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant