Message179174
In argparse, positional arguments with nargs='*' default to [] rather None, even if default=None is passed explicitly. The documentation says otherwise:
"The default keyword argument of add_argument(), whose value defaults to None, specifies what value should be used if the command-line argument is not present. ... For positional arguments with nargs equal to ? or *, the default value is used when no command-line argument was present:"
(from http://docs.python.org/dev/library/argparse.html#default )
import argparse
def parse(args, **kwargs):
parser = argparse.ArgumentParser()
parser.add_argument('foo', **kwargs)
ns = parser.parse_args(args)
print(repr(ns.foo))
parse([], nargs='?') # None
parse([], nargs='*') # [] <--
parse([], nargs='*', default=None) # [] <--
parse([], nargs='*', default=False) # False
parse([], nargs='*', default=0) # 0
Three options include (there may be more):
(1) document the behavior
(2) make a default of None yield None
(3) do (2), but change the default to [] instead of None when nargs='*' |
|
Date |
User |
Action |
Args |
2013-01-06 09:41:39 | chris.jerdonek | set | recipients:
+ chris.jerdonek, bethard |
2013-01-06 09:41:39 | chris.jerdonek | set | messageid: <1357465299.57.0.35284685768.issue16878@psf.upfronthosting.co.za> |
2013-01-06 09:41:39 | chris.jerdonek | link | issue16878 messages |
2013-01-06 09:41:38 | chris.jerdonek | create | |
|