From 6bfe5929e8400ea0c885a3e5da1ef9d58cfd6a0e Mon Sep 17 00:00:00 2001 From: Him188 Date: Sun, 28 Jun 2020 10:44:55 +0800 Subject: [PATCH] Implement SimpleCommand --- .../mirai/console/command/CompositeCommand.kt | 2 +- .../mirai/console/command/SimpleCommand.kt | 14 +++++++++++--- .../internal/CompositeCommandInternal.kt | 19 ++++++++++++++----- 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/CompositeCommand.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/CompositeCommand.kt index 3e987e2a3..9a3fbe7d1 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/CompositeCommand.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/CompositeCommand.kt @@ -71,7 +71,7 @@ abstract class CompositeCommand @JvmOverloads constructor( } final override suspend fun CommandSender.onCommand(args: Array) { - matchSubCommand(args)?.parseAndExecute(this, args) ?: kotlin.run { + matchSubCommand(args)?.parseAndExecute(this, args, true) ?: kotlin.run { defaultSubCommand.onCommand(this, args) } } diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/SimpleCommand.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/SimpleCommand.kt index 58adce745..bb7cf9bf0 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/SimpleCommand.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/SimpleCommand.kt @@ -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.CommandParserContextAware 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.SimpleCommandSubCommandAnnotationResolver @@ -38,11 +39,18 @@ abstract class SimpleCommand @JvmOverloads constructor( */ protected annotation class Handler - final override val context: CommandParserContext - get() = TODO("Not yet implemented") + final override val context: CommandParserContext = CommandParserContext.Builtins + overrideContext + + 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) = error("shouldn't be reached") final override suspend fun CommandSender.onCommand(args: Array) { - + subCommands.single().parseAndExecute(this, args, false) } final override val subCommandAnnotationResolver: SubCommandAnnotationResolver diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/internal/CompositeCommandInternal.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/internal/CompositeCommandInternal.kt index d56e9a7b6..2a4db7397 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/internal/CompositeCommandInternal.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/internal/CompositeCommandInternal.kt @@ -52,7 +52,7 @@ internal abstract class AbstractReflectionCommand @JvmOverloads constructor( @JvmField @Suppress("PropertyName") - internal var _usage: String = "" + internal var _usage: String = "" final override val usage: String // initialized by subCommand reflection get() = _usage @@ -62,13 +62,17 @@ internal abstract class AbstractReflectionCommand @JvmOverloads constructor( internal val defaultSubCommand: DefaultSubCommandDescriptor by lazy { DefaultSubCommandDescriptor( "", - CommandPermission.Default, + permission, onCommand = block2 { sender: CommandSender, args: Array -> sender.onDefault(args) } ) } + internal open fun checkSubCommand() { + + } + interface SubCommandAnnotationResolver { fun hasAnnotation(function: KFunction<*>): Boolean fun getSubCommandNames(function: KFunction<*>): Array @@ -86,7 +90,7 @@ internal abstract class AbstractReflectionCommand @JvmOverloads constructor( createSubCommand(function, context) }.toTypedArray().also { _usage = it.firstOrNull()?.usage ?: description - } + }.also { checkSubCommand() } } internal val bakedCommandNameToSubDescriptorArray: Map, SubCommandDescriptor> by lazy { @@ -118,9 +122,14 @@ internal abstract class AbstractReflectionCommand @JvmOverloads constructor( ) { internal suspend inline fun parseAndExecute( sender: CommandSender, - argsWithSubCommandNameNotRemoved: Array + argsWithSubCommandNameNotRemoved: Array, + removeSubName: Boolean ) { - if (!onCommand(sender, parseArgs(sender, argsWithSubCommandNameNotRemoved, names.size))) { + if (!onCommand( + sender, + parseArgs(sender, argsWithSubCommandNameNotRemoved, if (removeSubName) names.size else 0) + ) + ) { sender.sendMessage(usage) } }