Skip to content
This repository has been archived by the owner on Nov 11, 2018. It is now read-only.

Add shell completion files #30

Open
p-e-w opened this issue Apr 4, 2016 · 14 comments
Open

Add shell completion files #30

p-e-w opened this issue Apr 4, 2016 · 14 comments

Comments

@p-e-w
Copy link
Owner

p-e-w commented Apr 4, 2016

No description provided.

@sanketplus
Copy link
Contributor

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

@p-e-w
Copy link
Owner Author

p-e-w commented Apr 10, 2016

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

@sanketplus
Copy link
Contributor

are you working on it already?

@p-e-w
Copy link
Owner Author

p-e-w commented Apr 11, 2016

No, and in fact I have no clue how to achieve it yet 😉

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... 🐟 😄

@sanketplus
Copy link
Contributor

can this help?

@p-e-w
Copy link
Owner Author

p-e-w commented Apr 12, 2016

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

@sanketplus
Copy link
Contributor

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
Copy link
Contributor

WHOA!! 😲
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

@p-e-w
Copy link
Owner Author

p-e-w commented Apr 14, 2016

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

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.

@ghost
Copy link

ghost commented Apr 14, 2016

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

@sanketplus
Copy link
Contributor

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? 😃

@p-e-w
Copy link
Owner Author

p-e-w commented Apr 15, 2016

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.

@sanketplus
Copy link
Contributor

sanketplus commented Apr 18, 2016

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 ?

@ghost
Copy link

ghost commented Apr 26, 2016

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants