Bash tab completion with argparse does not show all the files in the directory

argparse, bash, python, shell

Solution

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

Problem

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. How does argparse (and the deprecated optparse) respond to 'tab' keypress after python program name, in bash? Python argparse and bash completion

Original source

Related problems