Issue 86 in argparse: `choices=[...]` overrides `nargs='*'`

392 views
Skip to first unread message

argp...@googlecode.com

unread,
Aug 4, 2012, 9:48:02 PM8/4/12
to argpar...@googlegroups.com
Status: New
Owner: ----
Labels: Type-Defect Priority-Medium

New issue 86 by andrew.l...@gmail.com: `choices=[...]` overrides `nargs='*'`
http://code.google.com/p/argparse/issues/detail?id=86

What steps will reproduce the problem?
1. Create an argument with `nargs='*'` and a list of choices
2. Call the script (or call parse_args) with no entry for that argument
e.g.

>>> import argparse
>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('foo', nargs='*', choices=[ 'a', 'b' ])
_StoreAction(option_strings=[], dest='foo', nargs='*', const=None,
default=None, type=None, choices=['a', 'b'], help=None, metavar=None)
>>> parser.parse_args([])
usage: [-h] [{a,b} [{a,b} ...]]
: error: argument foo: invalid choice: [] (choose from 'a', 'b')


I'd expect nargs='*' to always mean "providing nothing at all is a valid
choice," even if I don't explicitly specify "nothing at all" in my choices
array. Appending [] to my choices list works, but it's a surprising bit of
extra work to accomplish something that I thought I'd already handled by
saying `nargs='*'`. Am I misunderstanding how the nargs/choices interaction
is intended to work?

At the very least, "error: argument foo: invalid choice: []" seems
confusing for a user who didn't provide anything at all.

I'm seeing this in argparse 1.1 and 1.2.1 on py2.7//OSX


argp...@googlecode.com

unread,
Aug 5, 2012, 3:23:42 AM8/5/12
to argpar...@googlegroups.com
Updates:
Status: Invalid

Comment #1 on issue 86 by steven.b...@gmail.com: `choices=[...]` overrides
`nargs='*'`
http://code.google.com/p/argparse/issues/detail?id=86

Argparse is not maintained here anymore. Please file bugs at
http://bugs.python.org.

Reply all
Reply to author
Forward
0 new messages