argparse csv + choices

Neal Becker ndbecker2 at gmail.com
Thu Mar 31 08:48:13 EDT 2011


Robert Kern wrote:

> On 3/30/11 10:32 AM, Neal Becker wrote:
>> I'm trying to combine 'choices' with a comma-seperated list of options, so I
>> could do e.g.,
>>
>> --cheat=a,b
>>
>>      parser.add_argument ('--cheat', choices=('a','b','c'), type=lambda x:
>> x.split(','), default=[])
>>
>> test.py --cheat a
>>   error: argument --cheat: invalid choice: ['a'] (choose from 'a', 'b', 'c')
>>
>> The validation of choice is failing, because parse returns a list, not an
>> item. Suggestions?
> 
> Do the validation in the type function.
> 
> 
> import argparse
> 
> class ChoiceList(object):
>      def __init__(self, choices):
>          self.choices = choices
> 
>      def __repr__(self):
>          return '%s(%r)' % (type(self).__name__, self.choices)
> 
>      def __call__(self, csv):
>          args = csv.split(',')
>          remainder = sorted(set(args) - set(self.choices))
>          if remainder:
>              raise ValueError("invalid choices: %r (choose from %r)" %
> (remainder, self.choices))
>          return args
> 
> 
> parser = argparse.ArgumentParser()
> parser.add_argument('--cheat', type=ChoiceList(['a','b','c']), default=[])
> print parser.parse_args(['--cheat=a,b'])
> parser.parse_args(['--cheat=a,b,d'])
> 

Excellent!  Thanks!




More information about the Python-list mailing list