title: Python 2.7 argparse: How to nest optional mutally exclusive arguments properly?
body: <p>My Program should include the following options, properly parsed by <a href="http://docs.python.org/2/library/argparse.html" rel="nofollow noreferrer">argparse</a>:</p>
  
  <ol>
  <li>purely optional: <code>[-h, --help]</code> and <code>[-v, --version]</code></li>
  <li>mutually exclusive: <code>[-f FILE, --file FILE]</code> and <code>[-u URL, --url URL]</code></li>
  <li>optional if <code>--url</code> was chosen: <code>[-V, --verbose]</code></li>
  <li>required if either <code>--file</code> or <code>--url</code> was chosen: <code>[-F, --format FORMAT]</code></li>
  </ol>
  
  <p>The desired usage pattern would be:</p>
  
  <pre><code>prog.py [-h] [-v] [-f FILE (-F FORMAT) | -u URL [-V] (-F FORMAT) ]
  </code></pre>
  
  <p>with the <code>-F</code> requirement applying to both members of the mutually exclusive group.<br>
  Not sure if it rather be a <a href="https://stackoverflow.com/questions/15857953/how-to-include-one-positional-argument-into-argparse-mutually-exclusive-group">positional</a>.</p>
  
  <p>So it should be possible to run:</p>
  
  <pre><code>prog.py -u "http://foo.bar" -V -F csv
  </code></pre>
  
  <p>and the parser screaming in case i forgot the <code>-F</code> (as he's supposed to).</p>
  
  <p>What i've done so far:</p>
  
  <pre><code>parser = ArgumentParser(decription='foo')
  
  group = parser.add_mutually_exclusive_group()    
  group.add_argument('-f','--file', nargs=1, type=str, help='')
  group.add_argument('-u','--url', nargs=1, type=str, help='')    
  
  parser.add_argument('-V','--verbose', action='store_true', default=False, help='')
  parser.add_argument('-F','--format', nargs=1, type=str, help='')
  </code></pre>
  
  <p>Since it has a 'vanilla mode' to run without command line arguments, all arguments must be optional.</p>
  
  <p>How can i implement points 3. and 4. into my code?</p>
  
  <p>EDIT:<br>
  I tried <code>-f</code> and <code>-u</code> as subparsers, as described <a href="https://stackoverflow.com/questions/11760578/argparse-arguments-nesting">here</a>, but subcommands seem to be treated like positionals and the parser gives me an <code>error: too few arguments</code> if i run it without arguments.</p>