Require either of two arguments using argparse
问题 Given: import argparse pa = argparse.ArgumentParser() pa.add_argument('--foo') pa.add_argument('--bar') print pa.parse_args('--foo 1'.split()) how do I make at least one of "foo, bar" mandatory: --foo x , --bar y and --foo x --bar y are fine make at most one of "foo, bar" mandatory: --foo x or --bar y are fine, --foo x --bar y is not 回答1: I think you are searching for something like mutual exclusion (at least for the second part of your question). This way, only foo or bar will be accepted,