views:

80

answers:

1

Library at

http://testapi.codeplex.com/

Excerpt of usage from

http://blogs.msdn.com/ivo_manolov/archive/2008/12/17/9230331.aspx

A third common approach is forming strongly-typed commands from the command-line parameters. This is common for cases when the command-line looks as follows:

some-exe  COMMAND  parameters-to-the-command

The parsing in this case is a little bit more involved:

  1. Create one class for every supported command, which derives from the Command abstract base class and implements an expected Execute method.
  2. Pass an expected command along with the command-line arguments to CommandLineParser.ParseCommand – the method will return a strongly-typed Command instance that can be Execute()-d.

    // EXAMPLE #3: // Sample for parsing the following command-line: // Test.exe run /runId=10 /verbose // In this particular case we have an actual command on the command-line (“run”), which we want to effectively de-serialize and execute.

     public class   RunCommand : Command
     { 
     bool?  Verbose { get; set; } 
     int? RunId { get; set; }
     public override void Execute()
        {
       // Implement your "run" execution logic here.
        }
     }
      Command c = new RunCommand();
      CommandLineParser.ParseArguments(c, args);
      c.Execute();
    

============================

I don't get if we instantiate specific class before parsing arguments , what's the point of command line argument "run" which is very first one. I thought the idea was to instantiate and execute command/class based on a command line parameter ( "run" parameter becomes instance RunCommand class, "walk" becomes WalkCommand class and so on ). Can it be done with the latest version ? Moreover what I downloaded from codeplex doesn't work the way described above and doesn't accept the very first parameter without slash. So if using reflection I have to pass command name as usual parameter and do then multi-step - determining class name , instantiate via reflection, and only then parse other arguments via ParseArguments.

A: 

MicMit, The example on my blog was actually flawed. I have since corrected it.

In essence, what you do is: 1. parse the 1st argument (the command name) to figure out what command you need to instantiate 2. instantiate the target command, then pass the rest of the argument list to it. 3. execute the command

i.e. in your Main, you would do something like the following...

if (String.Compare(args[0], "run", StringComparison.InvariantCultureIgnoreCase) == 0)
{
    Command c = new RunCommand();
    c.ParseArguments(args.Skip(1)); // or CommandLineParser.ParseArguments(c, args.Skip(1))
    c.Execute();
}

Hope that helps.

Ivo Manolov
I figured it out , but blog editing deserves accepting it as an answer.
MicMit