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); } } }
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)); }
private void Print(string error, CLICommandInfo cmd) { var cliresult = CLIResult.Error(error); cliresult.Print(); }