如何实现自定义命令行和执行 [英] How to implement custom command line & execution

查看:102
本文介绍了如何实现自定义命令行和执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的应用程序构建自定义命令行,我有几个基本命令,并且我只是使用一堆 if语句来检查命令是什么。当前看起来像这样

I'm trying to build a custom commandline for my app, i have several basic commands, and i simply use bunch of "if" statements to check what the command is. currently it looks something like this

public void ExecuteCommand()
    {
        string input = ReadLine(); //gets last string from input
        bool isDone = false; //need bool to check whether command was executed or no, by default false.

        Match result = Regex.Match(input, @"([^\s]+)"); //to get command name
        string commandName = result.Value.ToLower();

        string value = Regex.Match(input, @"\s(.*)").Value; //to get its parameter. currently everything after ' ' space.

        if (commandName == "close")
        {
            Close(); isDone = true;
        }

        //so commandline is separate window, and appendedForm is a main form. in which some functions are executed.

        if (commandName == "exit")
        {
            appendedForm.Close();
        }

        if (commandName == "spoof")
        {
            appendedForm.Fn_Spoof();
            isDone = true;
        }

        if(commandName == "spoofstop")
        {
            appendedForm.Fn_StopCapture();
            isDone = true;
        }

        if(commandName == "scan")
        {
            appendedForm.Fn_Scan(); isDone = true;
        }

        if(commandName == "clear")
        {
            output.Text = "";
            WriteLine("Console cleared. Cache is empty.");
            //data_lines.Clear();

            isDone = true;

        }
        ...
}

基本上就是这样。我有一个mainForm和命令行表单。将字符串输入键入命令行,然后检查命令的名称并从mainForm执行一些功能。

So that's basically it. I have a mainForm, and commandline form. string input is typed into commandline, then I check the name of command and execute some function from mainForm.

我的问题是,实现这种事情的最佳方法是什么?我当然可以继续写一堆如果,但是有一些事情告诉我,这不是最好的方法。

My question is, what is the best way of implementing such kind of thing? I surely can just continue writing bunch of "if"s, but something tells me that it's not the best way to make it.

我想创建类 Command

I've thought of creating class "Command"

 public class Command
 {
    public string name;
    public string description;
    public bool hasParameter;

    Command()
    {

    }
 }

并将所有命令存储在某种数组中,但是我不确定如何使用它从mainForm调用函数。

And storing all commands in some sort of array, but I am not sure how would I use this to call a function from mainForm.

欢迎提出任何想法!

推荐答案

您可以塞满所有命令放入 Dictionary< string,someDelegate> ;如果您可以使用所有具有相同返回类型的命令。

You could stuff all commands into a Dictionary<string, someDelegate>; if you can live with all commands having the same return type.

我使用了字符串并设置了一些命令。

I have used string and set up a few commands.

我利用 params 关键字可避免每次调用时出现难看的新对象[]

您仍然需要强制转换参数,除非您可以将它们全部设为一种类型。 (实际上并不是一个坏主意,因为它们全部来自输入字符串。)

You still need to cast the arguments, unless you can make them all one type. (Which may actually be not such a bad idea, as they all come from an input string..)

这里是一个示例:

public delegate string cmdDel(params object[] args);

Dictionary<string,  cmdDel> cmd = new Dictionary<string,  cmdDel>();

添加一些功能:

cmd.Add("clear", cmd_clear);
cmd.Add("exit", cmd_exit);
cmd.Add("add", cmd_add);
cmd.Add("log", cmd_log);

具有这些主体:

public string cmd_clear(params object[] args)
{
    return "cleared";
}

public string cmd_exit(params object[] args)
{
    return "exit";
}

public string cmd_add(params object[] args)
{
    return ((int)args[0] + (int)args[1]).ToString();
}

public string cmd_log(params object[] args)
{
    StringBuilder log = new StringBuilder();
    foreach (object a in args) log.Append(a.ToString() + " ");
    return log.ToString(); 
}

并测试:

Console.WriteLine(cmd["clear"]());
Console.WriteLine(cmd["add"]( 23, 42));
Console.WriteLine(cmd["log"]( 23, "+" + 42, "=", cmd["add"]( 23, 42) ));
Console.WriteLine(cmd["exit"]());




已清除

cleared

65

23 + 42 = 65

23 + 42 = 65

退出

当然,您仍然需要(至少)使用与命令相同的行数。并且还需要进行类似数量的错误检查。

Of course you still need to use (at least) as many lines for setup as you have commands. And also need to do a similar amount of error checking.

但是命令处理部分可以变得非常简单。

But the command processing part can get pretty simple.

这篇关于如何实现自定义命令行和执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆