Determine actually given command line arguments

Skip Montanaro skip at pobox.com
Wed May 15 10:08:12 EDT 2013


> 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.

Skip



More information about the Python-list mailing list