forked from commandlineparser/commandline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpecificationExtensions.cs
91 lines (81 loc) · 3.07 KB
/
SpecificationExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information.
using System;
using System.Collections.Generic;
using System.Linq;
namespace CommandLine.Core
{
static class SpecificationExtensions
{
public static bool IsOption(this Specification specification)
{
return specification.Tag == SpecificationType.Option;
}
public static bool IsValue(this Specification specification)
{
return specification.Tag == SpecificationType.Value;
}
public static OptionSpecification WithLongName(this OptionSpecification specification, string newLongName)
{
return new OptionSpecification(
specification.ShortName,
newLongName,
specification.Required,
specification.SetName,
specification.Min,
specification.Max,
specification.Separator,
specification.DefaultValue,
specification.HelpText,
specification.MetaValue,
specification.EnumValues,
specification.ConversionType,
specification.TargetType,
specification.Group,
specification.FlagCounter,
specification.Hidden);
}
public static string UniqueName(this OptionSpecification specification)
{
return specification.ShortName.Length > 0 ? specification.ShortName : specification.LongName;
}
public static IEnumerable<Specification> ThrowingValidate(this IEnumerable<Specification> specifications, IEnumerable<Tuple<Func<Specification, bool>, string>> guardsLookup)
{
foreach (var guard in guardsLookup)
{
if (specifications.Any(spec => guard.Item1(spec)))
{
throw new InvalidOperationException(guard.Item2);
}
}
return specifications;
}
public static bool HavingRange(this Specification specification, Func<int, int, bool> predicate)
{
int min;
int max;
if (specification.Min.MatchJust(out min) && specification.Max.MatchJust(out max))
{
return predicate(min, max);
}
return false;
}
public static bool HavingMin(this Specification specification, Func<int, bool> predicate)
{
int min;
if (specification.Min.MatchJust(out min))
{
return predicate(min);
}
return false;
}
public static bool HavingMax(this Specification specification, Func<int, bool> predicate)
{
int max;
if (specification.Max.MatchJust(out max))
{
return predicate(max);
}
return false;
}
}
}