zenlotus / argparse

Automatically exported from code.google.com/p/argparse
Other
0 stars 0 forks source link

`choices=[...]` overrides `nargs='*'` #86

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
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

Original issue reported on code.google.com by andrew.l...@gmail.com on 5 Aug 2012 at 1:47

GoogleCodeExporter commented 9 years ago
Argparse is not maintained here anymore. Please file bugs at 
http://bugs.python.org.

Original comment by steven.b...@gmail.com on 5 Aug 2012 at 7:23