zenlotus / argparse

Automatically exported from code.google.com/p/argparse
Other
0 stars 0 forks source link

Using default values with the append action #1

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
[Originally submitted by risto.kankkunen@iki.fi to python-hosting.org tracker]

The default behavior for options with append action is not very convenient:

>>> import argparse
>>> parser = argparse.ArgumentParser()
>>> parser.add_argument("--foo", action='append')
>>> parser.parse_args(('--foo=1', '--foo=2'))
Namespace(foo=['1', '2'])
>>> parser.parse_args(())
Namespace(foo=None)

You would expect to get zero or more items in a list for options having an
append action. Now you get either None or one or more items in a list.

Giving a default value to fix this doesn't work:

>>> import argparse
>>> parser = argparse.ArgumentParser()
>>> parser.add_argument("--foo", action='append', default=[])
>>> parser.parse_args(())
Namespace(foo=[])
>>> parser.parse_args(('--foo=1', '--foo=2'))
Namespace(foo=['1', '2'])
>>> parser.parse_args(())
Namespace(foo=['1', '2'])
>>> parser.parse_args(('--foo=1', '--foo=2'))
Namespace(foo=['1', '2', '1', '2'])

This also applies if you want to have any other default value: if the
default is mutable, the default gets mutated. If the default is immutable,
you get an exception.

Original issue reported on code.google.com by steven.b...@gmail.com on 27 Mar 2009 at 4:59

GoogleCodeExporter commented 9 years ago
I'm curious what your use case is that requires calling ``parse_args`` more than
once. Could you elaborate?

Original comment by steven.b...@gmail.com on 28 Mar 2009 at 6:28

GoogleCodeExporter commented 9 years ago
The use case is:

"I needed to parse the command line twice, because I wanted to combine options 
from
the command line and from a configuration file: I parse the command line and 
check if
there is a '--config=FILE' option. If there is, I parse the config file and call
set_defaults() for each item there. This way I get the same error checking etc. 
for
free. After this I need of course to re-parse the command line so that its 
options
override the ones from the config file."

The fix, I believe should be to rewrite _AppendAction and _AppendConstAction to 
do
something like:

    items = _ensure_value(namespace, self.dest, []) + [values]
    setattr(namespace, self.dest, items)

instead of the current call to append.

Original comment by steven.b...@gmail.com on 29 Apr 2009 at 6:15

GoogleCodeExporter commented 9 years ago
All append actions copy their destination list as of r21.

Original comment by steven.b...@gmail.com on 4 May 2009 at 5:22

GoogleCodeExporter commented 9 years ago
Not sure why this was marked as fixed. argparse 1.2.1 still has this issue.

Original comment by si...@hova.net on 7 Oct 2011 at 12:57

GoogleCodeExporter commented 9 years ago
The issue that was fixed was that if a default is supplied, it is copied so 
that multiple calls to parse_args work correctly.

I assume you're asking for the default for the "append" action to be an empty 
list? If so, please open a new issue for that, at the argparse bug tracker, 
which is the Python bug tracker: http://bugs.python.org/ Bugs reported to the 
Google Code tracker are likely to be lost.

Original comment by steven.b...@gmail.com on 15 Dec 2011 at 1:21