Thursday, December 16, 2004 - Posts

C# Command Line

Well i came across a snippet complier .. but was so comfortable with command prompt and needed some this that would just run that one line of code.. the i read about Monad. Well why not just use the framework to execute that one line and kinda ended up writing this small piece for doing stuff like

C# Command Line Tool v1.0
Note: Mail your comments to
sajay.antony (at the rate) gmail.com

>Console.WriteLine("Hello World");
Hello World
>for(int i=10;i<16;i++) Console.WriteLine("Hex value = {0:x}",i);
Hex value = a
Hex value = b
Hex value = c
Hex value = d
Hex value = e
Hex value = f
>quit

 

using System;

using System.Reflection;

using System.Windows.Forms;

using System.Text;

 

namespace CsCmd

{

    public class ExecutorBase

    {

        public ExecutorBase()

        {

        }

        public virtual void eval()

        {

           

        }

    }

}

 

 

namespace CsCmd

{

 public class ExpressionParser

 {

  ExecutorBase myobj = null;

  public ExpressionParser()

  {

  }

  public bool init(string expr)

  {

   Microsoft.CSharp.CSharpCodeProvider cp

                                = new Microsoft.CSharp.CSharpCodeProvider();

   System.CodeDom.Compiler.ICodeCompiler ic = cp.CreateCompiler();

   System.CodeDom.Compiler.CompilerParameters cParam

                         = new System.CodeDom.Compiler.CompilerParameters();

   cParam.GenerateInMemory = true;

   cParam.GenerateExecutable = false;

   cParam.ReferencedAssemblies.Add("system.dll");

   cParam.ReferencedAssemblies.Add("system.windows.forms.dll");

   cParam.ReferencedAssemblies.Add("CsCmd.exe");

 

  

   string src = @"using System;

        using System.Windows.Forms;

     

       class myclass:CsCmd.ExecutorBase

       {{      

         public override void eval()

            {{

                {0}

            }}

        }}";

 

   src = string.Format(src, expr);

   System.CodeDom.Compiler.CompilerResults cResults

                                   = ic.CompileAssemblyFromSource(cParam,src);

   foreach (System.CodeDom.Compiler.CompilerError ce in cResults.Errors)

    Console.WriteLine(ce.ErrorText);

 

   if (cResults.Errors.Count == 0 && cResults.CompiledAssembly != null)

   {

    Type ObjType = cResults.CompiledAssembly.GetType("myclass");

    try

    {

     if (ObjType != null)

     {

      myobj = (ExecutorBase)Activator.CreateInstance(ObjType);

     }

    }

    catch (Exception ex)

    {

     Console.WriteLine(ex.Message);

    }

    return true;

   }

   else

    return false;

  }

 

   public void eval()

     {

         if (myobj != null)

            myobj.eval();

     }

 }

 

 

    public class MainApp

    {

 

        const string AppVersion = "C# Command Line Tool v1.0 beta \nAuthor : Sajay Antony \nNote: Mail your comments to sajay.antony@gmail.com \n";

        public static void Main()

        {

            ExpressionParser mp = new ExpressionParser();

 

            Console.WriteLine(AppVersion);

 

            string input =string.Empty;

            //Console.TreatControlCAsInput = true;

            Console.Write(">");

            input = NextToken();

            do

            {

                switch (Parse(input))

                {

                    case CommandType.Statement:

                            mp.init(input);

                            mp.eval();

                        break;

                }

                Console.Write(">");

                input = NextToken();

            }

            while (input != "quit");

        }

 

        const string WriteLine = "Console.WriteLine({0});";

        public static string NextToken()

        {

           

            string input=string.Empty;

            input += Console.ReadLine();

            return input;

        }

 

 

        public enum CommandType

        {

            Statement,

            ToolCommand

        }

 

        public static CommandType Parse(string input)

        {

            CommandType returnVal;

            switch (input)

            {

                case "help":

                     Console.WriteLine("Type quit to exit program");

                     returnVal = CommandType.ToolCommand;

                     break;

                case "quit":

                       returnVal = CommandType.ToolCommand;

                    break;

                default:

                    returnVal = CommandType.Statement;

                    break;

 

            }

             return returnVal;

         }

 

 

    }

}

 

the skeleton was from code project by Vlad Tepes

with 2 Comments