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

Make enum conversion case-insensitive by default #115

Closed
sschuberth opened this issue Jan 24, 2020 · 5 comments · Fixed by #119
Closed

Make enum conversion case-insensitive by default #115

sschuberth opened this issue Jan 24, 2020 · 5 comments · Fixed by #119

Comments

@sschuberth
Copy link
Contributor

I'd like to propose to make enum value conversion case-insensitive by default, because it's probably more convenient for the command line user to not care about casing, and the risk to accidentally refer to another enum value that just differs in case is rather low.

@sschuberth
Copy link
Contributor Author

I was trying to work around this via option(...).convert { it.toUpperCase() }.enum<...>() to be able to use lower-case name for upper-case enum constants, but for some reason that does not work.

@sschuberth
Copy link
Contributor Author

My work-around is to replace

.enum<OutputFormat>()

with

.choice(enumValues<OutputFormat>().associateBy { it.name }.toSortedMap(java.lang.String.CASE_INSENSITIVE_ORDER))

@sschuberth
Copy link
Contributor Author

Or as a new extension function:

inline fun <reified T : Enum<T>> RawOption.enumCaseInsensitive(key: (T) -> String = { it.name }): NullableOption<T, T> {
    return choice(enumValues<T>().associateBy { key(it) }.toSortedMap(java.lang.String.CASE_INSENSITIVE_ORDER))
}

@ajalt
Copy link
Owner

ajalt commented Jan 24, 2020

That's a good idea. I decided to add an ignoreCase parameter to enum and choice, with value value defaulting to false for choice, and true for enum.

@sschuberth
Copy link
Contributor Author

Even better, thanks a lot!

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 a pull request may close this issue.

2 participants