AnkurVj AnkurVj - 9 months ago 31
Scala Question

Parsing options that take more than one value with scopt in scala

I am using scopt to parse command line arguments in scala. I want it to be able to parse options with more than one value. For instance, the range option, if specified, should take exactly two values.

--range 25 45


Coming, from python background, I am basically looking for a way to do the following with scopt instead of python's argparse:

parser.add_argument("--range", default=None, nargs=2, type=float,
metavar=('start', 'end'),
help=(" Foo bar start and stop "))


I dont think
minOccurs
and
maxOccurs
solves my problem exactly, nor the key:value example in its help.

0__ 0__
Answer Source

Looking at the source code, this is not possible. The Read type class used has a member tuplesToRead, but it doesn't seem to be working when you force it to 2 instead of 1. You will have to make a feature request, I guess, or work around this by using --min 25 --max 45, or --range '25 45' with a custom Read instance that splits this string into two parts. As @roterl noted, this is not a standard way of parsing.