你的C#应用​​程序中的命令行界面 [英] Command line interface inside your C# application

查看:187
本文介绍了你的C#应用​​程序中的命令行界面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直用C#工作了几个项目,没有什么大什么的。所以我想从现在开始一个真正的项目。我创建了一个C#中的IRC机器人。
本IRC机器人运行没有在一个命令行的GUI。它运行现在,当,它输出的每一个命令和东西。



我想要做的就是创建一个界面命令行中的应用程序。
因此,即时通讯能够键入命令到应用程序,并输出命令的结果。



我试过阅读几个主题多线程我的软件因为我相信,这是需要我来实现这一功能。但我不知道是肯定的。



我读过类似的>命令行界面和的 http://msdn.microsoft.com/en-us/library/aa288457(v = vs.71)的.aspx 。但是我还是很困惑。



我试过对此事google搜索喜欢的 http://alexis.royer.free.fr/CLI/



我想请教在这个问题上,如果可能,有利于对基础知识这一计划。如果有人强烈建议我不要寻找C#中这个功能,那么我会很高兴听到他/她为什么不和建议我应该用什么/做的。



<意见p>提前感谢!
罗伯托。



P.S。原谅我的不正确的英语语法。


解决方案

下面是一个非常简单的命令行,对如何做不同颜色的例子,处理参数:

 使用系统; 
使用System.Linq的;

命名空间SOSimpleCLI
{
类节目
{
静态无效的主要(字串[] args)
{
串CMDLINE = NULL;
,而(CMDLINE =结束!)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.Write(SOSimpleCLI:);
Console.ForegroundColor = ConsoleColor.White;

CMDLINE =到Console.ReadLine();

的String [] CMD = cmdLine.Split('');
开关(cmd.FirstOrDefault())
{
案CMD1:
Console.WriteLine(CMD1());
中断;

案CMD2:
如果(!cmd.Length = 2)
{
Console.WriteLine(args作为CMD2数目错误);
}
,否则
{
Console.WriteLine(CMD2(CMD [1]));
}
中断;
}
}
}

静态字符串CMD1()
{
的回报,从命令1日起;
}

静态字符串CMD2(字符串ARG)
{
返回的String.Format(从命令2传来:{0},ARG);
}
}
}



我不知道为什么你希望它是多线程的,但你可以补充一点,通过与不同的方法里面的线程交互。


I have been working with C# for a few projects, nothing big or something. So I want to start working on a real project now. I've created a IRC bot in C#. This IRC bot runs without a GUI in a commandline. Now when it runs, it outputs every command and stuff.

What i want to do is create a interface for the application inside the commandline. So that im able to type commands into the application and output the results of the command.

I've tried reading several topics on multi-threading my software cause i believe this is needed for me to achieve this functionality. But i dont know for sure.

I've read threads like Command-line interface in Java and http://msdn.microsoft.com/en-us/library/aa288457(v=vs.71).aspx. But im still very much confused.

I've tried googling on the matter for any toolkits like http://alexis.royer.free.fr/CLI/

I would like to ask for advice on this matter and, if possible, help on the basics for this program. If someone strongly suggest me not to search for this functionality within C# then i would be happy to hear his/her opinion on why not and advice on what i should use/do.

Many thanks in advance! Roberto.

P.s. My apologies for my incorrect English grammar.

解决方案

Here's a very simple command line, with an example of how to do different colours and handle arguments:

using System;
using System.Linq;

namespace SOSimpleCLI
{
    class Program
    {
        static void Main(string[] args)
        {
            string cmdLine = null;
            while (cmdLine != "end")
            {
                Console.ForegroundColor = ConsoleColor.Green;
                Console.Write("SOSimpleCLI: ");
                Console.ForegroundColor = ConsoleColor.White;

                cmdLine = Console.ReadLine();

                string[] cmd = cmdLine.Split(' ');
                switch (cmd.FirstOrDefault())
                {
                    case "cmd1":
                        Console.WriteLine(Cmd1());
                        break;

                    case "cmd2":
                        if (cmd.Length != 2)
                        {
                            Console.WriteLine("Wrong number of args for cmd2");
                        }
                        else
                        {
                            Console.WriteLine(Cmd2(cmd[1]));
                        }
                        break;
                }
            }
        }

        static string Cmd1()
        {
            return "Came from command 1";
        }

        static string Cmd2(string arg)
        {
            return string.Format("Came from command 2: {0}", arg);
        }
    }
}

I'm not sure why you want it to be multithreaded, but you could add that in by interacting with the threads inside the different methods.

这篇关于你的C#应用​​程序中的命令行界面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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