I am building a script for users new to Linux, so please understand why I am asking this 🙂
My script runs like this:
python script.py -f filename.txt
I am using the optparse module for this. However, I noticed the following when doing tab completion.
The tab completion works when I do:
python script.py <tab completion> # Tab completion works normally as expected
But it does not work when I do it like this:
python script.py -f <tab completion> # No type of tab completion works here.
I really don’t want my users typing the name of the input file. Tab completion is a must. How can I get it working or what am I doing wrong here?
This is more to do with how bash works than how python works. Experimenting a bit, it looks as if the second and further TAB actually causes bash to expand.
Edit: The probable reason that bash is only expanding the
*.pyand*.pycfiles is because the first word on the line ispython. If you add#! /usr/bin/env pythonto the first line of the python script,chmod +x script.pyand then try./scipt.py -fand tab completion, what happens then? I suspect it’ll work just fine.