4

I noticed that bash tab completion returns less files if I use an argparse parameter. How can I change/control that?

minimal example code

me@here:~/test$ cat argparsetest.py 
import argparse
parser.add_argument('-i', help='input', required=True)

bash completion examples:

# shows all the files 
me@here:~/test$ python argparsetest.py 
argparsetest.py  result.png       u1.py  

# does not show the image result.png I am actually interested in
me@here:~/test$ python argparsetest.py -i
argparsetest.py  u1.py            

There are already two similar questions, but I did not find them helpfull.

Community
  • 1
  • 1
Framester
  • 33,341
  • 51
  • 130
  • 192
  • I cannot reproduce thie behavior. python 2.7.2, bash completion version `1:1.3-1ubuntu6`. Both tab completing with and without an `-i` includes the png file. Also, you're missing a line like `parser = argparse.ArgumentParser(description='do things')` in your minimal code. – JosefAssad May 01 '12 at 16:43

1 Answers1

10

This is not related to argparse or even Python itself. You probably have "programmable bash completion" enabled, and the completion rules are being confused because your command line starts with "python".

The easiest way to work around that is to add to the top of your python file:

#!/usr/bin/env python

, then make the Python script executable:

me@here:~/test$ chmod u+x argparsetest.py 

and then call it directly, without explicitly calling "python":

me@here:~/test$ ./argparsetest.py<TAB>
argparsetest.py  result.png       u1.py  

me@here:~/test$ ./argparsetest.py -i<TAB>
argparsetest.py  result.png       u1.py  

Alternatively, you can turn off bash completion completely with

complete -r

And, if you want to disable it for future session, comment out or remove the lines on either your ~/.bashrc or /etc/bashrc that probably look like this:

if [ -f /etc/bash_completion ]; then
    . /etc/bash_completion
fi
rbp
  • 43,594
  • 3
  • 38
  • 31