Irit Katriel added the comment:
We could make the error message less wrong:
>>> p.parse_args(['d'])
usage: [-h] {a,b,c}
: error: argument a: invalid choice: 'd' (choose a value in 'abc')
% git diff
diff --git a/Lib/argparse.py b/Lib/argparse.py
index b44fa4f0f6..f03cc1f110 100644
---
paul j3 added the comment:
I just posted a patch to http://bugs.python.org/issue16468 that deals with this
'bc' in 'abc' issue.
--
___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue16977
___
paul j3 added the comment:
Changing _check_value from:
def _check_value(self, action, value):
# converted value must be one of the choices (if specified)
if action.choices is not None and value not in action.choices:
...
to
def _check_value(self, action,
paul j3 added the comment:
test_argparse.py has some choices='abc' cases.
In those should parser.parse_args(['--foo','bc']) be considered a success or
failure?
The underlying issue here is that while string iteration behaves like list
iteration, string __contains__ looks for substrings,
Chris Jerdonek added the comment:
There are also test cases with a string being passed for choices.
--
___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue16977
___
Jeff Knupp added the comment:
Attached a patch. Rather than altering choices or making a special check for
string instances, I just changed the if statement to
if action.choices is not None and value not in list(action.choices):
from
if action.choices is not None and value not in
Chris Jerdonek added the comment:
See issue 16468 for why that won't work in general.
--
___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue16977
___
Jeff Knupp added the comment:
The only time this would be an issue is for infinite sequences via range or a
generator, which doesn't work anyway.
p = argparse.ArgumentParser()
a = p.add_argument('a', choices=itertools.count(0), type=int)
p.parse_args(['1'])
... hangs
Are there any
Chris Jerdonek added the comment:
When choices isn't iterable but supports the in operator, e.g.
class MyChoices(object):
def __contains__(self, item):
return True
--
___
Python tracker rep...@bugs.python.org
Eric V. Smith added the comment:
Isn't this really just an inappropriate use of a string instead of a list? If
indeed this is in the documentation, it should be changed.
I still don't like:
p.add_argument('a', choices=list('abc'))
but at least it would work.
This call to list() could be done
Chris Jerdonek added the comment:
This wasn't just in the documentation. It was the *first* example of how to
use the choices argument out of the two examples in that section (from the time
Python first adopted argparse and before):
16.4.3.7. choices
Some command-line arguments should
New submission from Chris Jerdonek:
When passing a string for the choices argument, argparse's usage and error
messages differ from its behavior:
p = argparse.ArgumentParser()
p.add_argument('a', choices='abc')
p.parse_args(['d'])
usage: [-h] {a,b,c}
: error: argument a: invalid choice: 'd'
Chris Jerdonek added the comment:
I forgot to mention that argparse uses such cases as examples in its
documentation (one of which was replaced in bddbaaf332d7).
--
___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue16977
13 matches
Mail list logo