简体   繁体   中英

Read (unnamed) argument in console line

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?

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:NDesk.Options.OptionSet.Parse%28System.String,NDesk.Options.OptionContext%29

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM