嵌套命令行解析器给出错误:参数太少

时间:2017-12-01 09:18:25

标签: python parsing argparse

我正在尝试解析条件命令行参数 这是我的代码

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()

从这段代码中,我想在解析器中添加选项,如果在命令中给出了testName,那么它就是compulasary来提供cpu和boradname。

但是当我尝试运行此代码时,它会给出错误:parser.py:错误:参数太少

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

1 个答案:

答案 0 :(得分:0)

-testName=xyz是调用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

此Python2错误意味着缺少一个或多个必需参数。但它并没有告诉我们缺少什么。用法表示需要-configFile-user{testName}。它们不在括号中。你有2个,但有-testName而不是最后一个。

Python3中的相同调用给出了不同的错误:

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

(好的或坏的)在Python3中,不需要subparsers,所以不要抱怨subparse命令丢失,而是抱怨它无法处理你想要用于subparser的字符串。

如果我使用testName(没有 - )(并打印args),我得到:

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')

您可能希望将required添加到gpuboardName参数中(正如您对user所做的那样。

在POSIX和argparse中,最好使用显示选项标记,例如' -u'和长期的人一样 - 用户' ' -user'可行,但解析最适合单点和双点划分。

您可能需要在dest来电中添加parser.add_subparsers参数。

有关subparsers的更多信息,无论是否需要

How do you get argparse to choose a default subparser?