-
Notifications
You must be signed in to change notification settings - Fork 122
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
Better support for enum types #84
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
clikt/src/main/kotlin/com/github/ajalt/clikt/parameters/types/enum.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.github.ajalt.clikt.parameters.types | ||
|
||
import com.github.ajalt.clikt.completion.CompletionCandidates | ||
import com.github.ajalt.clikt.core.BadParameterValue | ||
import com.github.ajalt.clikt.parameters.arguments.RawArgument | ||
import com.github.ajalt.clikt.parameters.arguments.convert | ||
import com.github.ajalt.clikt.parameters.options.RawOption | ||
import com.github.ajalt.clikt.parameters.options.convert | ||
|
||
// this function needs to be accessible, because `enum()` needs to be inlined for the reified type. | ||
// see also https://stackoverflow.com/a/41905907 | ||
@PublishedApi | ||
internal fun <T> completions(values: Array<T>) = | ||
CompletionCandidates.Fixed(values.map { it.toString() }.toSet()) | ||
|
||
// this function needs to be accessible, because `enum()` needs to be inlined for the reified type. | ||
// see also https://stackoverflow.com/a/41905907 | ||
@PublishedApi | ||
internal inline fun <reified T : Enum<T>> valueToEnum(value: String): T { | ||
return try { | ||
enumValueOf(value.toUpperCase()) | ||
} catch (ex: IllegalArgumentException) { | ||
// workaround to avoid T::class.simpleName which would require kotlin-reflections.jar | ||
throw BadParameterValue("Unknown enum constant ${T::class.java.simpleName}.$value") | ||
} | ||
} | ||
|
||
inline fun <reified T : Enum<T>> RawArgument.enum() = | ||
convert<T>(completionCandidates = completions<T>(enumValues())) { valueToEnum(it) } | ||
|
||
inline fun <reified T : Enum<T>> RawOption.enum() = | ||
convert<T>(completionCandidates = completions<T>(enumValues())) { valueToEnum(it) } | ||
|
77 changes: 77 additions & 0 deletions
77
clikt/src/test/kotlin/com/github/ajalt/clikt/parameters/types/EnumTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package com.github.ajalt.clikt.parameters.types | ||
|
||
import com.github.ajalt.clikt.core.BadParameterValue | ||
import com.github.ajalt.clikt.parameters.arguments.argument | ||
import com.github.ajalt.clikt.parameters.arguments.multiple | ||
import com.github.ajalt.clikt.parameters.arguments.optional | ||
import com.github.ajalt.clikt.parameters.options.default | ||
import com.github.ajalt.clikt.parameters.options.option | ||
import com.github.ajalt.clikt.testing.TestCommand | ||
import io.kotlintest.data.forall | ||
import io.kotlintest.shouldBe | ||
import io.kotlintest.shouldThrow | ||
import io.kotlintest.tables.row | ||
import org.junit.Test | ||
|
||
@Suppress("unused") | ||
class EnumTest { | ||
enum class TestEnum { A, B } | ||
|
||
@Test | ||
fun `enum option`() = forall( | ||
row("", null), | ||
row("--xx A", TestEnum.A), | ||
row("--xx=A", TestEnum.A), | ||
row("-xB", TestEnum.B)) { argv, expected -> | ||
class C : TestCommand() { | ||
val x by option("-x", "--xx").enum<TestEnum>() | ||
override fun run_() { | ||
x shouldBe expected | ||
} | ||
} | ||
|
||
C().parse(argv) | ||
} | ||
|
||
@Test | ||
fun `enum option error`() { | ||
class C : TestCommand() { | ||
val foo by option().enum<TestEnum>() | ||
} | ||
|
||
shouldThrow<BadParameterValue> { C().parse("--foo bar") } | ||
.message shouldBe "Invalid value for \"--foo\": Unknown enum constant TestEnum.bar" | ||
} | ||
|
||
@Test | ||
fun `enum option with default`() = forall( | ||
row("", TestEnum.B), | ||
row("--xx A", TestEnum.A), | ||
row("--xx=A", TestEnum.A), | ||
row("-xA", TestEnum.A)) { argv, expected -> | ||
class C : TestCommand() { | ||
val x by option("-x", "--xx").enum<TestEnum>().default(TestEnum.B) | ||
override fun run_() { | ||
x shouldBe expected | ||
} | ||
} | ||
C().parse(argv) | ||
} | ||
|
||
@Test | ||
fun `enum argument`() = forall( | ||
row("", null, emptyList()), | ||
row("A", TestEnum.A, emptyList()), | ||
row("A A B", TestEnum.A, listOf(TestEnum.A, TestEnum.B))) { argv, ex, ey -> | ||
class C : TestCommand() { | ||
val x by argument().enum<TestEnum>().optional() | ||
val y by argument().enum<TestEnum>().multiple() | ||
override fun run_() { | ||
x shouldBe ex | ||
y shouldBe ey | ||
} | ||
} | ||
|
||
C().parse(argv) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this is a problem, I'll revert this, but it fixed the travis build (see https://travis-ci.community/t/install-of-oracle-jdk-8-failing/3038/3)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for fixing this!