简体   繁体   English

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

[英]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. 由于ConsoleFx的执行速度似乎太慢(糟糕,它有很大的潜力),并且每次构建都显示太多重大更改,因此我决定切换到Mono.Options来满足命令行解析的需要。

My OptionSet is built in the following method 我的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;
}

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. 应该等待true,然后给我两个参数,分别是brandCode1和brandCode2。 How can I capture them in a clean way from the args[] ? 如何从args []中以干净的方式捕获它们?

Is this not possible with Mono.Options? Mono.Options无法做到这一点吗?

From what I can tell from reading the docs, you need to call OptionSet 's parse method at some point. 从我阅读文档可以看出,您有时需要调用OptionSetparse方法。 When you do, it processes your actions and returns "A List<string> containing all unhandled arguments." 完成后,它将处理您的操作并返回“包含所有未处理参数的List<string> ”。

Unfortunately, you also need to pass it the main method's argument to get this to work. 不幸的是,您还需要向它传递main方法的参数才能使它起作用。

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 编辑:如果我的链接(仍然)不起作用,则解析应链接到http://www.ndesk.org/doc/ndesk-options/NDesk.Options/OptionSet.html#M:NDesk.Options.OptionSet。解析%28System.String,NDesk.Options.OptionContext%29

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将文本文件(.txt)作为命令行参数传递给控制台应用程序 - Not able to pass text file(.txt) as command line argument to console application 如何在C#控制台中的单行中读取矩阵行元素 - how to read matrix row elements in single line in C# console .NET Console.Readline不读取输入的第一行 - .NET Console.Readline doesn't read the first line entered 从单行给出的控制台读取数字,用空格分隔 - Read numbers from the console given in a single line, separated by a space 在c#console应用程序中读取多行作为输入 - Read multiple line as input in c# console application C#控制台应用程序不会从命令行读取,因为它应该 - C# Console Application Does NOT read from the command line, as it should C#-在同一行上读取2个字符串,在控制台应用程序上用空格隔开 - C# - Read 2 strings on the same line separated by whitespace on a console application 控制台中带有参数的应用程序 - application with argument in the console 新线从哪里来。 从文件读取并放入字符串。 然后输出到控制台 - Where does the new line come from. Read from a file and put in a string. Then output to the console 使streamreader读取文本文件中的特定行,然后写入控制台C# - Getting streamreader to read specific line in text file, then write to console c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM