On 15.05.2013 16:08, Skip Montanaro wrote:
Yes, I was trying that and it sort of works with strings if I use something sufficiently improbable 
like "__UNSELECTED__" as default.  But it gets difficult with boolean or even number 
arguments where you just may not have valid "improbable" defaults.  You could now say, so 
what, it's the default anyway.  But in my program I would like to distinguish between given and not 
given arguments rather than between default and non-default.

Initialize all your arg variables to None, then after command line
processing, any which remain as None weren't set on the command line.
At that point, set them to the actual defaults.  I think that's a
pretty common idiom.

Note: I am an old cranky dude and still use getopt.  This idiom is
pretty easy there.  YMMV with argparse or optparse.

Unfortunately, argparse wants to know the type of the argument and the boolean arguments (those with action=store_true) can't be initialized with None.

However, maybe I could convert boolean arguments to something like

parser.add_argument('--foo', type=str, nargs='?', const='True', default=None)

I'd then have to check for string 'True' rather than for boolean True, though.

Regards,
Henry

--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to