Skip to content

添加子命令

可以通过实现 ISubListCommand 和 IContainSubCommands 实现给命令添加子命令

csharp
using GrapeCity.Forguncy.Commands;
using System.Collections.Generic;
using System.ComponentModel;
using System.Threading.Tasks;

namespace MyPlugin
{
    public class MyPluginServerCommand : Command, ICommandExecutableInServerSideAsync, ISubListCommand, IContainSubCommands
    {
        [Browsable(false)]
        public List<Command> CommandList { get; set; } = new List<Command>();

        public IEnumerable<List<Command>> EnumSubCommands()
        {
            yield return CommandList;
        }

        public async Task<ExecuteResult> ExecuteAsync(IServerCommandExecuteContext dataContext)
        {
            dataContext.Log.AppendLine("子命令开始执行");
            var result = await dataContext.ExecuteCommandsAsync(this.CommandList);
            dataContext.Log.AppendLine("子命令执行结束");
            return result;
        }

        public override CommandScope GetCommandScope()
        {
            return CommandScope.ExecutableInServer;
        }
    }
}

代码说明

  1. 给 CommandList属性标注 [Browsable(false)] 避免CommandList属性出现在主命令的属性中
  2. 通过 dataContext.ExecuteCommandsAsync 方法执行子命令

效果

1670251478996-6139c916-e10f-42e0-a7a3-1f76222a1e63.png

更新: 2022-12-05 22:48:05
原文: https://www.yuque.com/robert-bh51n/ea8l6c/kcw63btlnzehmeaf