Es gibt eine grundsätzliche Frage im Spiel: Gibt es eine Begrenzung für die Anzahl der Schlüssel = Wert setzt auf der Kommandozeile?
Wenn die Anzahl der Schlüssel = Wert setzt die Variable ist, dann möchten Sie Argument verwenden, läuft wie vom <>
default handler erlaubt:
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]);
} },
};
Dadurch werden alle Schlüssel = Wert Optionen nach /p1
aufgeteilt und fügen Sie zum p1
Wörterbuch:
p.Parse (new[]{"/p1", "key1=value1", "key2=value2", "/p2"});
// `p1` now contains { { "key1", "value1" }, {"key2", "value2" } }
Aus offensichtlichen Gründen, würde ich das oben sein der vernünftige Weg zu gehen, in Betracht ziehen.
Wenn es jedoch immer 3 Sätze sein wird (und damit 6 erforderlichen Werte), könnten Sie stattdessen eine Option Unterklasse erstellen, die 6 Werte erfordert:
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)));
}
}
Sie können dann diese ActionOption zu OptionSet.Add(Option)
bieten:
var p = new OptionSet {
new ActionOption<string, string, string, string, string, string> (
"p1", null, (a, b, c, d, e, f) => {...}),
};