title: argparse: How can I allow multiple values to override a default
body: <p>This is slightly related to the topic covered in a <a href="https://stackoverflow.com/questions/5176846/why-does-argparse-give-me-a-list-in-a-list">question about allowing an argument to be specified multiple times</a>.</p>
  
  <p><strong>I'd like to be able to specify an option multiple times like this:</strong></p>
  
  <pre><code> tool --foo 1 --foo 2 --foo 3
  </code></pre>
  
  <p><strong>And also like this:</strong></p>
  
  <pre><code> tool a b c
  </code></pre>
  
  <p>I'd also like to support both at the same time:</p>
  
  <pre><code> tool a b c --foo 1 --foo2 --foo 3
  </code></pre>
  
  <p>This works fine with:</p>
  
  <pre><code>import argparse
  parser = argparse.ArgumentParser()
  parser.add_argument('foo', nargs='*', action='append')
  parser.add_argument('--foo', nargs='*', dest='foo', action='append')
  </code></pre>
  
  <p>The result list can be easily flattened out:</p>
  
  <pre><code>args = parser.parse_args('a b c --foo 1 --foo 2 --foo 3'.split())
  args.foo = [el for elements in args.foo for el in elements]
  </code></pre>
  
  <p>yields:</p>
  
  <pre><code>&gt;&gt;&gt; args
  Namespace(foo=['a', 'b', 'c', '1', '2', '3'])
  </code></pre>
  
  <p><strong>How do I add a default value in a way that the default is not being used as soon as one argument is specified by the user?</strong></p>
  
  <p>If adding just <code>default=[['spam']]</code> to one of the <code>add_argument()</code> calls, the default is always part of the result. I cannot get argparse to remove it by itself as soon as a user provides an argument herself.</p>
  
  <p>I'm hoping that there's a solution with what <code>argparse</code> already provides itself.</p>