title: Why does argparse order matter with an optional argument?
body: <p>I have some code to parse some arguments in a file called "blah.py":</p>
  
  <pre><code>import argparse
  
  parser = argparse.ArgumentParser()
  
  parser.add_argument('opt', nargs='?')
  parser.add_argument('--bool', action='store_true')
  parser.add_argument('value')
  
  args = parser.parse_args()
  </code></pre>
  
  <p>If I call this file like so:</p>
  
  <pre><code>blah.py a b --bool
  </code></pre>
  
  <p>It works correctly (opt='a', value='b', bool=True).</p>
  
  <p>However, if I call this in a different order:</p>
  
  <pre><code>blah.py a --bool b
  </code></pre>
  
  <p>I would expect the same result, but the argparse module is printing an <code>error: unrecognized arguments: b</code> message.  Is this a bug with the argparse module?  Why does it work this way, and what can I do to avoid running into issues like these, as I define the CLI for my script?</p>
  
  <p>As my interface is much more complex than the example (having subcommands and many more arguments), I have varying ways to call the interface which requires some arguments to be optional for some calls, but mandatory for others.  I plan to manually verify these cases, and on an error call parser.print_usage and exit.</p>