maybe
maybe copied to clipboard
Add shell completion files
Hey sorry to bother @p-e-w but could you explain what we are trying to achieve here?
@sanketplus: Here is the basic problem (bash):
$ sudo reb --> PRESS TAB KEY
$ sudo reboot
$ maybe reb --> PRESS TAB KEY
$ maybe reb
bash supports programmable completion. A completion script needs to be added so bash knows that maybe expects another command as its argument, and can complete accordingly.
Other shells handle completion differently. Notably, fish appears to draw from PATH everywhere so the above works as expected already.
are you working on it already?
No, and in fact I have no clue how to achieve it yet :wink:
I'd be very happy to accept a pull request for this issue. Looking at sudo is probably a good place to start, since it's the exact same use case: Running a command with different privileges. Eventually, we probably want completion for maybe's command line arguments as well (like --list-only, more to come), but for now, making sudo-like command completion work would be a big step forward already.
Or we can just wait for everyone to migrate to fish instead... :fish: :smile:
can this help?
argcomplete looks fantastic – I had no idea something like this existed! From the documentation, it seems that it will take care of option completion automatically, which leaves only completion of the command to be implemented. I'm still unsure how to best approach this, as argcomplete's FilesCompleter is not the right tool for the job. I tried to locate sudo's bash completion file, but no luck so far.
In any case, argcomplete definitely looks like the way to go. :+1:
While digging more on the same I discovered the following!
A similar but more simpler CLICK which can replace both argparser and argcomplete
And for completion which BASH uses, it happens that BASH is using readline library. The file you were searching for is maybe /usr/share/bash-completion/bash_completion. I got it's path from file /etc/bash_completion (Ubuntu)
What we can do is for command line args we can use either Click or Argparse and for rest of the completion we can use BASH Completion (complete command) [or we can use BASH completion for everything]. This An introduction to bash completion can get help us getting started.
WHOA!! :astonished:
Just executed $ complete -F _command maybe and it now auto completes commands ( _commands is a type which will be auto completed)
and after that $ maybe reb --> TAB resulted in maybe reboot
I found sudo's completion file: https://github.com/scop/bash-completion/blob/master/completions/sudo. Also, complete -p gives
complete -F _root_command sudo
similar to what you discovered. I have searched the web in vain for more details on parameters like _command and _root_command, however. As usual, advanced bash stuff is totally arcane. The output of help complete is ridiculously lacking. But even so, your insight already gets us 90% of what we want. :+1:
There is just one problem: It does not seem to be possible to specify the simple command complete -F _command maybe from argcomplete! A hybrid completion file that invokes both argcomplete and complete might be needed.
Had a look at Click too (which was also new to me). It has some nice ideas, but also a big red flag:
Click is internally based on optparse instead of argparse. This however is an implementation detail that a user does not have to be concerned with.
No offense to the author, but I strongly disagree. optparse has been deprecated since Python 2.7. It might very well be removed in a future Python version. If that happens, and Click does not get fixed, maybe will stop working. I much prefer argparse + argcomplete.
It sounds an interesting problem. I will check it tomorrow.
Instead of using argparse + argcomplete and inbuilt BASH completion (complete -F _command maybe) what I think would be nice is a custom script in /etc/bash_completion.d/ in which we can make a function (similar to _command and _root_command which are also functions only) which will auto complete arguments accepted by argparse. Hence it would be argparse for parsing arguments and BASH completion for autocomplete. This would eliminate requirement argcomplete. That script maybe can solve the problem of executing command complete -F _command maybe
How about that? :smiley:
Two potential issues:
- Installing to
/etc/bash_completion.d/requires root privileges. This means it won't work withpip install --useror virtualenv installs, which is quite a pity. - If we don't use argcomplete, we have to manually write and maintain completers for all command line options. I favor the automatic approach.
Alternatively:
- We could add an entry to .bashrc or similar file something like
complete -F _command maybe - For auto completing args we could use argcomplete
how about this approach @p-e-w ?
I use the /etc/bash_completion.d/ in my fork.