This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author paul.j3
Recipients Sworddragon, paul.j3, r.david.murray, terry.reedy
Date 2015-09-04.20:53:19
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1441400000.15.0.650703532116.issue24956@psf.upfronthosting.co.za>
In-reply-to
Content
A related issue which Sworddragon found just before starting this issue is

    http://bugs.python.org/issue9625

The handling of defaults in argparse is a bit complicated.  In general it errs on the side of giving the user/developer freedom to set them how ever they want.  That's especially true in the case of non-string defaults.  A default does not have to be something that the user could give.  In particular the default 'default' is None, which can't be parsed from the command line.  The same for boolean values (the default 'type' does not translate string 'False' to boolean 'False'.)

Errors in the defaults are usually caught during program development.  

If the calling code generates the defaults dynamically, it seems reasonable that it should also check that they are valid.  The validity test for choices is a simple 'in' (contains) one.  'choices' can be a list or dictionary (keys), and can even be dynamically generated.

     parser = ....
     choices = <get a list>
     default = <get a value>
     if default not in choices:
         <scream>
     parser.add_argument(..., choices=choices, default=default)
     etc.
History
Date User Action Args
2015-09-04 20:53:20paul.j3setrecipients: + paul.j3, terry.reedy, r.david.murray, Sworddragon
2015-09-04 20:53:20paul.j3setmessageid: <1441400000.15.0.650703532116.issue24956@psf.upfronthosting.co.za>
2015-09-04 20:53:20paul.j3linkissue24956 messages
2015-09-04 20:53:19paul.j3create