Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Introduce choices #38

Open
mjdorma opened this Issue · 4 comments

2 participants

@mjdorma

In this issue I am looking for a neat way of adopting/utilising the choices capability from ArgParse so my positional and or keyword arguments can be decorated with a fixed set of choices.

One approach that may work is to add an additional interface to begin which allows us to wrap "choices" into the function decorator. Here is my prototype code:

import begin

@begin.start
@begin.choices(arg=('foo', 'moo', 'cow'))
def main(arg):
    pass

Now when we violate these choices we get the following behaviour:

> python test.py blad
usage: test.py [-h] ARG
test.py: error: argument ARG: invalid choice: 'blad' (choose from 'foo', 'moo',
'cow')
@mjdorma

Post coffee / lunch, an alternative strategy to the above solution could be to implicitly treat the default value of an arg as its set of choices iff the value has a baseclass of set.

I.e.

import begin

@begin.start
def main(arg=set('foo', 'moo', 'cow')):
    pass

If the arg default value has a baseclass of set, treat it as the set of choices for that arg.

@aliles aliles added the enhancement label
@aliles
Owner

Thanks, this design looks quite solid. It's definately the best solution I've seen so far for supporting choices. Although I'm not (yet) sold on using a default value that's a set instance as the choice of values. That interferes with the design goal of being able to call the function directly from code. Failing to pass in a value would result in non-deterministic errors rather than a consistent TypeError over a missing argument.

@mjdorma

Agree on not using implicit set solution, it breaks one of the fundamental awesome features of begins that is to be able to call the interface as it were a normal function.

Good call!

@mjdorma

Example solution using begin.convert
https://gist.github.com/mjdorma/9756431

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.