//------------------------------------------------------------------------------ // 此代码版权(除特别声明或在XREF结尾的命名空间的代码)归作者本人若汝棋茗所有 // 源代码使用协议遵循本仓库的开源协议及附加协议,若本仓库没有设置,则按MIT开源协议授权 // CSDN博客:https://blog.csdn.net/qq_40374647 // 哔哩哔哩视频:https://space.bilibili.com/94253567 // Gitee源代码仓库:https://gitee.com/RRQM_Home // Github源代码仓库:https://github.com/RRQM // API首页:https://www.yuque.com/rrqm/touchsocket/index // 交流QQ群:234762506 // 感谢您的下载和使用 //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ using System; using System.Collections.Generic; using System.Linq; namespace TouchSocket.Core { /// /// 控制台行为 /// //[IntelligentCoder.AsyncMethodPoster(Flags = IntelligentCoder.MemberFlags.Public)] public partial class ConsoleAction { private readonly string helpOrder; /// /// 构造函数 /// /// 帮助信息指令,如:"h|help|?" public ConsoleAction(string helpOrder = "h|help|?") { this.helpOrder = helpOrder; Add(helpOrder, "帮助信息", ShowAll); string title = $@" _______ _ _____ _ _ |__ __| | | / ____| | | | | | | ___ _ _ ___ | |__ | (___ ___ ___ | | __ ___ | |_ | | / _ \ | | | | / __|| '_ \ \___ \ / _ \ / __|| |/ // _ \| __| | || (_) || |_| || (__ | | | | ____) || (_) || (__ | <| __/| |_ |_| \___/ \__,_| \___||_| |_||_____/ \___/ \___||_|\_\\___| \__| ------------------------------------------------------------------- Author : 若汝棋茗 Version : {System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString()} Gitee : https://gitee.com/rrqm_home Github : https://github.com/rrqm API : https://www.yuque.com/rrqm/touchsocket/index ------------------------------------------------------------------- "; Console.WriteLine(title); } /// /// 显示所有注册指令 /// public void ShowAll() { int max = actions.Values.Max(a => a.FullOrder.Length) + 8; List s = new List(); foreach (var item in actions) { if (!s.Contains(item.Value.FullOrder.ToLower())) { s.Add(item.Value.FullOrder.ToLower()); Console.Write($"[{item.Value.FullOrder}]"); for (int i = 0; i < max - item.Value.FullOrder.Length; i++) { Console.Write("-"); } Console.WriteLine(item.Value.Description); } } } /// /// 帮助信息指令 /// public string HelpOrder => helpOrder; private readonly Dictionary actions = new Dictionary(); /// /// 添加 /// /// 指令,多个指令用“|”分割 /// 描述 /// public void Add(string order, string description, Action action) { string[] orders = order.ToLower().Split('|'); foreach (var item in orders) { actions.Add(item, new VAction(description, order, action)); } } /// /// 执行异常 /// public event Action OnException; /// /// 执行,返回值仅表示是否有这个指令,异常获取请使用 /// /// /// public bool Run(string order) { if (actions.TryGetValue(order.ToLower(), out VAction vAction)) { try { vAction.Action.Invoke(); } catch (Exception ex) { OnException?.Invoke(ex); } return true; } else { return false; } } } internal struct VAction { private readonly Action action; public Action Action => action; private readonly string fullOrder; public string FullOrder => fullOrder; private readonly string description; /// /// 构造函数 /// /// /// /// public VAction(string description, string fullOrder, Action action) { this.fullOrder = fullOrder; this.action = action ?? throw new ArgumentNullException(nameof(action)); this.description = description ?? throw new ArgumentNullException(nameof(description)); } public string Description => description; } }