2017-12-01 2 views
-1

조건부 명령 줄 인수를 구문 분석하려고합니다. TESTNAME이 명령에 지정되고있는 경우는 CPU와 boradname을 제공하기 위해 compulasary처럼
여기이 코드에서 내 코드중첩 된 명령 줄 파서에서 오류가 발생했습니다. 인수가 너무 적습니다.

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

, 난 파서에 옵션을 추가 할 수 있습니다.

하지만이 코드를 실행하려고하면이 오류를 제공합니다 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 

답변

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의 subparsers에서, 그래서 대신 subparse 명령이 누락 된 것을 불평하지 않아도됩니다 (좋거나 나쁜 경우)
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의 같은 호출은 다른 오류를 제공합니다 서브 파사를 위해 의도 한 문자열을 처리하지 않습니다.

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

당신은 이미 user와 마찬가지로합니다 (gpuboardName 인수에 required을 추가 할 수 있습니다.

POSIX와 argparse에서는 '-u'와 같은 show 옵션 플래그를 사용하고 '--user'와 같은 긴 옵션을 사용하는 것이 바람직합니다. '-user'는 작동하지만 구문 분석은 단일 및 이중 대시로 가장 잘 작동합니다.

parser.add_subparsers 호출에 dest 매개 변수를 추가 할 수 있습니다. (이 선택 사항으로) 나는 여전히 같은 오류를주고있다 TESTNAME를 제공하지 않으면

subparsers에 더

필요 여부

How do you get argparse to choose a default subparser?

+0

.. –

+0

는 py2에 subparser는 – hpaulj

+0

되어 필요 python에서 대체 솔루션 2.7 –

관련 문제