I have tested optcomplete working with the optparse module. Its example is a simple file so I could get that working. I also tested it using the argparse module as the prior one is deprecated. But I really do not understand how and by whom the python program gets called on tab presses. I suspect bash together with the shebang line and the argparse (or optparse) module are involved in some way. I have been trying to figure this out (now gonna read the source code).
I have a little more complex program structure, which includes a wrapper around the piece of code which handles the arguments. Its argparse.ArgumentParser() instantiation and calls to add_argument() – which are superclassed into another intermediate module to avoid duplicating code, and wrapper around that is being called – are inside a function.
I want to understand the way this tab completion works between bash and python (or for that matter any other interpretor like perl).
NOTE: I have a fair understanding of bash completion (which I learned just now), and I think I understand the bash(only) custom completion.
NOTE: I have read other similar SO questions, and none really answer this Q.
Edit: Here is the bash function.
I already understood how the python module gets to know about words typed in the command line, by reading os.environ values of variables
$COMP_WORDS
$COMP_CWORD
$COMP_LINE
$COMP_POINT
$COMPREPLY
These variables have values only on tab press.
My question is how does the python module gets triggered?
To understand what’s happening here, let’s check what that bash function actually does:
See the
$1at the end? That means that it actually calls the Python file we want to execute with special environment variables set! To trace what’s happening, let’s prepare a little script to intercept whatoptcomplete.autocompletedoes:This gives us the following when we try to autocomplete it:
So
optcomplete.autocompletejust reads the environment, prepares the matches, writes them to STDOUT and exits. The result-o -h -s -p --script --simple --help --outputis then put into a bash array (COMPREPLY=( ... )) and returned to bash to present the choices to the user. No magic involved 🙂