Python argparse: Require two corequisite positional arguments
问题 Using argparse, how do I specify that I want two positional arguments to appear together or not at all? I.e. I want my usage string to look like: Usage: FooBar.py [-h] [FOO BAR] 回答1: As suggested by @hpaulj, here's a solution you could use: In [1]: import argparse In [2]: parser = argparse.ArgumentParser(description="bla bla") In [3]: parser.add_argument("--foo", nargs=2, help="a foo argument") In [4]: parser.parse_args(["--foo", "1", "2"]) Out[4]: Namespace(foo=['1', '2']) In [5]: parser