Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python argparse and bash completion

I would like to get auto-completion on my python scripts also in the arguments.

I had never really understood how the bash_completion worked (for arguments), but after I digged in I understood that:

  1. it uses "complete" to bind a completing function to a command
  2. every completing function basically is a copy of the argument parser

The second point in particular is not great, because I would like to have it automatically generated.

The best thing would be that the shell asks to my program at every TAB about what to complete, but I have the impression that this can't really work, is that correct?

The second option is probably just to write a converter from an argparse parser to a shell function which completes correctly.

like image 874
andrea_crotti Avatar asked Dec 05 '11 15:12

andrea_crotti


People also ask

What is argparse in Python?

This tutorial is intended to be a gentle introduction to argparse, the recommended command-line parsing module in the Python standard library. There are two other modules that fulfill the same task, namely getopt (an equivalent for getopt () from the C language) and the deprecated optparse .

What is @argcomplete?

Argcomplete provides easy, extensible command line tab completion of arguments for your Python script. It makes two assumptions:

How do I enable global completion in Python?

you should put that line in your ~/.bashrc or follow argcomplete's docs and activate 'global' completion. After that you completion works as requested. The way this works is that the eval line creates a function _python_argcomlete which is registered using complete.

What is the Python-argcomplete-tcsh script?

The python-argcomplete-tcsh script provides completions for tcsh. The following is an example of the tcsh completion syntax for my-awesome-script emitted by register-python-argcomplete: or create new completion file, e.g:


2 Answers

Shameless self-promotion: https://github.com/kislyuk/argcomplete

argcomplete provides bash completion for argparse.

like image 150
weaver Avatar answered Oct 21 '22 17:10

weaver


Bash "completion" really is great. And easy for programs written in Python....

I think this is just what you want: optcomplete: Shell Completion Self-Generator for Python. It is available, e.g., as the "python-optcomplete" package in Ubuntu.

You insert a few lines in your python program, and the user (one time) runs the bash "complete" program to tell bash how to complete the arguments:

complete -F _optcomplete <program>

and now the user has completion! By default it gives simple completion on program options. See the example for how to customize how completion works for a particular option. It is beautifully written, and easy to extend to handle sub-commands, alternate completion options, etc.!

Update:

For completion in zsh (for both optparse and argparse) see genzshcomp 0.3.1 : Python Package Index

As noted by @englebip, we still need something similar for the new argparse module, introduced in Python 2.7 and 3.2, since optparse is now deprecated.

Here is the discussion on moving in that direction:

  • Mailing List Archive: [issue14103] argparse: add ability to create a bash_completion script
  • Issue 55 - argparse - add a utilities for shell autocompleters to hook into as well as support for extending completion info - Python command line parsing - Google Project Hosting

See also this background on how it is done: How does argparse (and the deprecated optparse) respond to 'tab' keypress after python program name, in bash? - Stack Overflow

like image 28
nealmcb Avatar answered Oct 21 '22 18:10

nealmcb