Message245099
Action append_const works for options:
>>> import argparse
>>>
>>> parser = argparse.ArgumentParser()
>>> _ = parser.add_argument('--foo', dest='x', action='append_const', const=42)
>>> _ = parser.add_argument('--bar', dest='x', action='append_const', const=43)
>>> parser.parse_args('--foo --bar'.split())
Namespace(x=[42, 43])
>>>
Action append_const works for single positionals:
>>> import argparse
>>>
>>> parser = argparse.ArgumentParser()
>>> _ = parser.add_argument('foo', action='append_const', const=42)
>>> _ = parser.add_argument('bar', action='append_const', const=43)
>>> parser.parse_args([])
Namespace(bar=[43], foo=[42])
>>>
Action append_const doesn't work for positionals in one list:
>>> import argparse
>>>
>>> parser = argparse.ArgumentParser()
>>> _ = parser.add_argument('foo', dest='x', action='append_const', const=42)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.3/site-packages/argparse-1.1-py3.3.egg/argparse.py", line 1282, in add_argument
"""
ValueError: dest supplied twice for positional argument
>>> _ = parser.add_argument('bar', dest='x', action='append_const', const=43)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.3/site-packages/argparse-1.1-py3.3.egg/argparse.py", line 1282, in add_argument
"""
ValueError: dest supplied twice for positional argument
>>> parser.parse_args([])
Namespace()
>>>
The reason is that a positional argument can't accept dest:
>>> import argparse
>>>
>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('foo', dest='x')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.3/site-packages/argparse-1.1-py3.3.egg/argparse.py", line 1282, in add_argument
"""
ValueError: dest supplied twice for positional argument
>>> |
|
Date |
User |
Action |
Args |
2015-06-09 23:05:09 | py.user | set | recipients:
+ py.user |
2015-06-09 23:05:09 | py.user | set | messageid: <1433891109.51.0.989129061245.issue24419@psf.upfronthosting.co.za> |
2015-06-09 23:05:09 | py.user | link | issue24419 messages |
2015-06-09 23:05:09 | py.user | create | |
|