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 ;
24
35using System ;
46using System . Collections . Generic ;
57using System . Linq ;
6- using CSharpx ;
78
89namespace CommandLine . Core
910{
@@ -16,12 +17,43 @@ public static IEnumerable<Func<IEnumerable<SpecificationProperty>, IEnumerable<E
1617 return new List < Func < IEnumerable < SpecificationProperty > , IEnumerable < Error > > >
1718 {
1819 EnforceMutuallyExclusiveSet ( ) ,
20+ EnforceGroup ( ) ,
1921 EnforceRequired ( ) ,
2022 EnforceRange ( ) ,
2123 EnforceSingle ( tokens )
2224 } ;
2325 }
2426
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+
2557 private static Func < IEnumerable < SpecificationProperty > , IEnumerable < Error > > EnforceMutuallyExclusiveSet ( )
2658 {
2759 return specProps =>
@@ -51,26 +83,27 @@ private static Func<IEnumerable<SpecificationProperty>, IEnumerable<Error>> Enfo
5183 return specProps =>
5284 {
5385 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 ;
6092 var setWithRequiredValue = (
6193 from s in requiredWithValue
6294 let o = ( OptionSpecification ) s
6395 where o . SetName . Length > 0
6496 select o . SetName )
6597 . Distinct ( ) ;
6698 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 ;
74107 var missing =
75108 requiredWithoutValue
76109 . Except ( requiredWithValue )
@@ -81,6 +114,7 @@ where sp.Specification.Required
81114 where sp . Value . IsNothing ( )
82115 let o = ( OptionSpecification ) sp . Specification
83116 where o . SetName . Length == 0
117+ where o . Group . Length == 0
84118 select sp . Specification )
85119 . Concat (
86120 from sp in specProps
@@ -130,11 +164,11 @@ from o in to.DefaultIfEmpty()
130164 where o != null
131165 select new { o . ShortName , o . LongName } ;
132166 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 } ;
138172 var groups = from x in shortOptions . Concat ( longOptions )
139173 group x by x into g
140174 let count = g . Count ( )
@@ -155,4 +189,4 @@ private static bool ContainsIfNotEmpty<T>(this IEnumerable<T> sequence, T value)
155189 return true ;
156190 }
157191 }
158- }
192+ }
0 commit comments