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.

classification
Title: argparse ArgumentDefaultsHelpFormatter interacts badly with --arg and nargs=+
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.2, Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: aj, bethard, cheryl.sabella, paul.j3
Priority: normal Keywords:

Created on 2012-08-10 06:06 by aj, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (3)
msg167854 - (view) Author: Alex Jurkiewicz (aj) Date: 2012-08-10 06:06
Sample code:

#!/usr/bin/env python
import argparse

print("\n\narg=foo, nargs=+")
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('foo', nargs='+', help='foos', default=['foo1', 'foo2'])
parser.print_help()

print("\n\narg=foo, nargs=*")
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('foo', nargs='*', help='foos', default=['foo1', 'foo2'])
parser.print_help()

print("\n\narg=--foo, nargs=+")
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('--foo', nargs='+', help='foos', default=['foo1', 'foo2'])
parser.print_help()

print("\n\narg=--foo, nargs=*")
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('--foo', nargs='*', help='foos', default=['foo1', 'foo2'])
parser.print_help()


The first example's help text for 'foo' is missing "(default: ['foo1', 'foo2'])". I tested 2.7.2, 2.7.3, 3.2.3.
msg199951 - (view) Author: paul j3 (paul.j3) * (Python triager) Date: 2013-10-14 21:03
Looks like this behavior is intentional.  This subclass adds a '%(default)s' string to the help lines if:

    if '%(default)' not in action.help:
        if action.default is not SUPPRESS:
            defaulting_nargs = [OPTIONAL, ZERO_OR_MORE]
            if action.option_strings or action.nargs in defaulting_nargs:
                help += ' (default: %(default)s)'

So it adds the default if it is an `optional`, or a `positional` with '*' or '?'.  

A `default` value for a required positional (including '+') does not make sense, since the user input always overwrites the default.  

(I recommend closing this issue).
msg334024 - (view) Author: Cheryl Sabella (cheryl.sabella) * (Python committer) Date: 2019-01-18 23:43
Closing per paul.j3's last comment which also is mentioned in the documentation (https://docs.python.org/3.8/library/argparse.html#default
):

> For positional arguments with nargs equal to ? or *, the default value is used when no command-line argument was present
History
Date User Action Args
2022-04-11 14:57:34adminsetgithub: 59818
2019-01-18 23:43:59cheryl.sabellasetnosy: + cheryl.sabella
messages: + msg334024
2019-01-18 23:32:47cheryl.sabellasetstatus: open -> closed
resolution: not a bug
stage: resolved
2013-10-14 21:04:00paul.j3setnosy: + paul.j3
messages: + msg199951
2013-10-14 14:20:33georg.brandlsetnosy: + bethard
2012-08-10 06:06:52ajcreate