zenlotus / argparse

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

'choice' arguments should support default values #57

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?

Currently this is the behavior of argparse:

>>> import argparse
>>> parser = argparse.ArgumentParser(prog='PROG')
>>> parser.add_argument('foo', choices='abc', default='a')
_StoreAction(option_strings=[], dest='foo', nargs=None, const=None,
default='a', type=None, choices='abc', help=None, metavar=None)

If a value is given and it's valid, it is stored in foo:
>>> parser.parse_args('c'.split())
Namespace(foo='c')

What is the expected output? What do you see instead?

But if no value is given, an error is generated:
>>> parser.parse_args(''.split())
usage: PROG [-h] {a,b,c}
PROG: error: too few arguments

It would be great if instead we got

Namespace(foo='a')

since 'a' was declared as the default value for 'foo'.

What version of the product are you using? On what operating system?
argparse from svn, on linux.

Original issue reported on code.google.com by fperez....@gmail.com on 15 Jan 2010 at 1:01

GoogleCodeExporter commented 9 years ago
You're getting that error because your 'foo' argument is required, not 
optional. If
you want it to be optional, you need to do something like:

>>> parser = argparse.ArgumentParser(prog='PROG')
>>> parser.add_argument('foo', nargs='?', choices='abc', default='a')
>>> parser.parse_args([])
Namespace(foo='a')

Original comment by steven.b...@gmail.com on 31 Jan 2010 at 10:38

GoogleCodeExporter commented 9 years ago

Original comment by steven.b...@gmail.com on 28 Feb 2010 at 10:11