zenlotus / argparse

Automatically exported from code.google.com/p/argparse
Other
0 stars 0 forks source link

Allow a callable for the 'choices' keyword #7

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
[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)'

Original issue reported on code.google.com by steven.b...@gmail.com on 28 Mar 2009 at 2:24

GoogleCodeExporter commented 9 years ago
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

Original comment by steven.b...@gmail.com on 28 Mar 2009 at 2:29