在控制台行中读取(未命名)参数

时间:2009-12-21 14:39:53

标签: c# command-line

由于ConsoleFx看起来进展得太慢(太糟糕了,它有很多潜力)并且每次构建都显示了太多重大变化,我决定切换到Mono.Options以满足我的命令行解析需求。

我的OptionSet使用以下方法构建

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;
}

我找到的所有教程,处理选项以及如何捕获它们,但从未提及过参数。

以下调用例如

   c:\>test.exe brandCode1 brandCode2 /w

应该等待true,并给我两个带有值brandCode1和brandCode2的参数。 如何从args []中以干净的方式捕获它们?

Mono.Options无法实现这一目标吗?

1 个答案:

答案 0 :(得分:1)

从阅读文档中我可以看出,您需要在某个时候调用OptionSet的{​​{3}}方法。当您这样做时,它会处理您的操作并返回“包含所有未处理参数的A List<string>。”

不幸的是,你还需要传递main方法的参数才能使它工作。

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

编辑:如果我的链接(仍然)无效,解析应该链接到parse

相关问题