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 Matthijs Kooijman
Recipients Matthijs Kooijman, bethard, paul.j3, porton, rhettinger, zach.ware
Date 2018-08-17.22:13:52
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1534544032.53.0.56676864532.issue34188@psf.upfronthosting.co.za>
In-reply-to
Content
One way to make the original example from Victor work, is to use the following action class:

  class ConvertChoices(argparse.Action):                                                                                                                     
      """                                                                                                                                                    
      Argparse action that interprets the `choices` argument as a dict                                                    
      mapping the user-specified choices values to the resulting option                                                   
      values.                                                                                                                                                
      """                                                                                                                                                    
      def __init__(self, *args, choices, **kwargs):                                                                       
          super().__init__(*args, choices=choices.keys(), **kwargs)                                                       
          self.mapping = choices                                                                                                                             
                                                                                                                                                             
      def __call__(self, parser, namespace, value, option_string=None):                                                   
          setattr(namespace, self.dest, self.mapping[value])
History
Date User Action Args
2018-08-17 22:13:52Matthijs Kooijmansetrecipients: + Matthijs Kooijman, rhettinger, bethard, paul.j3, zach.ware, porton
2018-08-17 22:13:52Matthijs Kooijmansetmessageid: <1534544032.53.0.56676864532.issue34188@psf.upfronthosting.co.za>
2018-08-17 22:13:52Matthijs Kooijmanlinkissue34188 messages
2018-08-17 22:13:52Matthijs Kooijmancreate