Reputation: 1571
Suppose you have a python function, as so:
def foo(spam, eggs, ham):
pass
You could call it using the positional arguments only (foo(1, 2, 3)
), but you could also be explicit and say foo(spam=1, eggs=2, ham=3)
, or mix the two (foo(1, 2, ham=3)
).
Is it possible to get the same kind of functionality with argparse? I have a couple of positional arguments with keywords, and I don't want to define all of them when using just one.
Upvotes: 4
Views: 3587
Reputation: 310097
You can do something like this:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('foo',nargs='?',default=argparse.SUPPRESS)
parser.add_argument('--foo',dest='foo',default=None)
parser.add_argument('bar',nargs='?',default=argparse.SUPPRESS)
parser.add_argument('--bar',dest='bar',default=None)
parser.add_argument('baz',nargs='?',default=argparse.SUPPRESS)
parser.add_argument('--baz',dest='baz',default=None)
print parser.parse_args()
which works mostly as you describe:
temp $ python test.py 1 2 --baz=3
Namespace(bar='2', baz='3', foo='1')
temp $ python test.py --baz=3
Namespace(bar=None, baz='3', foo=None)
temp $ python test.py --foo=2 --baz=3
Namespace(bar=None, baz='3', foo='2')
temp $ python test.py 1 2 3
Namespace(bar='2', baz='3', foo='1')
python would give you an error for the next one in the function call analogy, but argparse will allow it:
temp $ python test.py 1 2 3 --foo=27.5
Namespace(bar='2', baz='3', foo='27.5')
You could probably work around that by using mutually exclusive groupings
Upvotes: 6