-1

I am trying to parse conditional command line arguments.
Here is my code

import argparse
def parseArguments():
    parser = argparse.ArgumentParser(description="Parses the command line arguments")
    parser.add_argument('-launchTest', dest='launchTest', action='store_true', help='provide this to run triage process of test suites')
    parser.add_argument('-getStatus', dest='getStatus', action='store_true', help='provide this to run triage process of test suites')
    parser.add_argument('-configFile', dest='configFile', required=True, help='provide json config data')
    parser.add_argument('-user', dest='user', required=True, action='store', help='provide user name for launching tests on GVS')
    parser.add_argument('-date', dest='date' , help='provide date or CL to run tests, date format: MM_DD_YYYY')
    parser.add_argument('-cl', dest='cl', help='provide either date or cl to run the tests')
    parser.add_argument('-outPutDir', dest='outPutDir', default='/compune-nightly/nightly/{}/GVS-Tegra/', help='provide output directory path to store results')

    subparser = parser.add_subparsers(help='sub-command help')
    parser_a = subparser.add_parser('testName', help='this to run specific test with testName and test details must be present in argumentConfig.json provide gpu and boardName with this')
    parser_a.add_argument('-gpu', action='store', help='provide this to run specific test with testName, testType and test details must be present in argumentConfig.json')
    parser_a.add_argument('-boardName', action='store', help='provide this to run specific test with testName, testType, gpu and test details must be present in argumentConfig.json')
    arguments = parser.parse_args()
    return arguments

def main():
    parseArguments()

main()

From this code, I want to add option in parser like if testName is given in command then it is compulasary to provide cpu and boradname.

But When I am trying to run this code it gives Error: parser.py: error: too few arguments

python parser.py -configFile=abcd -user=amanj -testName=xyz -gpu=123 -boardName=123 
usage: parser.py [-h] [-launchTest] [-getStatus] -configFile CONFIGFILE -user
                 USER [-date DATE] [-cl CL] [-outPutDir OUTPUTDIR]
                 {testName} ... parser.py: error: too few arguments
quamrana
  • 37,849
  • 12
  • 53
  • 71
Aman Jaiswal
  • 1,084
  • 2
  • 18
  • 36

1 Answers1

0

-testName=xyz is the wrong way to invoke the subparser.

1018:~/mypy$ python2 stack47590105.py -configFile=abcd -user=amanj -testName=xyz -gpu=123 -boardName=123 
usage: stack47590105.py [-h] [-launchTest] [-getStatus] -configFile CONFIGFILE
                        -user USER [-date DATE] [-cl CL]
                        [-outPutDir OUTPUTDIR]
                        {testName} ...
stack47590105.py: error: too few arguments

This Python2 error means that one or more required arguments are missing. But it doesn't tell us what is missing. Usage indicates that -configFile, -user, and {testName} are required. They aren't in brackets. You have 2 of those, but have -testName instead of the last.

The same call in Python3 gives a different error:

1018:~/mypy$ python3 stack47590105.py -configFile=abcd -user=amanj -testName=xyz -gpu=123 -boardName=123 
usage: stack47590105.py [-h] [-launchTest] [-getStatus] -configFile CONFIGFILE
                        -user USER [-date DATE] [-cl CL]
                        [-outPutDir OUTPUTDIR]
                        {testName} ...
stack47590105.py: error: unrecognized arguments: -testName=xyz -gpu=123 -boardName=123

(for good or bad) In Python3 subparsers aren't required, so instead of complaining that the subparse command is missing it complains that it can't handle the strings you intended for the subparser.

If I use testName (without the the -) (and print the args) I get:

1019:~/mypy$ python2 stack47590105.py -configFile=abcd -user=amanj testName -gpu=123 -boardName=123 
Namespace(boardName='123', cl=None, configFile='abcd', date=None, getStatus=False, gpu='123', launchTest=False, outPutDir='/compune-nightly/nightly/{}/GVS-Tegra/', user='amanj')

You may want to add required to the gpu and boardName arguments (as you already do with user.

In POSIX and argparse it is preferred to use show option flags like '-u' and long ones like '--user'. '-user' works, but the parsing works best with the single and double dash distinction.

You may want to add a dest parameter to the parser.add_subparsers call.

More on subparsers, required or not

How do you get argparse to choose a default subparser?

hpaulj
  • 221,503
  • 14
  • 230
  • 353