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

me.jakejmattson.kutils.api.dsl.arguments.CommandArguments.kt Maven / Gradle / Ivy

There is a newer version: 0.18.1
Show newest version
@file:Suppress("unused")

package me.jakejmattson.kutils.api.dsl.arguments

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

sealed class ArgumentResult {
    data class Success(val result: T, val consumed: Int = 1) : ArgumentResult()
    data class Error(val error: String) : ArgumentResult()
}

abstract class ArgumentType : Cloneable {
    abstract val name: String

    var isOptional: Boolean = false
        private set

    var defaultValue: ((CommandEvent<*>) -> T)? = null
        private set

    private fun  cloneToOptional() = (clone() as ArgumentType).apply { isOptional = true }
    fun makeOptional(default: T) = cloneToOptional().apply { defaultValue = { default } }
    fun makeOptional(default: (CommandEvent<*>) -> T) = cloneToOptional().apply { defaultValue = default }
    fun makeNullableOptional(default: T? = null) = cloneToOptional().apply { defaultValue = { default } }
    fun makeNullableOptional(default: (CommandEvent<*>) -> T?) = cloneToOptional().apply { defaultValue = default }

    abstract fun convert(arg: String, args: List, event: CommandEvent<*>): ArgumentResult
    abstract fun generateExamples(event: CommandEvent<*>): List

    override fun toString() = this::class.toString().substringAfterLast(".").substringBefore("$")
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy