Skip to content


Introduce choices #38

mjdorma opened this Issue · 4 comments

2 participants


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.choices(arg=('foo', 'moo', 'cow'))
def main(arg):

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

> python blad
usage: [-h] ARG error: argument ARG: invalid choice: 'blad' (choose from 'foo', 'moo',

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.


import begin

def main(arg=set('foo', 'moo', 'cow')):

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

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.


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!


Example solution using begin.convert

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.