This is second article in my blog post series “Oxygene by Example”.

While working with command line arguments in Prism is quite simple, the “Main” method has an array of string that holds the value, it can become quite complex when having to parse options too. When having to work with those, it’s easiest to use the NDesk for Oxygene file. It’s a single file that can be added to any project (no need for a separate dll library). It was originally written in C# by Jonathan Pryor, we ported it to Oxygene so it can be embedded in an Oxygene project too.

The first step is to add NDesk.Options to the uses list, this is where the classes are defined. A general console application entry point looks like:

classmethod ConsoleApp.Main(arguments:arrayof String): Integer;beginvar lSourceUri: String;var lDestinationUri: String;var lSomeOption: Boolean;var lShowHelp: Boolean;var lOptionSet :=new OptionSet();var lFiles:sequenceof String;   lOptionSet.Add("s|source=", "{filename or url}of source object", v -> begin lSourceUri := v end); lOptionSet.Add("d|destination=", "{filename or url}of destination object", v -> begin lDestinationUri := v end); lOptionSet.Add("o|someoption", "some binary option", v -> begin lSomeOption :=assigned(v)end); lOptionSet.Add("h|?", "show help", v -> begin lShowHelp :=assigned(v);end);try lFiles := lOptionSet.Parse(arguments);excepton Exception do lShowHelp :=true;end;if(lShowHelp)thenbegin lOptionSet.WriteOptionDescriptions(Console.Out);exit1;endelsebegin Console.WriteLine('Options supplied:'); Console.WriteLine('Source: '+ lSourceUri); Console.WriteLine('Destination: '+ lDestinationUri); Console.WriteLine('Some option: '+ iif(lSomeOption,'ON','OFF'));foreach el in lFiles do Console.WriteLine('Argument: '+el);end; Console.ReadLine();end;
The OptionSet class is the main class for the options parser, first you have to define the different options by using the “Add” method. The first parameter defines what option it maps to. in “s|source=”, there are two bindings for this option, “s” and “source”, the | just separates them. The “=” is used to denote that the option takes a parameter. “s” is a single letter option, to use it one can use “MyApp -ssourcefile.text”, or “MyApp –source sourcefile.text”, the short options do not have a space, and a single dash, the long options have a space and two dashes.

The second parameter for Add is the description, when writing out the option list it will print this (see below), the last parameter is a delegate that gets called when it’s triggered. This delegate (in the cases above we use anonymous methods) has one parameter with the value passed. From these anonymous methods we set the options we define in the variables above it, that way we can read them later on.

The last step is lOptionSet.Parse(arguments), this will return a list of “rest” parameters, anything that’s not an option (or parameter to an option) will be in this list. It will raise an exception when there’s an error parsing arguments. We set the lShowHelp to true in this case, so we can later write the options to the console with WriteOptionDescriptions.

Using the Ndesk.Options classes makes parsing parameters a lot easier than doing it manually. The OptionSet classes have a lot more features, all of which are documented at the top of the source file Options.pas.

The original version of this article can be found in our wiki.