【发布时间】:2013-03-16 16:27:36
【问题描述】:
我正在编写一个类似的程序:
import argparse
def task1(args):
print "running task 1"
def task2(args):
print "running task 2"
if __name__=="__main__":
parser=argparse.ArgumentParser(description="How can I have mutually exclusive groups in subparsers?")
subparsers=parser.add_subparsers()
t1sub=subparsers.add_parser("task1")
#....
t1sub.set_defaults(func=task1)
# here I would like to have a mutually exclusive group
# when task 1 of the option one between --in and --out is required, but one excludes the other
# apparently a subparser has no add_group() not add_mutually_exclusive_group(), though
t2sub=subparsers.add_parser("task2")
#....
t1sub.set_defaults(func=task2)
args = parser.parse_args()
args.func(args)
正如我在运行 task1 时所解释的那样,--in 或 --out 之间的一个是必需的,但不是两者都需要。
如何将此功能添加到我的程序中??
【问题讨论】:
标签: python python-2.7 argparse mutual-exclusion