title: How to code argparse combinational options in python
body: <p>I have been troubled with this small piece of activity to be completed. I did do some experiment, but was not able to achieve the result.</p>
  
  <p><strong>Requirement:</strong>   </p>
  
  <pre><code>   test2.py [-c/-v] -f
  </code></pre>
  
  <p><strong>Usage or Rules:</strong></p>
  
  <ol>
  <li><p>-c (compare) takes 2 parameter.</p>
  
  <p>-v (verify) takes 1 parameter. </p>
  
  <p><em>Either</em> of these two must be present, <em>but not both</em>.  </p></li>
  <li>-f is a mandatory parameter (output file name).  </li>
  </ol>
  
  <p><strong>Output:</strong>  </p>
  
  <p>I am able to get the desired output as shown below</p>
  
  <pre><code>kp@kp:~/Study/scripts$ ./test.py -c P1 P2 -f p
  kp@kp:~/Study/scripts$ ./test.py -v P1 -f p
  kp@kp:~/Study/scripts$ ./test.py -v P1 
  usage: test.py &lt;functional argument&gt; &lt;ouput target argument&gt;
  test.py: error: argument -f/--file is required
  kp@kp:~/Study/scripts$ ./test.py -c P1 P2 
  usage: test.py &lt;functional argument&gt; &lt;ouput target argument&gt;
  test.py: error: argument -f/--file is required
  kp@kp:~/Study/scripts$ 
  </code></pre>
  
  <p><strong>Problem is:</strong></p>
  
  <p>When you use, <code>test.py -h</code>,<br>
  1. <strong>The output will not indicate that -c/-v <em>either of them is mandatory but not both</em></strong> . It indicates all the arguments are optional.<br>
  2. The output will indicate -f option under optional arguments which is incorrect. -f is mandatory argument, and I want to display outside - optional arguments.  </p>
  
  <p>How to change the script so that -h option output will be more user friendly (without any external validation)</p>
  
  <pre><code>usage: test.py &lt;functional argument&gt; &lt;ouput target argument&gt;
  
  Package Compare/Verifier tool.
  
  optional arguments:
    -h, --help            show this help message and exit
    -f outFileName, --file outFileName
                          File Name where result is stored.
    -c Package1 Package2, --compare Package1 Package2
                          Compare two packages.
    -v Package, --verify Package
                          Verify Content of package.
  kiran@kiran-laptop:~/Study/scripts$ 
  </code></pre>
  
  <p><strong>Code:</strong></p>
  
  <p>I am using the below code to achieve the output,</p>
  
  <pre><code>#!/usr/bin/python
  
  import sys
  import argparse
  
  def main():
      usage='%(prog)s &lt;functional argument&gt; &lt;ouput target argument&gt;'
      description='Package Compare/Verifier tool.'
      parser = argparse.ArgumentParser(usage=usage,description=description)
  
      parser.add_argument('-f','--file',action='store',nargs=1,dest='outFileName',help='File Name where result is stored.',metavar="outFileName",required=True)
  
  
      group = parser.add_mutually_exclusive_group(required=True)
      group.add_argument('-c','--compare',action='store',nargs=2,dest='packageInfo',help='Compare two packages.',metavar=("Package1","Package2"))
      group.add_argument('-v','--verify',action='store',nargs=1,dest='packageName',help='Verify Content of package.',metavar='Package')
      args = parser.parse_args()
  
  if __name__ == "__main__":
      main()
  </code></pre>