@@ -20,18 +20,22 @@ public static ParserResult<T> Build<T>(
20
20
StringComparer nameComparer ,
21
21
bool ignoreValueCase ,
22
22
CultureInfo parsingCulture ,
23
+ bool autoHelp ,
24
+ bool autoVersion ,
23
25
IEnumerable < ErrorType > nonFatalErrors )
24
26
{
25
27
var typeInfo = factory . MapValueOrDefault ( f => f ( ) . GetType ( ) , typeof ( T ) ) ;
26
28
27
29
var specProps = typeInfo . GetSpecifications ( pi => SpecificationProperty . Create (
28
- Specification . FromProperty ( pi ) , pi , Maybe . Nothing < object > ( ) ) ) ;
30
+ Specification . FromProperty ( pi ) , pi , Maybe . Nothing < object > ( ) ) )
31
+ . Memorize ( ) ;
29
32
30
33
var specs = from pt in specProps select pt . Specification ;
31
34
32
35
var optionSpecs = specs
33
36
. ThrowingValidate ( SpecificationGuards . Lookup )
34
- . OfType < OptionSpecification > ( ) ;
37
+ . OfType < OptionSpecification > ( )
38
+ . Memorize ( ) ;
35
39
36
40
Func < T > makeDefault = ( ) =>
37
41
typeof ( T ) . IsMutable ( )
@@ -42,18 +46,19 @@ public static ParserResult<T> Build<T>(
42
46
Func < IEnumerable < Error > , ParserResult < T > > notParsed =
43
47
errs => new NotParsed < T > ( makeDefault ( ) . GetType ( ) . ToTypeInfo ( ) , errs ) ;
44
48
49
+ var argumentsList = arguments . Memorize ( ) ;
45
50
Func < ParserResult < T > > buildUp = ( ) =>
46
51
{
47
- var tokenizerResult = tokenizer ( arguments , optionSpecs ) ;
52
+ var tokenizerResult = tokenizer ( argumentsList , optionSpecs ) ;
48
53
49
- var tokens = tokenizerResult . SucceededWith ( ) ;
54
+ var tokens = tokenizerResult . SucceededWith ( ) . Memorize ( ) ;
50
55
51
56
var partitions = TokenPartitioner . Partition (
52
57
tokens ,
53
58
name => TypeLookup . FindTypeDescriptorAndSibling ( name , optionSpecs , nameComparer ) ) ;
54
- var optionsPartition = partitions . Item1 ;
55
- var valuesPartition = partitions . Item2 ;
56
- var errorsPartition = partitions . Item3 ;
59
+ var optionsPartition = partitions . Item1 . Memorize ( ) ;
60
+ var valuesPartition = partitions . Item2 . Memorize ( ) ;
61
+ var errorsPartition = partitions . Item3 . Memorize ( ) ;
57
62
58
63
var optionSpecPropsResult =
59
64
OptionMapper . MapValues (
@@ -65,7 +70,7 @@ public static ParserResult<T> Build<T>(
65
70
var valueSpecPropsResult =
66
71
ValueMapper . MapValues (
67
72
( from pt in specProps where pt . Specification . IsValue ( ) orderby ( ( ValueSpecification ) pt . Specification ) . Index select pt ) ,
68
- valuesPartition ,
73
+ valuesPartition ,
69
74
( vals , type , isScalar ) => TypeConverter . ChangeType ( vals , type , isScalar , parsingCulture , ignoreValueCase ) ) ;
70
75
71
76
var missingValueErrors = from token in errorsPartition
@@ -75,7 +80,7 @@ public static ParserResult<T> Build<T>(
75
80
. FromOptionSpecification ( ) ) ;
76
81
77
82
var specPropsWithValue =
78
- optionSpecPropsResult . SucceededWith ( ) . Concat ( valueSpecPropsResult . SucceededWith ( ) ) ;
83
+ optionSpecPropsResult . SucceededWith ( ) . Concat ( valueSpecPropsResult . SucceededWith ( ) ) . Memorize ( ) ;
79
84
80
85
var setPropertyErrors = new List < Error > ( ) ;
81
86
@@ -100,9 +105,13 @@ public static ParserResult<T> Build<T>(
100
105
{
101
106
var ctor = typeInfo . GetTypeInfo ( ) . GetConstructor ( ( from sp in specProps select sp . Property . PropertyType ) . ToArray ( ) ) ;
102
107
var values = ( from prms in ctor . GetParameters ( )
103
- join sp in specPropsWithValue on prms . Name . ToLower ( ) equals sp . Property . Name . ToLower ( )
108
+ join sp in specPropsWithValue on prms . Name . ToLower ( ) equals sp . Property . Name . ToLower ( ) into spv
109
+ from sp in spv . DefaultIfEmpty ( )
104
110
select
105
- sp . Value . GetValueOrDefault (
111
+ sp == null
112
+ ? specProps . First ( s => String . Equals ( s . Property . Name , prms . Name , StringComparison . CurrentCultureIgnoreCase ) )
113
+ . Property . PropertyType . GetDefaultValue ( )
114
+ : sp . Value . GetValueOrDefault (
106
115
sp . Specification . DefaultValue . GetValueOrDefault (
107
116
sp . Specification . ConversionType . CreateDefaultForImmutable ( ) ) ) ) . ToArray ( ) ;
108
117
var immutable = ( T ) ctor . Invoke ( values ) ;
@@ -127,11 +136,13 @@ join sp in specPropsWithValue on prms.Name.ToLower() equals sp.Property.Name.ToL
127
136
return allErrors . Except ( warnings ) . ToParserResult ( instance ) ;
128
137
} ;
129
138
130
- var preprocessorErrors = arguments . Any ( )
131
- ? arguments . Preprocess ( PreprocessorGuards . Lookup ( nameComparer ) )
132
- : Enumerable . Empty < Error > ( ) ;
139
+ var preprocessorErrors = (
140
+ argumentsList . Any ( )
141
+ ? arguments . Preprocess ( PreprocessorGuards . Lookup ( nameComparer , autoHelp , autoVersion ) )
142
+ : Enumerable . Empty < Error > ( )
143
+ ) . Memorize ( ) ;
133
144
134
- var result = arguments . Any ( )
145
+ var result = argumentsList . Any ( )
135
146
? preprocessorErrors . Any ( )
136
147
? notParsed ( preprocessorErrors )
137
148
: buildUp ( )
0 commit comments