Python argparse conditional arguments

2019-08-01 08:43发布

问题:

I am creating a program that requires conditional arguments using argparse. I would like to generate new arguments in my code depending on if a previous argument has been entered or not. Here is a basic example of how I would like my code to look

import argparse
parser = argparse.ArgumentParser()

parser.add_argument("-bowtie",action = "store_true",help="use to run bowtie")
args = parser.parse_args()

if args.bowtie:
    parser.add_argument( add some new argument here )
    args = parser.parse_args()

回答1:

I've been doing a lot of stuff recently that's really similar to this. Look into subparsers:

parser = argparser.ArgumentParser
subparsers = parser.add_subparsers('-bowtie')
subparser = subparsers.add_parser()
subparser.add_argument('new argument')