kotlin-argparser
kotlin-argparser copied to clipboard
Extend the optional delegate to accept a list of hardcoded acceptable values
I propose to add the following overloads to the ArgParser@option function, so that we can specify a list of valid values, and if the user specifies anything else, it'll throw. Example usage would be
val mode by parser.option<Mode?>(
"-m", "--mode",
values = listOf("play", "search", "list"),
help = "some very nice description, valid options are: [id, name, flags]",
) {
when(this) {
"play" -> Mode.Play
"name" -> Mode.Search
"tags" -> Mode.List
else -> null
}
}.default(null)
or with the latter overload
val mode by parser.option(
"-m", "--mode",
values = listOf("play", "search", "list"),
help = "some very nice description, valid options are: [play, search, list]",
).default<String?>(null)
This would allow usage to be as follows:
$ ./app --mode=play // ok
$ ./app -m play // ok
$ ./app -m hello // error, "hello" was not a valid value
$ ./app // ok, the option has a default value and isnt a required field
It's technically already possible through the already existing overloads, but I can definitely see this to be a very common use, so I find it valuable enough to be included in the library.