title: How does argparse (and the deprecated optparse) respond to 'tab' keypress after python program name, in bash?
body: <p>I have tested <a href="http://furius.ca/optcomplete/"><code>optcomplete</code></a> working with the <code>optparse</code> module. Its example is a simple file so I could get that working. I also tested it using the <code>argparse</code> 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 <code>bash</code> together with the <code>shebang</code> line and the <code>argparse</code> (or <code>optparse</code>) module are involved in some way. I have been trying to figure this out (now gonna read the source code).</p>
  
  <p>I have a little more complex program structure, which includes a wrapper around the piece of code which handles the arguments. Its <code>argparse.ArgumentParser()</code> instantiation and calls to <code>add_argument()</code> - which are superclassed into another intermediate module to avoid duplicating code, and wrapper around that is being called - are inside a function.  </p>
  
  <p>I want to understand the way this tab completion works between bash and python (or for that matter any other interpretor like <code>perl</code>).  </p>
  
  <p>NOTE: I have a fair understanding of bash completion (which I learned just now), and I think I understand the bash(only) custom completion.  </p>
  
  <p>NOTE: I have read other similar SO questions, and none really answer this Q.</p>
  
  <p>Edit: <a href="https://hg.furius.ca/public/optcomplete/file/83bf43e3db27/etc/optcomplete.bash">Here</a> is the bash function.<br>
  I already understood how the python module gets to know about words typed in the command line, by reading <code>os.environ</code> values of variables  </p>
  
  <pre><code>$COMP_WORDS
  $COMP_CWORD
  $COMP_LINE
  $COMP_POINT
  $COMPREPLY
  </code></pre>
  
  <p>These variables have values only on tab press.
  My question is how does the python module gets triggered?</p>