Require either of two arguments using argparse

后端 未结 2 1839
时光说笑
时光说笑 2021-01-29 22:43

Given:

import argparse

pa = argparse.ArgumentParser()
pa.add_argument(\'--foo\')
pa.add_argument(\'--bar\')

print pa.parse_args(\'--foo 1\'.split())

2条回答
  •  遇见更好的自我
    2021-01-29 23:01

    If you need some check that is not provided by the module you can always do it manually:

    pa = argparse.ArgumentParser()
    ...
    args = pa.parse_args()
    
    if args.foo is None and args.bar is None:
       pa.error("at least one of --foo and --bar required")
    

提交回复
热议问题