views:

39

answers:

1

As ConsoleFx seems to be progressing too slow (bummer, it had much potential) and showing too many breaking changes every build, I decided to switch to Mono.Options for my commandline parsing needs.

My OptionSet is built in the following method

private static OptionSet BuildOptionSet()
{
    OptionSet optionSet = new OptionSet()
        .Add("?|help|h", "Prints out the options", option => help = option != null)
        .Add("w|wait", "Waits for any key after finished processing", option => wait)
    return optionSet;
}

All tutorials I find, deal with options and how to capture them, but arguments are never mentioned.

The following call for example

   c:\>test.exe brandCode1 brandCode2 /w

Should put wait on true and give me two arguments with values brandCode1 and brandCode2. How can I capture them in a clean way from the args[] ?

Is this not possible with Mono.Options?

+1  A: 

From what I can tell from reading the docs, you need to call OptionSet's parse method at some point. When you do, it processes your actions and returns "A List<string> containing all unhandled arguments."

Unfortunately, you also need to pass it the main method's argument to get this to work.

List<string> extra = optionSet.Parse(args);

Edit: In case my link (still) isn't working, parse should link to http://www.ndesk.org/doc/ndesk-options/NDesk.Options/OptionSet.html#M%3ANDesk.Options.OptionSet.Parse%28System.String,NDesk.Options.OptionContext%29

R. Bemrose
This is true. I think you can set a handler as well for unhandled options, but I've never tried that myself.
Skurmedel
I'll be honest, I went solely from the docs on this one, as I've not used Mono Options, and when I saw that it returned the unprocessed options when you ran parse, I just assumed that was the only way to do it.
R. Bemrose
Hehe, I have, in MS .NET even ;)
Skurmedel
This seems to work indeed. Sadly this leaves testing for the number of arguments etc to the client library. Well, no biggy.
borisCallens