Skip to content
This repository has been archived by the owner on Apr 22, 2021. It is now read-only.

Commit

Permalink
[enhancement] Added auto complete for enum argument
Browse files Browse the repository at this point in the history
  • Loading branch information
Luna5ama committed Jan 8, 2021
1 parent c9be698 commit 62cbb2d
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion org/kamiblue/command/Args.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.kamiblue.command
import org.kamiblue.command.utils.ExecuteBlock
import org.kamiblue.command.utils.Invokable
import org.kamiblue.commons.interfaces.Alias
import org.kamiblue.commons.interfaces.DisplayEnum
import org.kamiblue.commons.interfaces.Nameable

/**
Expand Down Expand Up @@ -176,14 +177,22 @@ class BooleanArg(
class EnumArg<E : Enum<E>>(
override val name: String,
enumClass: Class<E>
) : AbstractArg<E>() {
) : AbstractArg<E>(), AutoComplete by StaticPrefixMatch(getAllNames(enumClass)) {

private val enumValues = enumClass.enumConstants

override suspend fun convertToType(string: String?): E? {
return enumValues.find { it.name.equals(string, true) }
}

private companion object {
private fun <E : Enum<E>> getAllNames(clazz: Class<E>) = ArrayList<String>().apply {
for (enum in clazz.enumConstants) {
if (enum is DisplayEnum) add(enum.displayName)
add(enum.name)
}
}
}
}

/**
Expand Down

0 comments on commit 62cbb2d

Please sign in to comment.