Skip to content
This repository has been archived by the owner on Feb 21, 2023. It is now read-only.

Commit

Permalink
static/completions/yak.zsh: Improve completion of commands after --.
Browse files Browse the repository at this point in the history
I have found that yak's completion breaks down in the following situation:

```ShellSession
$ yak env -- <TAB>
```

I expect it to expand possible executables, but instead, it completes filenames (which is just a Zsh
fallback shining though, i believe).  A side-effect is that completions for commands i rely on also
break:

```ShellSession
$ yak env -- aws <TAB> # Completes external commands, instead of aws-subcommands
```

This improvement does two things:

* Enable the `-S` flag to `_arguments` as documented here:
  https://zsh.sourceforge.io/Doc/Release/Completion-System.html#Completion-Functions.  Specifically,
  "-S -> Do not complete options after a ‘--’ appearing on the line, and ignore the ‘--’."

* Don't modify the `words` array ourselves; just call `_normal` completion once we've got an
  environment specified in positional argument 1.  Also, use 3 instead of 2 colons for
  `'*:::command...`, which according to the docs means: With three colons before the message, the
  words special array and the CURRENT special parameter are modified to refer only to the normal
  arguments covered by this description.

* Finally, i've removed `--` as an explicit option, because `-S` takes care of this in a nice
  built-in way.

Completions now work as i expect them to, with or without the `--` between yak and the command i'm
keen to run.
  • Loading branch information
toothbrush committed Aug 9, 2021
1 parent db1ac13 commit 6f89db1
Showing 1 changed file with 3 additions and 9 deletions.
12 changes: 3 additions & 9 deletions static/completions/yak.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

function _yak {
local roles=($(yak --list-roles --cache-only 2>/dev/null))
_arguments '-h[Display this help message and exit]' \
_arguments -S \
'-h[Display this help message and exit]' \
'--help[Display this help message and exit]' \
'-l[List available AWS roles and exit]' \
'--list-roles[List available AWS roles and exit]' \
Expand All @@ -15,12 +16,5 @@ function _yak {
'--no-cache[Ignore cache for this request. Mutually exclusive with --cache-only]' \
'--cache-only[Only use cache, do not make external requests. Mutually exclusive with --no-cache]' \
'--version[Print the current version and exit]' \
'--[Terminator for -- flags - necessary if you would like to pass -/-- flags to subcommands]' \
'1:environment:(${roles})' '*::arguments: _yak_command'
}

function _yak_command {
shift words
(( CURRENT-- ))
_normal
'1:environment:(${roles})' '*:::command:_normal'
}

0 comments on commit 6f89db1

Please sign in to comment.