I'm trying to write a small command launcher application, and would like to use bash's tab completions in my own completion system. I've been able to get a list of completions for general commands using compgen -abck
.
However, I would also like to get completions for specific commands: for instance, the input git p
should display completion for git's commands.
Is there any way I can use compgen
to do this? If not, are there any other ways I can get a list of completions programmatically?
[EDIT: To clarify, I'm not trying to provide completion to bash - my app is a GUI command launcher. I'd simply like to use bash's existing completions in my own app.]
Here a simple but working example in bash :
With this kind of code, you will get commandN completed when you type "foo c" + tab and paramN completed when you type "foo command1 p" + tab
You can compute the completion list from the command help.
my2c
Check in the
/etc/bash_completion.d/
directory. This is where the different command completion scripts stay.I don't really know how it works, but the awesome window manager uses the following Lua code for getting access to bash completion's result:
https://github.com/awesomeWM/awesome/blob/master/lib/awful/completion.lua#L119
complete -p
we findcomplete -o bashdefault -o default -o nospace -F _git git
. We remember "_git" for later.All together we use the following script:
Output: "log"
I hacked up this script a while back which gives you pretty accurate Man-page Bash Completion I know other people have done similar things with parsing
--help
option output.Take a look at the Debian Administration An introduction to bash completion - it's fairly easy to understand - by the way, it also has a part two.