Command line interface inside your C# application

偶尔善良 提交于 2019-12-02 01:30:12

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.

Dai

In Windows, a process image can startup either in GUI mode (with windows on-screen... or nothing at all) or as a "Console" process (so a command window will be opened at the same time as the process). However the two are not mutually exclusive: a "Console" process can create windows, and a GUI process can create and own its own command-line windows. However a GUI application that creates a console window will not open that console in any parent console session.

From what I understand of your question you have an existing GUI application and you want to enable some kind of console (like the Quake console) to your application rather than convert it into a console-only program.

This can be done using the AllocConsole function in Win32. I suggest you read this Q&A: How do I include a console in Winforms?

If you're confused about whether or not you need to multi-thread your application, I'd recomend first reading a little bit about the advantages & disadvantages of threading. The first chapter of Joe Albahari's web book is a great introduction.

Without understanding the desgin of your program fully, I would recomend that you perform all of the Chat bot behavior on a background thread. Additionally, any commands you type into the console should also trigger background work, which leaves the console free to either print output or accept input, but not perform any of the actual chat duties. Please let me know if you have any questions.

For parsing Commands you really don't need to use threading here is some information on the best way to accomplish this

There are several command line parsers over @ Codeplex.com. They are going to be very complex so depending on how much commandline parsing you are wanting to do you can use those.

I would just start out with using string.substring

for CONNECT [ []] string command = "Connect irc://QuakeNet"

  int end = command.IndexOf(" ");
  string cmd = command.substring(0,end);

  switch(cmd.ToLower())
  {
    case "connect":
         string site = command.SubString(end + 1, cmd.Length -1)
         //Do what you need to do to connect to the site
    break;
   }

if you really get to the point you need to use threading you can always use a backgroundworker thread

Tutorial on how to use a background thread

Have you considered creating a Windows Service? The Service would contain all of your code which "does stuff". You can then issue commands to the service (via ExecuteCommand).

You could then create multiple UIs, including console and GUIs, which simply call into the service and then receive information back. This would enable you to issue commands via a standard command line window and see the results in a GUI without the complication of trying to manage both windows from inside the same project.

(This would also give you a multi-process application without having to explicitly manage the processes yourself.)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!