title: argparse -- requiring either 2 values or none for an optional argument
body: <p>I'm trying to make an optional argument for a script that can either take no values or 2 values, nothing else. Can you accomplish this using argparse?</p>
  
  <pre><code># desired output:
  # ./script.py -a --&gt; works
  # ./script.py -a val1 --&gt; error
  # ./script.py -a val1 val2 --&gt; works
  </code></pre>
  
  <p><br/>
  version 1 -- accepts 0 or 1 values:</p>
  
  <pre><code>parser = argparse.ArgumentParser()
  parser.add_argument("-a", "--action", nargs="?", const=True, action="store", help="do some action")
  args = parser.parse_args()
  
  # output:
  # ./script.py -a --&gt; works
  # ./script.py -a val1 --&gt; works
  # ./script.py -a val1 val2 --&gt; error
  </code></pre>
  
  <p><br/>
  version 2 - accepts exactly 2 values:</p>
  
  <pre><code>parser = argparse.ArgumentParser()
  parser.add_argument("-a", "--action", nargs=2, action="store", help="do some action")
  args = parser.parse_args()
  
  # output:
  # ./script.py -a --&gt; error
  # ./script.py -a val1 --&gt; error
  # ./script.py -a val1 val2 --&gt; works
  </code></pre>
  
  <p><br/>
  How do you combine these 2 different versions so that the script accepts 0 or 2 values for the argument, but rejects it when it only has 1 value?</p>