Export to GitHub

argparse - issue #7

Allow a callable for the 'choices' keyword


Posted on Mar 28, 2009 by Grumpy Rabbit

[Originally reported by dangets@gmail.com to the python-hosting.org tracker]

Right now according to your documentation on here, I see that 'choices' needs to be a sequence. I could see benefits on maybe changing this to be a 'validator' argument. That way you could create your own callables that would return True or False if the choice was valid.

For example, say I wanted a float and I knew the min and max that the choice had to be in between. Offhand I can't think of an easy way to make this into a sequence that might have the float the user chose.

So if the 'validator' is a sequence type, you could still 'if choice in validator', but just add 'elif callable, if validator(choice)'

Comment #1

Posted on Mar 28, 2009 by Grumpy Rabbit

You can easily support your min/max example by defining an appropriate container, e.g.::

class MinMaxContainer(object):
    def __init__(self, min, max):
        self.min = min
        self.max = max
    def __contains__(self, obj):
        return self.min <= obj <= self.max

Status: WontFix

Labels:
Type-Enhancement Priority-Medium