0

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]
macdjord
  • 405
  • 3
  • 11

1 Answers1

2

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.parse_args([])
Out[5]: Namespace(foo=None)

In [6]: parser.print_help()
usage: ipython [-h] [--foo FOO FOO]

bla bla

optional arguments:
  -h, --help     show this help message and exit
  --foo FOO FOO  a foo argument
elena
  • 2,820
  • 4
  • 19
  • 32