<?xml:namespace prefix = o ns = “urn:schemas-microsoft-com:office:office” />

A couple of days ago Chris Sells made this post about the way best to parse command line augments. Given an xml document describing the structure of a command line augment, he wants to know would it be best to use code gen or an interpreter to parse command line arguments. Here is the xml structure he was talking about./o:p

 /o:p

<args description=“Two wrongs don’t make a right, but three lefts do”>
  <arg name=“lefts” description=“Number of left turns” type=“int” default=4” />
  <arg name=“attitude” description=“Driver attitude” required=“true” type=“string” />
</args>/o:p

 /o:p

F# (and the ML languages in general) offers a third way. Don’t build you command line augments out of an xml structure build a little language inside you language to describe what should be done. The idea is you have a list describing arguments, the list is made up of tuples 3 paraments and each tuple contains the name of the argument, a function describing what to do with the argument, and a help description of the agument./o:p

 /o:p

So in stead of Chris’ xml structure you have a list that looked something like:/o:p

 /o:p

let usage =/o:p

  [ “-lefts”, Arg.Int(fun leftTurnsArg -> leftTurns := leftTurnsArg), “Number of left turns”;/o:p

  ”-attitude”, Arg.String(fun attitudeArg -> attitude := attitudeArg), “Driver attitude (manditory)”;]/o:p

 /o:p

The Arg.Int or Arg.String bit just tells the interpreter what to do with the function, in this case an Arg.Int tells the interpreter to pass the next argument as an integer and the Arg.String tells it to pass the next argument as a string. There are a few others, but I won’t go into them here. The complete program in F# looks something like:/o:p

 /o:p

let leftTurns = ref 4/o:p

let attitude = ref “”/o:p

 /o:p

let search pattern = raise (Arg.Bad “unexpected arguments”)/o:p

let usage =/o:p

  [ “-lefts”, Arg.Int(fun leftTurnsArg -> leftTurns := leftTurnsArg), “Number of left turns”;/o:p

  ”-attitude”, Arg.String(fun attitudeArg -> attitude := attitudeArg), “Driver attitude (manditory)”;]/o:p

 /o:p

let _ = /o:p

    Arg.parse usage search “TestArgParser <options>”;/o:p

         if !attitude = “” then/o:p

                   Arg.usage usage “TestArgParser <options>\n\tMust specifiy attitude\n” /o:p

         else/o:p

                   begin/o:p

                            print_string “Left turns: “;/o:p

                            print_int !leftTurns;/o:p

                            print_newline();/o:p

                            print_string (“attitude: “ ^ !attitude)/o:p

                   end/o:p

 /o:p

The only slightly annoying thing about doing it this way is there no good way of checking for mandatory field so this must be done afterwards. I think its interesting to note the longest part of the programing is actually the scetion to print out the retrived arguments! The result is pretty nice though as the “Arg.parse usage” line does a lot work for us, for example if we miss type one argument a help display is shown automatically and the program exits for us. For example the command line:/o:p

 /o:p

TestArgParser.exe -attitude good -lefts eight/o:p

 /o:p

Results in:/o:p

TestArgParser <options>/o:p

        -lefts <int>: Number of left turns/o:p

        -attitude <string>: Driver attitude (manditory)/o:p

        –help: display this list of options/o:p

        -help: display this list of options/o:p

 /o:p

Where as:/o:p

TestArgParser.exe -attitude good -lefts 8/o:p

 /o:p

Gives us:/o:p

Left turns: 8/o:p

attitude: good/o:p

 /o:p

What if you don’t want to build your app in F#? That’s not a problem. You could just implement an argument parsing dll in F# and place the values you require in fields to be read later by you’re application, which could be in any .Net language./o:p

 /o:p

It is also possible to use the arg parse from C#. You have to define you list of arguments as an array and then convert it to an F# list type. Creating the function takes a little more work a you must use F# FunsFuncConvert class to create F# closures. In the end the definition of the list it self looks like:/o:p

 /o:p

Tuple<string, Arg.spec, string>[] usages/o:p

         = { new Tuple<string, Arg.spec, string>(/o:p

                            ”-lefts”, /o:p

                            new Arg.spec._Int(/o:p

                            FuncConvert.ToFastFunc<int, object>(/o:p

                                      delegate(int leftsArg) {_lefts = leftsArg; return new object();} )/o:p

                            ), /o:p

                            “Number of left turns”),/o:p

         new Tuple<string, Arg.spec, string>(/o:p

                            ”-attitude”, /o:p

                            new Arg.spec._String(/o:p

                            FuncConvert.ToFastFunc<string, object>(/o:p

                                      delegate(string attitudeArg) {_attitude = attitudeArg; return new object();} )/o:p

                            ), /o:p

                            “Driver attitude”)};/o:p

 /o:p

To me all the extra meta-data you need in C# to describe your usage of types makes this look pretty ugly, but the results program functions exactly the same. Even if it is ugly it is certainly easier than had crafting a parser. The full listing for both programs are available here./o:p

/o:p