示例#1
0
        public override CommandCall ParseLine(string entry, string[] splits)
        {
            var cmdCall = new CommandCall();

            cmdCall.Entry = entry;

            string lastOptStr      = null;
            bool   firstOptionMeet = false;

            for (var i = 1; i < splits.Length; i++)
            {
                var si = splits[i];

                if (!si.StartsWith("-"))
                {
                    if (string.IsNullOrEmpty(lastOptStr))
                    {
                        if (firstOptionMeet)
                        {
                            cmdCall.SetError($"Can not parse params `{si}`");
                        }
                        else
                        {
                            cmdCall.AddRequired(si);
                        }
                    }
                    else
                    {
                        cmdCall.AddOption(lastOptStr, si);
                        lastOptStr = null;
                    }
                }
                else
                {
                    firstOptionMeet = true;
                    si = si.Substring(1, si.Length - 1);
                    if (!string.IsNullOrEmpty(lastOptStr))
                    {
                        cmdCall.AddOption(lastOptStr, null);
                    }
                    lastOptStr = si;
                }
            }

            if (!string.IsNullOrEmpty(lastOptStr))
            {
                cmdCall.AddOption(lastOptStr, null);
            }
            return(cmdCall);
        }
示例#2
0
文件: cliapp.cs 项目: zcyemi/CLIfy
        private void ExecuteCommandCall(CommandCall call, bool isargv = false)
        {
            if (call == null)
            {
                //helper or other things
                CLIResult.Success(GetAppHelp()).PrintWithColor(ConsoleColor.DarkGray);
            }
            else
            {
                foreach (var m in m_cmds)
                {
                    if (m.IsMatch(call, StrictMode))
                    {
                        if (call.HasError)
                        {
                            Print(call.ParseError, m);
                            return;
                        }

                        var result = m.Execute(call);
                        Print(result);

                        if (isargv)
                        {
                            Environment.Exit(result.ErrorCode);
                        }
                        else
                        {
                            if (result.Type == CLIResult.ResultType.Exit)
                            {
                                Environment.Exit(result.ErrorCode);
                            }
                        }

                        return;
                    }
                }
                Print(CLIResult.Error($"Command not found '{call.Entry}'"));

                if (isargv)
                {
                    Environment.Exit(-1);
                }
            }
        }
示例#3
0
 public bool IsMatch(CommandCall call, bool strict = false)
 {
     return(IsEntryMatch(call.Entry, strict));
 }
示例#4
0
        public CLIResult Execute(CommandCall call)
        {
            List <object> fill = new List <object>();

            var callreqIndex = 0;

            foreach (var p in Params)
            {
                if (!p.IsOptional)
                {
                    if (callreqIndex < call.ParamRequired.Count)
                    {
                        var fillobj = call.ParamRequired[callreqIndex];

                        object ret     = null;
                        var    convsuc = ConvertParameter(fillobj, p.ParamType, out ret);
                        if (convsuc)
                        {
                            fill.Add(ret);
                            callreqIndex++;
                        }
                        else
                        {
                            return(CLIResult.Error($"Can not convert '{fillobj}' to parameter [{p.Key}({p.ParamType.ToString()})]"));
                        }
                    }
                    else
                    {
                        return(CLIResult.Error($"Param '{p.Key}' is required.", this));
                    }
                }
                else
                {
                    var options = call.Options;
                    var opt     = options.Keys.FirstOrDefault((o) => { return(o.ToLower() == p.Key.ToLower()); });
                    if (opt == null)
                    {
                        fill.Add(p.DefaultValue);
                    }
                    else
                    {
                        var calloption = options[opt];

                        if (p.ParamType == typeof(bool))
                        {
                            if (string.IsNullOrEmpty(calloption))
                            {
                                fill.Add(true);
                            }
                            else
                            {
                                if (calloption.ToLower() == "true")
                                {
                                    fill.Add(true);
                                }
                                else if (calloption.ToLower() == "false")
                                {
                                    fill.Add(false);
                                }
                                else
                                {
                                    return(CLIResult.Error($"Parse optional parameter '{p.Key}' failed width '{calloption}'"));
                                }
                            }
                        }
                        else
                        {
                            if (string.IsNullOrEmpty(calloption))
                            {
                                return(CLIResult.Error($"Optional parameter '{p.Key}' missing values.", this));
                            }

                            object ret = null;
                            var    suc = ConvertParameter(calloption, p.ParamType, out ret);
                            if (suc)
                            {
                                fill.Add(ret);
                            }
                            else
                            {
                                return(CLIResult.Error($"Can not convert '{calloption}' to parameter [{p.Key}({p.ParamType.ToString()})]"));
                            }
                        }
                    }
                }
            }

            object callresult = null;

            try
            {
                callresult = m_method.Invoke(null, fill.ToArray());
            }
            catch (Exception e)
            {
#if DEBUG
                return(CLIResult.Error($"[Exception]Command <{CommandName}> : {e.Message} {e.StackTrace}", this));
#else
                return(CLIResult.Error($"[Exception]Command <{CommandName}> : {e.Message}", this));
#endif
            }

            if (callresult == null)
            {
                return(CLIResult.Success());
            }

            if (callresult is CLIResult)
            {
                return((CLIResult)callresult);
            }

            return(CLIResult.Success(callresult));
        }