Python argparse 不是所有参数都被解析

2024-09-26 17:49:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我执行了这个命令

  ./run.sh -kryder_rate=.9   -tech_kryder_moveout=.10 -kryder_life=5 -total_runs=20  -duration=100 -purchasecost=100 -runCost=66  -horizon=20 -abc=1

我让args对象得到这个(注意,虽然我传递了它们,但它没有得到'abc'和'horizon'

  Namespace(abc=None, deviceType=None, duration='100', horizon=None, kryder_life='5', kryder_rate='.9', purchasecost='100', runCost='66', tech_kryder_moveout='.10')

但是如果我减少参数的数量

   ./run.sh -kryder_rate=.9   -tech_kryder_moveout=.10 -kryder_life=5 -total_runs=20  -duration=100 -horizon=20 -abc=1

然后是“地平线”和“abc”

   Namespace(abc='1', deviceType=None, duration='100', horizon='20', kryder_life='5', kryder_rate='.9', purchasecost=None, runCost=None, tech_kryder_moveout='.10')

要传递的参数数量是否有限制?你知道吗

我搜索过,但谷歌告诉我没有这样的限制。你知道吗

我就是这样分析的

    parser = argparse.ArgumentParser(description='Description of Economic Model Pgm')
    parser.add_argument('-kryder_rate','--kryder_rate', help='kryder_rate', required=False)
    parser.add_argument('-kryder_life','--kryder_life', help='kryder_life', required=False)
    parser.add_argument('-duration','--duration', help='duration', required=False)
    parser.add_argument('-runCost','--runCost', help='runCost', required=False)
    parser.add_argument('-purchasecost','--purchasecost', help='purchasecost', required=False)
    parser.add_argument('-deviceType','--deviceType', help='deviceType', required=False)
    parser.add_argument('-horizon','--horizon', help='horizon', required=False)
    parser.add_argument('-tech_kryder_moveout','-tech_kryder_moveout',help='tech_kryder_moveout',required=False)
    parser.add_argument('-abc','--abc',help='test',required=False)
    args = parser.parse_args()
    print args

Tags: addfalseparserraterequiredhelpargumenttech

热门问题