[New-bugs-announce] [issue24419] In argparse action append_const doesn't work for positional arguments

py.user report at bugs.python.org
Wed Jun 10 01:05:09 CEST 2015


New submission from py.user:

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
>>>

----------
components: Library (Lib)
messages: 245099
nosy: py.user
priority: normal
severity: normal
status: open
title: In argparse action append_const doesn't work for positional arguments
type: behavior
versions: Python 3.3, Python 3.6

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue24419>
_______________________________________


More information about the New-bugs-announce mailing list