title: Python argparse: nargs='?' and optional arguments
body: <p>Let's say I have a simple program called <code>readfile.py</code> which supports command line arguments by using Python's <code>argparse</code>. </p>
  
  <p>The program reads from a file specified by the positional argument <code>input</code>. Without specifying additional arguments, this program is not very exciting. It <em>just</em> reads from <code>input</code> and exits. The optional argument <code>--output [OUTPUT_FILE]</code> signifies that the input file should be written to a file. <code>OUTPUT_FILE</code> is also optional. If it is not specified, the input should be written to <code>default.out</code>.</p>
  
  <p>I set up my argument parser like this:</p>
  
  <pre><code>parser = argparse.ArgumentParser(description='Read from a file')
  parser.add_argument(
      'input',
      help='file to read from')
  parser.add_argument(
      '--output',
      nargs='?',
      const='default.out',
      default=None,
      help="""write file to %(metavar)s. If %(metavar)s isn't
           specified, write file to %(const)s.""",
      metavar='OUTPUT_FILE')
  args = parser.parse_args()
  return args.file, args.output_file
  </code></pre>
  
  <p>Note I use <code>default=None</code> so that if <code>--ouput</code> doesn't appear on the command line, I can detect its absence with None.</p>
  
  <p>Which gives a usage signature like this:</p>
  
  <pre><code>usage: readfile.py [-h] [--output [OUTPUT_FILE]] input
  </code></pre>
  
  <p>This handles arguments as expected if I run</p>
  
  <pre><code>python readfile.py input.in --output somefile.out
  </code></pre>
  
  <p>or</p>
  
  <pre><code>python readfile.py --output somefile.out input
  </code></pre>
  
  <p>It sets <code>output</code> to <code>default.out</code> if I run</p>
  
  <pre><code>python readfile.py input.in --output
  </code></pre>
  
  <p>but if I run</p>
  
  <pre><code>python readfile.py --output input.in
  </code></pre>
  
  <p>it complains about there being too few arguments. I thought argparse would be 'smart' enough to interpret this pattern, setting <code>input</code> to <code>input.in</code> and <code>output</code> to <code>default.out</code>. The generated usage even suggests this. </p>
  
  <p>Am I missing something?</p>