4 ms·
Creating a bash completion script (2018)
- davvid 5y agoargcomplete[1] is worth an honorable mention ~ it makes it really easy to add shell completion to any Python program that uses argparse. [1] https://pypi.org/project/argcomplete/ https://pypi.org/project/argcomplete/
- m463 5y agocompletion scripts are very very useful, but writing them is a disorganized mess. 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
- toolslive 5y agowell, your completion script doesn't _need_ to be written in bash. If you're writing a completion script for an executable, that executable can be called with an option that generates the completions for bash to consume. You just need to generate the output in the expected format and install a softlink . Hope this can give you some comfort.
- Per_Bothner 5y agoBash completion for the domterm command (a terminal emulator) calls the domterm command itself with a special "#complete-for-bash" option: 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.
- sm_ts 5y agoI wrote long ago about writing competion scripts in any language: https://saveriomiroddi.github.io/Using-scripts-in-any-language-for-bash-zsh-tab-completion https://saveriomiroddi.github.io/Using-scripts-in-any-langua.... Although it's not ideal (one ends up duplicating the arguments parsing logic of the program), it's actually not complicated, just some boilerplate.
- harveywi 5y ago>It would almost be easier to modify the bash source itself to read and use a regular grammar It's an interesting idea that we're using for zio-cli [1]. [1] https://github.com/zio/zio-cli/pull/119 https://github.com/zio/zio-cli/pull/119
- theluketowers 5y agoSymfony 5.4 just added support for bash completion to PHP CLI commands using the library so we were able to add a very nice and easy to use interface to Winter CMS to take advantage of it: https://wintercms.com/blog/post/console-improvements-winter-12 https://wintercms.com/blog/post/console-improvements-winter-... 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']; }
- mschrage 5y agoWriting shell completions is a surprisingly tricky - especially for larger CLI tools or when you want to provide more advanced suggestions. Everything has to be done imperatively, usually as a shell script! It can get hairy quickly. 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/git-completion.bash https://github.com/git/git/blob/master/contrib/completion/gi... [2] https://github.com/withfig/autocomplete/blob/master/src/git.ts https://github.com/withfig/autocomplete/blob/master/src/git....
- pastage 5y agoGreat that someone is thinking about this, doesn't seem very usefull though, it requires MacOS. But the bash one has been there since the beginning, so you are comparing a project that seems to be created in 2021 without any need for backward compatibilty with one that has grown organically for 15 years.
- nrclark 5y agoIt doesn't have to be pure shell. I've written a few tools that have built-in completion helpers. All their completion scripts have to do is run the tool with the right environment variables, and return the results. That's also how autocompletion works with Python's excellent Click library.
- mieubrisse 5y agoFor folks writing a Go CLI, the Cobra library (used by the kubectl and docker CLIs) comes with completions for bash, zsh, and fish out of the box. Completion scripts are indeed a hairy mess (see the instructions for installing Bash completions on kubectl!) and we were very pleasantly surprised at how trivial Cobra made it. (I've no affiliation with Cobra; just a very happy user)
- kortex 5y agoI use Cleo for python, same thing, has the ability to generate completion scripts. I also find the API far saner than argparse or click. I don't really use the docstring-parser, I prefer using the Command api for finer control, but it's really cool to just write the help docs and automagically get options. https://github.com/sdispater/cleo https://github.com/sdispater/cleo
- jakupovic 5y agoCobra is awesome. Was surprised the same at how easy it was to add completion support. Here is +1 from this satisfied end user, no affiliation with cobra team.
- vcdimension 5y agoHere's a tutorial on creating zsh completion scripts: https://github.com/zsh-users/zsh-completions/blob/master/zsh-completions-howto.org https://github.com/zsh-users/zsh-completions/blob/master/zsh... 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 https://github.com/junegunn/fzf
- assbuttbuttass 5y agofish shell attempts to parse completions from man pages. https://fishshell.com/docs/current/cmds/fish_update_completions.html https://fishshell.com/docs/current/cmds/fish_update_completi...
- kazinator 5y agoI gave up on Bash completion when I ran into a silly thing that can't be turned off: when you complete something which is quoted 'foo.b_[Tab] it will complete, close the quote and add a space: 'foo.bar' _. When you wanted 'foo.bar_ so then you could continue, for instance, like 'foo.bar(arg, _. There isn't anything the completion handler can do to fix this. 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 {_