Creating a bash completion script (2018)
iridakos.com
iridakos.com
As a point of comparison, see the difference between the git completions written in bash [1] vs the declarative equivalent [2].
[1] https://github.com/git/git/blob/master/contrib/completion/gi...
[2] https://github.com/withfig/autocomplete/blob/master/src/git....
That's also how autocompletion works with Python's excellent Click library.
The zsh completion system is better than the bash one; it has a builtin menu system, or you can use fzf: https://github.com/junegunn/fzf
Defining completion values for a given argument in a Winter CMS CLI command is as easy as adding a single method to the command's class now:
public function suggestMyArgumentValues(string $currentValue, array $allInput): array
{
return ['array', 'of', 'suggested', 'values'];
}Bash completion is designed for simple arguments like command line options and path names; anything else was not envisioned, in spite of embedded scripts being a pattern in the traditional Unix command line.
E.g. Awk completion could go like this:
awk 'BE_[Tab]
awk 'BEGIN {_It would almost be easier to modify the bash source itself to read and use a regular grammar, say:
foo [-p <pid>] [-f <configfile>]
<pid>: {ps -eo pid}
<configfile>: {ls -1 *.cfg}
or maybe something even nicer with help text complete -o nospace -C 'domterm "#complete-for-bash" "$COMP_LINE" "$COMP_POINT"' domterm
It works pretty well has the advantage that it automatically ties the completion to the executable, avoiding version mismatches.It would be great if other shells could use a similar mechanism. Then you could use the same completion logic for multiple shells. A complication is that zsh and fish completion provide descriptive help text; there would need to be a mechanism for a "completion option" to provide such help text.
Although it's not ideal (one ends up duplicating the arguments parsing logic of the program), it's actually not complicated, just some boilerplate.
It's an interesting idea that we're using for zio-cli [1].
https://fishshell.com/docs/current/cmds/fish_update_completi...