Nested command line parser gives error: too few arguments
Nested command line parser gives error: too few arguments
我正在尝试解析条件命令行参数。
这是我的代码
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,那么必须提供 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
-testName=xyz
是调用子解析器的错误方法。
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 中不需要子解析器,因此与其抱怨缺少 subparse 命令,不如抱怨它无法处理您为子解析器准备的字符串。
如果我使用 testName
(不带 -)(并打印参数),我得到:
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
添加到 gpu
和 boardName
参数(正如您已经对 user
.
所做的那样
在 POSIX 和 argparse
中,首选使用显示选项标志,如“-u”和长选项标志,如“--user”。 '-user' 有效,但解析最适合单破折号和双破折号的区别。
您可能需要向 parser.add_subparsers
调用添加一个 dest
参数。
关于子解析器的更多信息,是否需要
我正在尝试解析条件命令行参数。
这是我的代码
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,那么必须提供 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
-testName=xyz
是调用子解析器的错误方法。
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 中不需要子解析器,因此与其抱怨缺少 subparse 命令,不如抱怨它无法处理您为子解析器准备的字符串。
如果我使用 testName
(不带 -)(并打印参数),我得到:
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
添加到 gpu
和 boardName
参数(正如您已经对 user
.
在 POSIX 和 argparse
中,首选使用显示选项标志,如“-u”和长选项标志,如“--user”。 '-user' 有效,但解析最适合单破折号和双破折号的区别。
您可能需要向 parser.add_subparsers
调用添加一个 dest
参数。
关于子解析器的更多信息,是否需要