有没有一种方法可以要求一个互斥组,在这个组中有多个选项?

huangapple go评论75阅读模式
英文:

Is there a way to require a mutually exclusive group, where one of the groups has multiple options?

问题

我希望有这样一个行为,要求以下两者之一:

  • --a
  • --b--c

是必需的。如果第二个要求只是 --b,我知道如何做:

group = parser.add_mutually_exclusive_group(required=True)
group.add_argument('--a', type=str)
group.add_argument('--b', type=str)
英文:

I would like a behavior where exactly one of

  • --a
  • --b AND --c

are required. I know how to do it if the second requirement were just --b:

group = parser.add_mutually_exclusive_group(required=True)
group.add_argument('--a', type=str)
group.add_argument('--b', type=str)

答案1

得分: 1

这是我会做的方式:

parser = argparse.ArgumentParser()

# 组1
parser.add_argument("--a", type=str)

# 组2
parser.add_argument("--b", type=str)
parser.add_argument("--c", type=str)

args = parser.parse_args()

if args.a and (args.b or args.c):
    print("-a 和 -b|-c 是互斥的...")
    sys.exit(2)
英文:

Here is how I would do it:

parser = argparse.ArgumentParser()

# group 1 
parser.add_argument("--a", type=str)

# group 2 
parser.add_argument("--b", type=str)
parser.add_argument("--c", type=str)

args = parser.parse_args()

if args.a and (args.b or args.c):
    print("-a and -b|-c are mutually exclusive ...")
    sys.exit(2)

huangapple
  • 本文由 发表于 2023年5月31日 22:39:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76374682.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定