示例#1
0
        /// <summary>
        /// 解释器执行解释命令行分割操作
        /// </summary>
        /// <param name="order"></param>
        /// <returns>返回null表示命令行不正确</returns>
        public CmdParams Execute(string orderStr)
        {
            //格式化命令行
            orderStr = new Regex("[\\s]+").Replace(orderStr, " ");
            orderStr = orderStr.Trim();
            var orderArray = orderStr.Split(' ');
            var cmdParam   = new CmdParams();

            cmdParam.OrderLine = orderStr;
            if (orderArray.Length == 0 || (orderArray.Length == 1 && orderArray[0] == ""))
            {
                return(null);
            }
            cmdParam.Order = orderArray[0];
            if (orderArray.Length == 1)
            {
                return(cmdParam);
            }
            //分离指令参数
            var pairParams = new List <KeyValuePair <string, string> >();

            for (var i = 1; i < orderArray.Length; i++)
            {
                var para = orderArray[i];
                if (para.Length == 0)
                {
                    continue;
                }
                if (para[0] == '-')
                {
                    string value = null;
                    if (i < orderArray.Length - 1)
                    {
                        var nextPara = orderArray[i + 1];
                        if (nextPara.Length == 0)
                        {
                            continue;
                        }
                        if (nextPara[0] != '-')
                        {
                            value = nextPara;
                            i++;
                        }
                    }
                    var pair = new KeyValuePair <string, string>(para, value);
                    pairParams.Add(pair);
                }
                else
                {
                    pairParams.Add(new KeyValuePair <string, string>(null, para));
                }
            }
            cmdParam.PairParams = pairParams;
            return(cmdParam);
        }
示例#2
0
        /// <summary>
        /// 尝试执行命令
        /// </summary>
        /// <param name="cmdParam">命令参数实体</param>
        /// <returns>执行结果:是否成功</returns>
        public async Task <bool> TryExecuteOrder(CmdParams cmdParam)
        {
            if (cmdParam == null)
            {
                throw new CmdException($"命令行格式错误:{cmdParam.OrderLine}");
            }
            if (cmdParam.Order == null)
            {
                throw new CmdException($"找不到命令头:{cmdParam.OrderLine}");
            }
            if (_orderQueue.TryGetValue(cmdParam.Order, out var cmdDetial))
            {
                if (cmdDetial == null || cmdDetial.Callback == null)
                {
                    CmdTerminal.CurrentInternal.InternalOutputCallback?.Invoke("命令获取失败,使用help查看所有命令。", CmdOutPutType.Error);
                    return(false);
                }
                if (!CheckMode(cmdDetial.CmdType))
                {
                    CmdTerminal.CurrentInternal.InternalOutputCallback?.Invoke($"命令[cmdParam.Order]执行失败,不在可执行范围内。", CmdOutPutType.Error);
                    return(false);
                }
                var newBackParams = new List <KeyValuePair <string, string> >();
                var fakeParam     = cmdDetial.ParamNames.ToArray();
                var index         = 0;
                foreach (var pPair in cmdParam.PairParams)
                {
                    if (pPair.Key == null)
                    {
                        //只有值
                        if (index >= cmdDetial.ParamNames.Count)
                        {
                            CmdTerminal.CurrentInternal.InternalOutputCallback?.Invoke("参数过多,使用help -o [command]查看命令参数说明。", CmdOutPutType.Error);
                            return(false);
                        }
                        var key = fakeParam[index].Key;

                        var newBPCount = newBackParams.Count(v => v.Key == key);
                        if (newBPCount > 0)
                        {
                            CmdTerminal.CurrentInternal.InternalOutputCallback?.Invoke("参数重复赋值,使用help -o [command]查看命令参数说明。", CmdOutPutType.Error);
                            return(false);
                        }
                        newBackParams.Add(new KeyValuePair <string, string>(key, pPair.Value));
                    }
                    else if (pPair.Value == null)
                    {
                        //只有键
                        var pCount = cmdDetial.ParamNames.Count(v => v.Key == pPair.Key);
                        if (pCount == 0)
                        {
                            CmdTerminal.CurrentInternal.InternalOutputCallback?.Invoke($"命令【{cmdDetial.Order}】没有参数【{pPair.Key}】,使用help -o [command]查看命令参数说明。", CmdOutPutType.Error);
                            return(false);
                        }
                        newBackParams.Add(new KeyValuePair <string, string>(pPair.Key, null));
                    }
                    else
                    {
                        //都有
                        var newBPCount = newBackParams.Count(v => v.Key == pPair.Key);
                        if (newBPCount > 0)
                        {
                            CmdTerminal.CurrentInternal.InternalOutputCallback?.Invoke("参数重复赋值,使用help -o [command]查看命令参数说明。", CmdOutPutType.Error);
                            return(false);
                        }
                        var pCount = cmdDetial.ParamNames.Count(v => v.Key == pPair.Key);
                        if (pCount == 0)
                        {
                            CmdTerminal.CurrentInternal.InternalOutputCallback?.Invoke($"命令【{cmdDetial.Order}】没有参数【{pPair.Key}】,使用help -o [command]查看命令参数说明。", CmdOutPutType.Error);
                            return(false);
                        }
                        newBackParams.Add(new KeyValuePair <string, string>(pPair.Key, pPair.Value));
                    }
                    index++;
                }
                cmdParam.PairParams = newBackParams;
                await Task.Run(() => cmdDetial.Callback.Invoke(cmdParam));

                return(true);
            }
            CmdTerminal.CurrentInternal.InternalOutputCallback?.Invoke("未找到命令,使用help查看所有命令。", CmdOutPutType.Error);
            return(false);
        }