BadScript 2
Loading...
Searching...
No Matches
SpecificationExtensions.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
7namespace CommandLine.Core
8{
9 internal static class SpecificationExtensions
10 {
11 public static bool IsOption(this Specification specification)
12 {
13 return specification.Tag == SpecificationType.Option;
14 }
15
16 public static bool IsValue(this Specification specification)
17 {
18 return specification.Tag == SpecificationType.Value;
19 }
20
21 public static OptionSpecification WithLongName(this OptionSpecification specification, string newLongName)
22 {
23 return new OptionSpecification(specification.ShortName,
24 newLongName,
25 specification.Required,
26 specification.SetName,
27 specification.Min,
28 specification.Max,
29 specification.Separator,
30 specification.DefaultValue,
31 specification.HelpText,
32 specification.MetaValue,
33 specification.EnumValues,
34 specification.ConversionType,
35 specification.TargetType,
36 specification.Group,
37 specification.FlagCounter,
38 specification.Hidden
39 );
40 }
41
42 public static string UniqueName(this OptionSpecification specification)
43 {
44 return specification.ShortName.Length > 0 ? specification.ShortName : specification.LongName;
45 }
46
47 public static IEnumerable<Specification> ThrowingValidate(this IEnumerable<Specification> specifications,
48 IEnumerable<Tuple<Func<Specification, bool>, string>>
49 guardsLookup)
50 {
51 foreach (Tuple<Func<Specification, bool>, string> guard in guardsLookup)
52 {
53 if (specifications.Any(spec => guard.Item1(spec)))
54 {
55 throw new InvalidOperationException(guard.Item2);
56 }
57 }
58
59 return specifications;
60 }
61
62 public static bool HavingRange(this Specification specification, Func<int, int, bool> predicate)
63 {
64 int min;
65 int max;
66
67 if (specification.Min.MatchJust(out min) && specification.Max.MatchJust(out max))
68 {
69 return predicate(min, max);
70 }
71
72 return false;
73 }
74
75 public static bool HavingMin(this Specification specification, Func<int, bool> predicate)
76 {
77 int min;
78
79 if (specification.Min.MatchJust(out min))
80 {
81 return predicate(min);
82 }
83
84 return false;
85 }
86
87 public static bool HavingMax(this Specification specification, Func<int, bool> predicate)
88 {
89 int max;
90
91 if (specification.Max.MatchJust(out max))
92 {
93 return predicate(max);
94 }
95
96 return false;
97 }
98 }
99}
bool FlagCounter
Whether this is an int option that counts how many times a flag was set rather than taking a value on...
static string UniqueName(this OptionSpecification specification)
static bool HavingMin(this Specification specification, Func< int, bool > predicate)
static bool IsOption(this Specification specification)
static IEnumerable< Specification > ThrowingValidate(this IEnumerable< Specification > specifications, IEnumerable< Tuple< Func< Specification, bool >, string > > guardsLookup)
static OptionSpecification WithLongName(this OptionSpecification specification, string newLongName)
static bool HavingMax(this Specification specification, Func< int, bool > predicate)
static bool IsValue(this Specification specification)
static bool HavingRange(this Specification specification, Func< int, int, bool > predicate)
Type ConversionType
This information is denormalized to decouple Specification from PropertyInfo.
IEnumerable< string > EnumValues