Export to GitHub

argparse - issue #5

Provide a way to have default= values parsed from strings


Posted on Mar 28, 2009 by Grumpy Rabbit

Sometimes it would be nice to be able to supply defaults as strings, and have the ArgumentParser parse them. For example, if you had a matrix type, that converted "zero" to, say, [[0, 0, 0], [0, 0, 0], [0, 0, 0]], then you might want your default value to still be "zero" for the purposes of help, so you'd like to write::

parser.add_argument(..., default="zero", ...)

Right now, defaults are not parsed, so this would fail. It would be nice if you could supply, say, parse_defaults=True to the ArgumentParser constructor to make this work.

Comment #1

Posted on Mar 29, 2009 by Grumpy Rabbit

I think this isn't actually an issue - type conversion is done for any string argument value, even if that value came from the default::

def simple_list(string): ... return string.split(",") ... parser = argparse.ArgumentParser() parser.add_argument('foo', nargs='?', type=simple_list, default="a,b,c") parser.parse_args([]) Namespace(foo=['a', 'b', 'c'])

So basically, this should already work.

Status: Invalid

Labels:
Type-Enhancement Priority-Medium