All Downloads are FREE. Search and download functionalities are using the official Maven repository.

me.jakejmattson.kutils.api.arguments.ChoiceArg.kt Maven / Gradle / Ivy

There is a newer version: 0.18.1
Show newest version
package me.jakejmattson.kutils.api.arguments

import me.jakejmattson.kutils.api.dsl.arguments.*
import me.jakejmattson.kutils.api.dsl.command.CommandEvent

open class ChoiceArg(override val name: String, vararg choices: T) : ArgumentType() {
    private val enumerations = choices.associateBy { it.toString().toLowerCase() }
    private val options = enumerations.keys

    override fun convert(arg: String, args: List, event: CommandEvent<*>): ArgumentResult {
        val selection = enumerations[arg.toLowerCase()] as? String
            ?: return ArgumentResult.Error("Invalid choice for $name. Available choices: ${options.joinToString()}")

        return ArgumentResult.Success(selection)
    }

    override fun generateExamples(event: CommandEvent<*>) = options.toList()
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy