Mandatory sub-arguments for particular value of conditional arguments -Python 2.7

半世苍凉 提交于 2019-12-12 00:03:38

问题


I need to parse additional arguments depending on the value of a conditional argument ('--name'). I don't want to handle it with 'if statements'.

import argparse
parser = argparse.ArgumentParser()

parser.add_argument("--name", required = True, choices = ['a','b','c'])
args = parser.parse_args()

if args.name== 'a':
    parser.add_argument( add some mandatory arguments here )

if args.name== 'b':
    parser.add_argument( add some mandatory arguments here )

if args.name== 'c':
    parser.add_argument( add some mandatory arguments here )

args = parser.parse_args()

I got a similar question on this Python argparse conditional arguments but the solution doesn't help me.

Note: I'm using python 2.7

来源:https://stackoverflow.com/questions/52857603/mandatory-sub-arguments-for-particular-value-of-conditional-arguments-python-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!