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 thesociable
Recipients thesociable
Date 2010-08-17.09:19:02
SpamBayes Score 1.5607127e-11
Marked as misclassified No
Message-id <1282036744.37.0.964764969496.issue9625@psf.upfronthosting.co.za>
In-reply-to
Content
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
History
Date User Action Args
2010-08-17 09:19:04thesociablesetrecipients: + thesociable
2010-08-17 09:19:04thesociablesetmessageid: <1282036744.37.0.964764969496.issue9625@psf.upfronthosting.co.za>
2010-08-17 09:19:03thesociablelinkissue9625 messages
2010-08-17 09:19:02thesociablecreate