Message114108
Variable argument count plays badly with choices.
Example:
========
>>> import argparse
>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('choices', nargs='*', default='a', choices=['a',
'b', 'c'])
>>> args = parser.parse_args()
>>> print type(args.choices)
<type 'str'>
>>> args = parser.parse_args(['a'])
>>> print type(args.choices)
<type 'list'>
If the user specifies the value on the command line then a list is used, but if the value comes from the default a string is used.
Unfortunately, changing default to a list value gives an error:
error: argument choices: invalid choice: ['a'] (choose from 'a', 'b',
'c')
Additionally, this means it is also not possible to use default=['a', 'c'].
The current workaround is to create a custom type:
def my_type(string):
if string not in ['a', 'b', 'c']:
raise TypeError
return string |
|
Date |
User |
Action |
Args |
2010-08-17 09:19:04 | thesociable | set | recipients:
+ thesociable |
2010-08-17 09:19:04 | thesociable | set | messageid: <1282036744.37.0.964764969496.issue9625@psf.upfronthosting.co.za> |
2010-08-17 09:19:03 | thesociable | link | issue9625 messages |
2010-08-17 09:19:02 | thesociable | create | |
|