aliles / begins

Command line programs for busy developers.
https://pypi.python.org/pypi/begins
Other
130 stars 27 forks source link

Required variable positional arguments #59

Open treyhunner opened 8 years ago

treyhunner commented 8 years ago

It would be convenient if I can specify that given *args are required.

Here I'm able to convert all captured *numbers to float, but I cannot find a way to require numbers to be non-empty.

I'm using a SystemExit exception as a workaround, but if begins printed out an error and the usage statement for me, that would be much more convenient.

I often require at least one positional argument when using *args so this is a common use case for me.

import begin

@begin.start
@begin.convert(numbers=float)
def main(*numbers : "numbers to add"):
    """Add the given numbers"""
    if numbers:
        print(sum(numbers)
    else:
        raise SystemExit("No numbers to add!")
Lapin0t commented 7 years ago

What you are saying is that it would be nice to have finer specification of nargs argument to argparse's add_argument. Maybe it could be done with something like @begin.params(numbers={'nargs':'+'}). Maybe having a lot of chained decorators is not so cool so @begin.convert could be included in @begin.params(numbers={'type':float, 'nargs':'+'}).