简体   繁体   English

NDesk.Options / Mono.Options:具有多个键/值对的参数

[英]NDesk.Options/Mono.Options: Parameter with multiple key/value pairs

I need to parse the following command line syntax: 我需要解析以下命令行语法:

MyApplication.exe /p1 key1=value1 key2=value2 key3=value3 /p2

key1, key2 & key3 belong to parameter p1. key1,key2和key3属于参数p1。

I've found Example #3 in the documentation, which shows a way to parse for a single key/value pair. 我在文档中找到了示例#3 ,它显示了解析单个键/值对的方法。

Is parsing for multiple key/value pairs for a single parameter possible with NDesk.Options 使用NDesk.Options解析单个参数的多个键/值对

There's a more fundamental question in play: is there a limit to the number of key=value sets on the command line? 还有一个更基本的问题:命令行上key = value集的数量是否有限制?

If the number of key=value sets is variable, then you want to use argument runs as permitted by the <> default handler : 如果key = value集的数量是可变的,那么您希望使用<>默认处理程序允许的参数运行

Dictionary<string, string> cur = null;
Dictionary<string, string> p1 = new Dictionary<string, string>();
Dictionary<string, string> p2 = new Dictionary<string, string>();
var p = new OptionSet () {
    { "p1", v => { cur = p1; } },
    { "p2", v => { cur = p2; } },
    { "<>", v => {
        string[] values = v.Split (new[]{'=', ':'}, 2);
        cur.Add (values [0], values [1]);
    } },
};

This will split all key=value options after /p1 and add them to the p1 dictionary: 这将在/p1之后拆分所有key = value选项并将它们添加到p1字典:

p.Parse (new[]{"/p1", "key1=value1", "key2=value2", "/p2"});
// `p1` now contains { { "key1", "value1" }, {"key2", "value2" } }

For obvious reasons, I'd consider the above to be the reasonable way to go. 出于显而易见的原因,我认为上述是合理的方式。

However, if there will always be 3 sets (and thus 6 required values), you could instead create an Option subclass which requires 6 values: 但是,如果总有3组(因此需要6个值),则可以创建一个需要6个值的Option子类:

class ActionOption<T1, T2, T3, T4, T5, T6> : Option {
    Action<T1, T2, T3, T4, T5, T6> action;
    public ActionOption (string prototype, string description,
             Action<T1, T2, T3, T4, T5, T6> action)
        : base (prototype, description, 6)
    {
        this.action = action;
    }

    protected override void OnParseComplete (OptionContext c)
    {
        action (
                Parse<T1>(c.OptionValues [0], c)),
                Parse<T2>(c.OptionValues [1], c)),
                Parse<T3>(c.OptionValues [2], c)),
                Parse<T4>(c.OptionValues [3], c)),
                Parse<T5>(c.OptionValues [4], c)),
                Parse<T6>(c.OptionValues [5], c)));
    }
}

You can then provide this ActionOption to OptionSet.Add(Option) : 然后,您可以将此ActionOption提供给OptionSet.Add(Option)

var p = new OptionSet {
    new ActionOption<string, string, string, string, string, string> (
            "p1", null, (a, b, c, d, e, f) => {...}),
};

NDesk.Options has special syntax to suport exactly this: NDesk.Options有特殊的语法来支持这个:

        var pars = new Hashtable();
        var opts = new NDesk.Options.OptionSet{
            {"p={:}{/}", (n,v) => pars.Add(n, v)}
        };

Now you can run your program with command line like this: 现在您可以使用命令行运行您的程序,如下所示:

-p=key1:value1 -p=key2/value2

Notice, that you can set pairs delimiter in options configuration. 请注意,您可以在选项配置中设置对分隔符。 In this example it is [:/] 在这个例子中它是[:/]

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

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