BadScript 2
Loading...
Searching...
No Matches
OptionMapper.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;
4using System.Collections.Generic;
5using System.Linq;
6
7using CSharpx;
8
10
11namespace CommandLine.Core
12{
13 internal static class OptionMapper
14 {
15 public static Result<
16 IEnumerable<SpecificationProperty>, Error>
17 MapValues(IEnumerable<SpecificationProperty> propertyTuples,
18 IEnumerable<KeyValuePair<string, IEnumerable<string>>> options,
19 Func<IEnumerable<string>, Type, bool, bool, Maybe<object>> converter,
20 StringComparer comparer)
21 {
22 IEnumerable<Tuple<SpecificationProperty, Maybe<Error>>> sequencesAndErrors = propertyTuples
23 .Select(pt =>
24 {
26 s.Key.MatchName(((OptionSpecification)pt.Specification).ShortName,
27 ((OptionSpecification)pt.Specification).LongName,
28 comparer
29 )
30 )
31 .ToMaybe();
32
33 if (matched.IsJust())
34 {
35 IEnumerable<KeyValuePair<string, IEnumerable<string>>> matches =
36 matched.GetValueOrDefault(Enumerable
37 .Empty<KeyValuePair<string, IEnumerable<string>>>()
38 );
39 List<string> values = new List<string>();
40
41 foreach (KeyValuePair<string, IEnumerable<string>> kvp in matches)
42 {
43 foreach (string value in kvp.Value)
44 {
45 values.Add(value);
46 }
47 }
48
49 bool isFlag = pt.Specification.Tag == SpecificationType.Option &&
50 ((OptionSpecification)pt.Specification).FlagCounter;
51
52 return converter(values,
53 isFlag ? typeof(bool) : pt.Property.PropertyType,
54 pt.Specification.TargetType != TargetType.Sequence,
55 isFlag
56 )
57 .Select(value => Tuple.Create(pt.WithValue(Maybe.Just(value)),
58 Maybe.Nothing<Error>()
59 )
60 )
61 .GetValueOrDefault(Tuple.Create<SpecificationProperty, Maybe<Error>>(pt,
62 Maybe.Just<Error>(new
64 pt.Specification)
65 .FromOptionSpecification()
66 )
67 )
68 )
69 );
70 }
71
72 return Tuple.Create(pt, Maybe.Nothing<Error>());
73 }
74 )
75 .Memoize();
76
77 return Result.Succeed(sequencesAndErrors.Select(se => se.Item1),
78 sequencesAndErrors.Select(se => se.Item2)
79 .OfType<Just<Error>>()
80 .Select(se => se.Value)
81 );
82 }
83 }
84}
Models a CSharpx.Maybe when contains a value.
Definition Maybe.cs:88
The Maybe type models an optional value. A value of type Maybe a either contains a value of type a (r...
Definition Maybe.cs:33
Models an error generated when a value conversion fails.
Definition Error.cs:418
static Result< IEnumerable< SpecificationProperty >, Error > MapValues(IEnumerable< SpecificationProperty > propertyTuples, IEnumerable< KeyValuePair< string, IEnumerable< string > > > options, Func< IEnumerable< string >, Type, bool, bool, Maybe< object > > converter, StringComparer comparer)
Base type of all errors.
Definition Error.cs:110
Represents the result of a computation.