Python argparse and bash completion

andrea_crotti picture andrea_crotti · Dec 5, 2011 · Viewed 18.2k times · Source

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.

Answer

weaver picture weaver · Nov 25, 2012

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

argcomplete provides bash completion for argparse.