mirror of
https://github.com/mamoe/mirai.git
synced 2025-01-11 02:50:15 +08:00
Implement SimpleCommand
This commit is contained in:
parent
01fd1b3b6e
commit
6bfe5929e8
@ -71,7 +71,7 @@ abstract class CompositeCommand @JvmOverloads constructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
final override suspend fun CommandSender.onCommand(args: Array<out Any>) {
|
final override suspend fun CommandSender.onCommand(args: Array<out Any>) {
|
||||||
matchSubCommand(args)?.parseAndExecute(this, args) ?: kotlin.run {
|
matchSubCommand(args)?.parseAndExecute(this, args, true) ?: kotlin.run {
|
||||||
defaultSubCommand.onCommand(this, args)
|
defaultSubCommand.onCommand(this, args)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -20,6 +20,7 @@ package net.mamoe.mirai.console.command
|
|||||||
import net.mamoe.mirai.console.command.description.CommandParserContext
|
import net.mamoe.mirai.console.command.description.CommandParserContext
|
||||||
import net.mamoe.mirai.console.command.description.CommandParserContextAware
|
import net.mamoe.mirai.console.command.description.CommandParserContextAware
|
||||||
import net.mamoe.mirai.console.command.description.EmptyCommandParserContext
|
import net.mamoe.mirai.console.command.description.EmptyCommandParserContext
|
||||||
|
import net.mamoe.mirai.console.command.description.plus
|
||||||
import net.mamoe.mirai.console.command.internal.AbstractReflectionCommand
|
import net.mamoe.mirai.console.command.internal.AbstractReflectionCommand
|
||||||
import net.mamoe.mirai.console.command.internal.SimpleCommandSubCommandAnnotationResolver
|
import net.mamoe.mirai.console.command.internal.SimpleCommandSubCommandAnnotationResolver
|
||||||
|
|
||||||
@ -38,11 +39,18 @@ abstract class SimpleCommand @JvmOverloads constructor(
|
|||||||
*/
|
*/
|
||||||
protected annotation class Handler
|
protected annotation class Handler
|
||||||
|
|
||||||
final override val context: CommandParserContext
|
final override val context: CommandParserContext = CommandParserContext.Builtins + overrideContext
|
||||||
get() = TODO("Not yet implemented")
|
|
||||||
|
override fun checkSubCommand() {
|
||||||
|
super.checkSubCommand()
|
||||||
|
check(subCommands.size == 1) { "There can only be exactly one function annotated with Handler at this moment as overloading is not yet supported." }
|
||||||
|
}
|
||||||
|
|
||||||
|
@Deprecated("prohibited", level = DeprecationLevel.HIDDEN)
|
||||||
|
final override suspend fun CommandSender.onDefault(rawArgs: Array<out Any>) = error("shouldn't be reached")
|
||||||
|
|
||||||
final override suspend fun CommandSender.onCommand(args: Array<out Any>) {
|
final override suspend fun CommandSender.onCommand(args: Array<out Any>) {
|
||||||
|
subCommands.single().parseAndExecute(this, args, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
final override val subCommandAnnotationResolver: SubCommandAnnotationResolver
|
final override val subCommandAnnotationResolver: SubCommandAnnotationResolver
|
||||||
|
@ -52,7 +52,7 @@ internal abstract class AbstractReflectionCommand @JvmOverloads constructor(
|
|||||||
|
|
||||||
@JvmField
|
@JvmField
|
||||||
@Suppress("PropertyName")
|
@Suppress("PropertyName")
|
||||||
internal var _usage: String = "<command build failed>"
|
internal var _usage: String = "<not yet initialized>"
|
||||||
|
|
||||||
final override val usage: String // initialized by subCommand reflection
|
final override val usage: String // initialized by subCommand reflection
|
||||||
get() = _usage
|
get() = _usage
|
||||||
@ -62,13 +62,17 @@ internal abstract class AbstractReflectionCommand @JvmOverloads constructor(
|
|||||||
internal val defaultSubCommand: DefaultSubCommandDescriptor by lazy {
|
internal val defaultSubCommand: DefaultSubCommandDescriptor by lazy {
|
||||||
DefaultSubCommandDescriptor(
|
DefaultSubCommandDescriptor(
|
||||||
"",
|
"",
|
||||||
CommandPermission.Default,
|
permission,
|
||||||
onCommand = block2 { sender: CommandSender, args: Array<out Any> ->
|
onCommand = block2 { sender: CommandSender, args: Array<out Any> ->
|
||||||
sender.onDefault(args)
|
sender.onDefault(args)
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
internal open fun checkSubCommand() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
interface SubCommandAnnotationResolver {
|
interface SubCommandAnnotationResolver {
|
||||||
fun hasAnnotation(function: KFunction<*>): Boolean
|
fun hasAnnotation(function: KFunction<*>): Boolean
|
||||||
fun getSubCommandNames(function: KFunction<*>): Array<out String>
|
fun getSubCommandNames(function: KFunction<*>): Array<out String>
|
||||||
@ -86,7 +90,7 @@ internal abstract class AbstractReflectionCommand @JvmOverloads constructor(
|
|||||||
createSubCommand(function, context)
|
createSubCommand(function, context)
|
||||||
}.toTypedArray().also {
|
}.toTypedArray().also {
|
||||||
_usage = it.firstOrNull()?.usage ?: description
|
_usage = it.firstOrNull()?.usage ?: description
|
||||||
}
|
}.also { checkSubCommand() }
|
||||||
}
|
}
|
||||||
|
|
||||||
internal val bakedCommandNameToSubDescriptorArray: Map<Array<String>, SubCommandDescriptor> by lazy {
|
internal val bakedCommandNameToSubDescriptorArray: Map<Array<String>, SubCommandDescriptor> by lazy {
|
||||||
@ -118,9 +122,14 @@ internal abstract class AbstractReflectionCommand @JvmOverloads constructor(
|
|||||||
) {
|
) {
|
||||||
internal suspend inline fun parseAndExecute(
|
internal suspend inline fun parseAndExecute(
|
||||||
sender: CommandSender,
|
sender: CommandSender,
|
||||||
argsWithSubCommandNameNotRemoved: Array<out Any>
|
argsWithSubCommandNameNotRemoved: Array<out Any>,
|
||||||
|
removeSubName: Boolean
|
||||||
|
) {
|
||||||
|
if (!onCommand(
|
||||||
|
sender,
|
||||||
|
parseArgs(sender, argsWithSubCommandNameNotRemoved, if (removeSubName) names.size else 0)
|
||||||
|
)
|
||||||
) {
|
) {
|
||||||
if (!onCommand(sender, parseArgs(sender, argsWithSubCommandNameNotRemoved, names.size))) {
|
|
||||||
sender.sendMessage(usage)
|
sender.sendMessage(usage)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user