Пример #1
0
 public bool TryInvoke(CliInvocationContext context, string[] args)
 {
     if (args.Length != Parameters.Length - 1)
     {
         context.Window.WriteError("Usage: " + Usage);
         return(false);
     }
     else
     {
         object[] pars = new object[args.Length + 1];
         pars[0] = context;
         for (int i = 1; i <= args.Length; i++)
         {
             if (ParameterTypes[i] == typeof(string))
             {
                 pars[i] = args[i - 1];
             }
             else
             {
                 MethodInfo parse = ParameterTypes[i].GetMethod("Parse", new Type[] { typeof(string) });
                 pars[i] = parse.Invoke(null, new object[] { args[i - 1] });
             }
         }
         //try { Method.Invoke(null, pars); }
         //catch { return false; }
         Method.Invoke(null, pars);
         return(true);
     }
 }
Пример #2
0
        private void _txtInput_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Return)
            {
                string line = _txtInput.Text;
                WriteLine("> " + _txtInput.Text);
                _txtInput.Text = "";

                if (Cli != null)
                {
                    CommandLine.Parse(line, out string command, out string[] arguments);
                    bool          foundCmd       = false;
                    StringBuilder helpTexts      = new StringBuilder();
                    bool          printHelpTexts = true;
                    foreach (CliCommandInfo cmd in Cli.Commands)
                    {
                        if (cmd.Name == command)
                        {
                            foundCmd = true;
                            if (cmd.Parameters.Length == arguments.Length + 1)
                            {
                                CliInvocationContext context = new CliInvocationContext(this);
                                if (cmd.TryInvoke(context, arguments))
                                {
                                    printHelpTexts = false;
                                    break;
                                }
                                else
                                {
                                }
                            }
                            else
                            {
                                helpTexts.AppendLine(cmd.Description);
                                helpTexts.AppendLine("Usage: " + cmd.Name + " " + cmd.Usage);
                            }
                        }
                    }
                    if (!foundCmd)
                    {
                        WriteError("Invalid command: " + command);
                    }
                    else if (printHelpTexts)
                    {
                        WriteInfo(helpTexts.ToString());
                    }
                }
                else if (UseTypedEntry)
                {
                    CommandLine.ParseWithTypes(line, out string command, out object[] arguments);
                    OnTypedEntry(command, arguments);
                }
                else
                {
                    CommandLine.Parse(line, out string command, out string[] arguments);
                    OnEntry(command, arguments);
                }
            }
        }
Пример #3
0
        public static void Help(CliInvocationContext context)
        {
            var helpText = new StringBuilder();

            helpText.AppendLine("Zefugi - Dev Console - Usage help:");
            helpText.AppendLine("This is a simple command parsing console. Commands come in the format of a command name (single word) and no or a number of arguments.");
            helpText.AppendLine("Arguments can either be a string (use \" escape characters to include spaces) or a parsable string that will result in an object being parsed to the command.");
            helpText.AppendLine();
            helpText.AppendLine("To get help on a specific command type 'help <commandName>'");
            helpText.AppendLine();
            helpText.AppendLine("For more information see https://github.com/Zefugi/DevConsole");
            context.Window.WriteInfo(helpText.ToString());
        }
Пример #4
0
        public static void Help(CliInvocationContext context, string commandName)
        {
            var helpTexts = new StringBuilder();

            foreach (CliCommandInfo cmd in context.Window.Cli.Commands)
            {
                if (cmd.Name == commandName)
                {
                    helpTexts.AppendLine(cmd.Description);
                    helpTexts.AppendLine("Usage: " + cmd.Name + " " + cmd.Usage);
                }
            }
            if (helpTexts.Length == 0)
            {
                context.Window.WriteInfo("Unknown command " + commandName);
            }
            else
            {
                context.Window.WriteInfo("Help for command '" + commandName + "':\n" + helpTexts.ToString());
            }
        }
Пример #5
0
 public static void Exit(CliInvocationContext context)
 {
     context.Window.Close();
 }
Пример #6
0
 public static void Echo(CliInvocationContext context, string text)
 {
     context.Window.WriteInfo(text);
 }