Mandatory sub-arguments for particular value of co

2019-08-28 04:05发布

问题:

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