Python argparse conditional arguments

不打扰是莪最后的温柔 提交于 2020-01-05 03:34:25

问题


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


来源:https://stackoverflow.com/questions/15078181/python-argparse-conditional-arguments

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