PromptCommandAttribute SearchRightCommand(PromptCommandAttribute[] cmds, IEnumerable <CommandToken> args) { switch (cmds.Length) { case 0: return(null); case 1: return(cmds[0]); default: { // Multiple commands PromptCommandAttribute cmd = null; foreach (var a in cmds) { try { a.ConvertToArguments(args.Skip(a.CommandLength).ToArray()); if (cmd == null || cmd.Order > a.Order) { cmd = a; } } catch { } } return(cmd); } } }
private void Prompt_OnCommandRequested(IPrompt prompt, PromptCommandAttribute cmd, string commandLine) { if (cmd.Command == "record stop") { return; } _record.WriteLine(commandLine); }
/// <summary> /// Execute command /// </summary> /// <param name="command">Command</param> /// <returns>Return false if fail</returns> public bool Execute(string command) { PromptCommandAttribute cmd = null; try { // Parse arguments var cmdArgs = new List <string>(command.SplitCommandLine()); if (cmdArgs.Count <= 0) { return(false); } // Process command if (!_commandCache.TryGetValue(cmdArgs.First().ToLowerInvariant(), out cmd)) { throw (new Exception("Command not found")); } // Get command cmd.Method.Invoke(this, cmd.ConvertToArguments(cmdArgs.Skip(1).ToArray())); return(true); } catch (Exception e) { _consoleWriter.WriteLine(e.Message, ConsoleOutputStyle.Error); // Print help if (cmd != null && !string.IsNullOrEmpty(cmd.Help)) { _consoleWriter.WriteLine(cmd.Help, ConsoleOutputStyle.Information); } return(false); } }
/// <summary> /// Print help from Multiple commands /// </summary> /// <param name="cmds">Commands</param> void PrintHelp(IEnumerable <PromptCommandAttribute> cmds) { // Print help PromptCommandAttribute cmd = cmds.FirstOrDefault(); if (cmd != null && !string.IsNullOrEmpty(cmd.Help)) { _consoleWriter.WriteLine(cmd.Help, ConsoleOutputStyle.Information); _consoleWriter.WriteLine(""); _consoleWriter.WriteLine("Examples:", ConsoleOutputStyle.Information); // How to use? List <string> modes = new List <string>(); foreach (var v in cmds) { string args = ""; if (v.Parameters != null && v.Parameters.Length > 0) { foreach (var par in v.Parameters) { string allowed = ""; if (par.ParameterType.IsEnum) { foreach (object o in Enum.GetValues(par.ParameterType)) { allowed += (allowed != "" ? "," : "") + o.ToString(); } allowed = $" {par.Name}={allowed}"; if (!modes.Contains(allowed)) { modes.Add(allowed); } } if (par.HasDefaultValue) { args += $" [{par.Name}={(par.DefaultValue == null ? "NULL" : par.DefaultValue.ToString())}]"; } else { args += $" {par.Name}"; } } } _consoleWriter.WriteLine(" " + v.Command + args, ConsoleOutputStyle.Information); } if (modes.Count > 0) { // Options _consoleWriter.WriteLine("Options:", ConsoleOutputStyle.Information); foreach (var par in modes) { _consoleWriter.WriteLine(" " + par, ConsoleOutputStyle.Information); } } } }