[New-bugs-announce] [issue16878] argparse: positional args with nargs='*' defaults to []

Chris Jerdonek report at bugs.python.org
Sun Jan 6 10:41:39 CET 2013

New submission from Chris Jerdonek:

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)

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='*'

components: Library (Lib)
messages: 179174
nosy: bethard, chris.jerdonek
priority: normal
severity: normal
status: open
title: argparse: positional args with nargs='*' defaults to []
type: behavior
versions: Python 2.7, Python 3.2, Python 3.3, Python 3.4

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list