Enchancement suggestion for argparse: intuit type from default

Ulrich Eckhardt ulrich.eckhardt at dominolaser.com
Wed Mar 14 13:26:14 CET 2012


Am 13.03.2012 22:08, schrieb Roy Smith:
> Using argparse, if I write:
>
>      parser.add_argument('--foo', default=100)
>
> it seems like it should be able to intuit that the type of foo should
> be int (i.e. type(default)) without my having to write:
>
>      parser.add_argument('--foo', type=int, default=100)
>
> Does this seem like a reasonable enhancement to argparse?

The following would turn into an error:

   # in foo.py:
   p.add_argument('--offset', 0)

   # calling foo.py:
   foo.py --offset 1.5

OTOH, this would be obvious even from halfway serious testing, so I'm +1 
for making this change. Have you looked at existing use of this and 
where it would break anything? When the argument doesn't match the type, 
is the error message sufficiently understandable?

Uli



More information about the Python-list mailing list