1
- // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information.
1
+ // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information.
2
+
3
+ using CSharpx ;
2
4
3
5
using System ;
4
6
using System . Collections . Generic ;
5
7
using System . Linq ;
6
- using CSharpx ;
7
8
8
9
namespace CommandLine . Core
9
10
{
@@ -16,12 +17,43 @@ public static IEnumerable<Func<IEnumerable<SpecificationProperty>, IEnumerable<E
16
17
return new List < Func < IEnumerable < SpecificationProperty > , IEnumerable < Error > > >
17
18
{
18
19
EnforceMutuallyExclusiveSet ( ) ,
20
+ EnforceGroup ( ) ,
19
21
EnforceRequired ( ) ,
20
22
EnforceRange ( ) ,
21
23
EnforceSingle ( tokens )
22
24
} ;
23
25
}
24
26
27
+ private static Func < IEnumerable < SpecificationProperty > , IEnumerable < Error > > EnforceGroup ( )
28
+ {
29
+ return specProps =>
30
+ {
31
+ var optionsValues =
32
+ from sp in specProps
33
+ where sp . Specification . IsOption ( )
34
+ let o = ( OptionSpecification ) sp . Specification
35
+ where o . Group . Length > 0
36
+ select new
37
+ {
38
+ Option = o ,
39
+ Value = sp . Value
40
+ } ;
41
+
42
+ var groups = from o in optionsValues
43
+ group o by o . Option . Group into g
44
+ select g ;
45
+
46
+ var errorGroups = groups . Where ( gr => gr . All ( g => g . Value . IsNothing ( ) ) ) ;
47
+
48
+ if ( errorGroups . Any ( ) )
49
+ {
50
+ return errorGroups . Select ( gr => new MissingGroupOptionError ( gr . Key , gr . Select ( g => new NameInfo ( g . Option . ShortName , g . Option . LongName ) ) ) ) ;
51
+ }
52
+
53
+ return Enumerable . Empty < Error > ( ) ;
54
+ } ;
55
+ }
56
+
25
57
private static Func < IEnumerable < SpecificationProperty > , IEnumerable < Error > > EnforceMutuallyExclusiveSet ( )
26
58
{
27
59
return specProps =>
@@ -51,26 +83,27 @@ private static Func<IEnumerable<SpecificationProperty>, IEnumerable<Error>> Enfo
51
83
return specProps =>
52
84
{
53
85
var requiredWithValue = from sp in specProps
54
- where sp . Specification . IsOption ( )
55
- where sp . Specification . Required
56
- where sp . Value . IsJust ( )
57
- let o = ( OptionSpecification ) sp . Specification
58
- where o . SetName . Length > 0
59
- select sp . Specification ;
86
+ where sp . Specification . IsOption ( )
87
+ where sp . Specification . Required
88
+ where sp . Value . IsJust ( )
89
+ let o = ( OptionSpecification ) sp . Specification
90
+ where o . SetName . Length > 0
91
+ select sp . Specification ;
60
92
var setWithRequiredValue = (
61
93
from s in requiredWithValue
62
94
let o = ( OptionSpecification ) s
63
95
where o . SetName . Length > 0
64
96
select o . SetName )
65
97
. Distinct ( ) ;
66
98
var requiredWithoutValue = from sp in specProps
67
- where sp . Specification . IsOption ( )
68
- where sp . Specification . Required
69
- where sp . Value . IsNothing ( )
70
- let o = ( OptionSpecification ) sp . Specification
71
- where o . SetName . Length > 0
72
- where setWithRequiredValue . ContainsIfNotEmpty ( o . SetName )
73
- select sp . Specification ;
99
+ where sp . Specification . IsOption ( )
100
+ where sp . Specification . Required
101
+ where sp . Value . IsNothing ( )
102
+ let o = ( OptionSpecification ) sp . Specification
103
+ where o . SetName . Length > 0
104
+ where o . Group . Length == 0
105
+ where setWithRequiredValue . ContainsIfNotEmpty ( o . SetName )
106
+ select sp . Specification ;
74
107
var missing =
75
108
requiredWithoutValue
76
109
. Except ( requiredWithValue )
@@ -81,6 +114,7 @@ where sp.Specification.Required
81
114
where sp . Value . IsNothing ( )
82
115
let o = ( OptionSpecification ) sp . Specification
83
116
where o . SetName . Length == 0
117
+ where o . Group . Length == 0
84
118
select sp . Specification )
85
119
. Concat (
86
120
from sp in specProps
@@ -130,11 +164,11 @@ from o in to.DefaultIfEmpty()
130
164
where o != null
131
165
select new { o . ShortName , o . LongName } ;
132
166
var longOptions = from t in tokens
133
- where t . IsName ( )
134
- join o in specs on t . Text equals o . LongName into to
135
- from o in to . DefaultIfEmpty ( )
136
- where o != null
137
- select new { o . ShortName , o . LongName } ;
167
+ where t . IsName ( )
168
+ join o in specs on t . Text equals o . LongName into to
169
+ from o in to . DefaultIfEmpty ( )
170
+ where o != null
171
+ select new { o . ShortName , o . LongName } ;
138
172
var groups = from x in shortOptions . Concat ( longOptions )
139
173
group x by x into g
140
174
let count = g . Count ( )
@@ -155,4 +189,4 @@ private static bool ContainsIfNotEmpty<T>(this IEnumerable<T> sequence, T value)
155
189
return true ;
156
190
}
157
191
}
158
- }
192
+ }
0 commit comments