Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend the optional delegate to accept a list of hardcoded acceptable values #78

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

NoakPalander
Copy link

@NoakPalander NoakPalander commented May 30, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant