So I have a python program that reads a config file like this:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-c', '--config', type=str, help='config file', required=True)
args = parser.parse_args()
cfg_filename = args.config
I want to be able to autocomplete paths in bash. For example, suppose the above program is called main.py
and there is a config file called config_1.txt
, structured as follows:
folder/
main.py
configs/
config_1.txt
And suppose in my terminal it looks like
user@host:~/folder$ python main.py -c con
I hope pressing one tab gives:
user@host:~/folder$ python main.py -c config/
and then another tab gives
user@host:~/folder$ python main.py -c config/config_1.txt
I've tried adding
import argcomplete
argcomplete.autocomplete(parser)
but didn't work.
For me using the equals sign ("=") after the argument name got the autocomplete to work and suggest all files matching the input path so far. For example, try:
python main.py -c=<your path>
When I didn't use the "=" sign the autocomplete worked only for folders and files ending with ".py" extension. The possible reason behind could be completion script installed for python. See here for more: https://www.reddit.com/r/bash/comments/b8oqeg/comment/ek1ds7t/?utm_source=share&utm_medium=web2x&context=3