BadScript 2
Loading...
Searching...
No Matches
KeyValuePairHelper.cs
Go to the documentation of this file.
1// Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information.
2
3using System.Collections.Generic;
4using System.Linq;
5
7
8using CSharpx;
9
10namespace CommandLine.Core
11{
12 internal static class KeyValuePairHelper
13 {
14 public static IEnumerable<KeyValuePair<string, IEnumerable<string>>> ForSwitch(IEnumerable<Token> tokens)
15 {
16 return tokens.Select(t => t.Text.ToKeyValuePair("true"));
17 }
18
19 public static IEnumerable<KeyValuePair<string, IEnumerable<string>>> ForScalar(IEnumerable<Token> tokens)
20 {
21 return tokens
22 .Group(2)
23 .Select(g => g[0]
24 .Text.ToKeyValuePair(g[1].Text)
25 );
26 }
27
28 public static IEnumerable<KeyValuePair<string, IEnumerable<string>>> ForSequence(IEnumerable<Token> tokens)
29 {
30 return from t in tokens.Pairwise((f, s) =>
31 f.IsName()
32 ? f.Text.ToKeyValuePair(tokens.SkipWhile(t => !t.Equals(f))
33 .SkipWhile(t => t.Equals(f))
34 .TakeWhile(v => v.IsValue())
35 .Select(x => x.Text)
36 .ToArray()
37 )
38 : string.Empty.ToKeyValuePair()
39 )
40 where t.Key.Length > 0 && t.Value.Any()
41 select t;
42 }
43
44 private static KeyValuePair<string, IEnumerable<string>> ToKeyValuePair(
45 this string value,
46 params string[] values)
47 {
48 return new KeyValuePair<string, IEnumerable<string>>(value, values);
49 }
50 }
51}
static IEnumerable< KeyValuePair< string, IEnumerable< string > > > ForSwitch(IEnumerable< Token > tokens)
static IEnumerable< KeyValuePair< string, IEnumerable< string > > > ForScalar(IEnumerable< Token > tokens)
static IEnumerable< KeyValuePair< string, IEnumerable< string > > > ForSequence(IEnumerable< Token > tokens)
static KeyValuePair< string, IEnumerable< string > > ToKeyValuePair(this string value, params string[] values)