This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author chris.jerdonek
Recipients bethard, chris.jerdonek
Date 2013-01-06.09:41:38
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1357465299.57.0.35284685768.issue16878@psf.upfronthosting.co.za>
In-reply-to
Content
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='*'
History
Date User Action Args
2013-01-06 09:41:39chris.jerdoneksetrecipients: + chris.jerdonek, bethard
2013-01-06 09:41:39chris.jerdoneksetmessageid: <1357465299.57.0.35284685768.issue16878@psf.upfronthosting.co.za>
2013-01-06 09:41:39chris.jerdoneklinkissue16878 messages
2013-01-06 09:41:38chris.jerdonekcreate