简体   繁体   English

如何使用C#中的NDesk选项解析一个简单的项目列表

[英]How to parse a simple list of items with NDesk Options in C#

I am new to the NDesk.Options library. 我是NDesk.Options库的新手。 Can't figure out the simplest way to parse a simple list of items. 无法找出解析简单项目列表的最简单方法。

Example: prog --items=item1 item2 item3 (I want to use a List items in the code) 示例:prog --items = item1 item2 item3(我想在代码中使用List项)

It should support quoting as well as I want to use the item list as list of file or dir names. 它应该支持引用以及我想使用项目列表作为文件或目录名称列表。

prog --items="c:\\a\\b\\c.txt" "c:\\prog files\\d.txt" prog --dirs="c:\\prog files\\" "d:\\x\\y\\space in dirname" prog --items =“c:\\ a \\ b \\ c.txt”“c:\\ prog files \\ d.txt”prog --dirs =“c:\\ prog files \\”“d:\\ x \\ y \\ space在dirname“

An alternative to accept a list of values for a single argument is to accept the same argument multiple times. 接受单个参数的值列表的替代方法是多次接受相同的参数。 For example, 例如,

prog --file="c:\\a\\b\\c.txt" --file="c:\\prog files\\d.txt" prog --file =“c:\\ a \\ b \\ c.txt”--file =“c:\\ prog files \\ d.txt”

In which case, your code would look something like this. 在这种情况下,您的代码看起来像这样。

List<string> fileList = new List<string>();

OptionSet options = new OptionSet
{
    { "f|file", "File name. Repeat argument for each file.", v =>
        {
            fileList.Add(v);
        }
    }
};

options.Parse(args);

IMHO, this code is easier to read and maintain. 恕我直言,这段代码更容易阅读和维护。

You can use the "<>" input which denotes that no flag was associated with the input. 您可以使用“<>”输入,表示没有标志与输入关联。 Since the options are read left-to-right, you can set a 'currentParameter' flag when the starting flag is encountered, and assume any subsequent inputs without a flag are part of the list. 由于选项是从左向右读取的,因此可以在遇到起始标志时设置“currentParameter”标志,并假设没有标志的任何后续输入都是列表的一部分。 Here is an example where we can specify a List as the input Files, and a Dictionary (Parameters) which are a list of key-value pairs. 下面是一个示例,我们可以将List指定为输入文件,并将Dictionary(参数)指定为键值对列表。 Other variations are of course available as well. 当然也可以使用其他变体。

OptionSet options = new OptionSet()
        {
            {"f|file", "a list of files" , v => {
                currentParameter = "f";
            }},
            {"p", @"Parameter values to use for variable resolution in the xml - use the form 'Name=Value'.  a ':' or ';' may be used in place of the equals sign", v => {
                currentParameter = "p";
            }},
            { "<>", v => {
                switch(currentParameter) {
                    case "p":
                        string[] items = v.Split(new[]{'=', ':', ';'}, 2);
                        Parameters.Add(items[0], items[1]);
                        break;
                    case "f":
                        Files.Add(Path.Combine(Environment.CurrentDirectory, v));
                        break;
                }
            }}
        };
options.Parse(args);

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

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