瀏覽代碼

Implement SimpleCommand

Him188 5 年之前
父節點
當前提交
6bfe5929e8

+ 1 - 1
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<out Any>) {
-        matchSubCommand(args)?.parseAndExecute(this, args) ?: kotlin.run {
+        matchSubCommand(args)?.parseAndExecute(this, args, true) ?: kotlin.run {
             defaultSubCommand.onCommand(this, args)
         }
     }

+ 11 - 3
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
 
-    final override suspend fun CommandSender.onCommand(args: Array<out Any>) {
+    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>) {
+        subCommands.single().parseAndExecute(this, args, false)
     }
 
     final override val subCommandAnnotationResolver: SubCommandAnnotationResolver

+ 14 - 5
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 = "<command build failed>"
+    internal var _usage: String = "<not yet initialized>"
 
     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<out Any> ->
                 sender.onDefault(args)
             }
         )
     }
 
+    internal open fun checkSubCommand() {
+
+    }
+
     interface SubCommandAnnotationResolver {
         fun hasAnnotation(function: KFunction<*>): Boolean
         fun getSubCommandNames(function: KFunction<*>): Array<out String>
@@ -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<Array<String>, SubCommandDescriptor> by lazy {
@@ -118,9 +122,14 @@ internal abstract class AbstractReflectionCommand @JvmOverloads constructor(
     ) {
         internal suspend inline fun parseAndExecute(
             sender: CommandSender,
-            argsWithSubCommandNameNotRemoved: Array<out Any>
+            argsWithSubCommandNameNotRemoved: Array<out Any>,
+            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)
             }
         }