maybe icon indicating copy to clipboard operation
maybe copied to clipboard

Add shell completion files

Open p-e-w opened this issue 9 years ago • 14 comments

p-e-w avatar Apr 04 '16 17:04 p-e-w

Hey sorry to bother @p-e-w but could you explain what we are trying to achieve here?

sanketplus avatar Apr 05 '16 07:04 sanketplus

@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.

p-e-w avatar Apr 10 '16 12:04 p-e-w

are you working on it already?

sanketplus avatar Apr 11 '16 06:04 sanketplus

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:

p-e-w avatar Apr 11 '16 17:04 p-e-w

can this help?

sanketplus avatar Apr 12 '16 06:04 sanketplus

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:

p-e-w avatar Apr 12 '16 18:04 p-e-w

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.

sanketplus avatar Apr 13 '16 09:04 sanketplus

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

sanketplus avatar Apr 13 '16 09:04 sanketplus

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.

p-e-w avatar Apr 14 '16 16:04 p-e-w

It sounds an interesting problem. I will check it tomorrow.

ghost avatar Apr 14 '16 19:04 ghost

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:

sanketplus avatar Apr 15 '16 06:04 sanketplus

Two potential issues:

  1. Installing to /etc/bash_completion.d/ requires root privileges. This means it won't work with pip install --user or virtualenv installs, which is quite a pity.
  2. If we don't use argcomplete, we have to manually write and maintain completers for all command line options. I favor the automatic approach.

p-e-w avatar Apr 15 '16 16:04 p-e-w

Alternatively:

  1. We could add an entry to .bashrc or similar file something like complete -F _command maybe
  2. For auto completing args we could use argcomplete

how about this approach @p-e-w ?

sanketplus avatar Apr 18 '16 08:04 sanketplus

I use the /etc/bash_completion.d/ in my fork.

ghost avatar Apr 26 '16 21:04 ghost