@@ -118,32 +118,32 @@ private void BuildCommands(CommandLineBuilder rootBuilder, Type type)
118
118
{
119
119
command = new Command ( commandAttribute . Name , commandAttribute . Help ) ;
120
120
var properties = new List < ( PropertyInfo , Option ) > ( ) ;
121
- PropertyInfo argument = null ;
121
+ var arguments = new List < ( PropertyInfo , Argument ) > ( ) ;
122
122
123
123
foreach ( PropertyInfo property in type . GetProperties ( ) . Where ( p => p . CanWrite ) )
124
124
{
125
125
var argumentAttribute = ( ArgumentAttribute ) property . GetCustomAttributes ( typeof ( ArgumentAttribute ) , inherit : false ) . SingleOrDefault ( ) ;
126
126
if ( argumentAttribute != null )
127
127
{
128
- if ( argument != null ) {
129
- throw new ArgumentException ( $ "More than one ArgumentAttribute in command class: { type . Name } ") ;
130
- }
131
128
IArgumentArity arity = property . PropertyType . IsArray ? ArgumentArity . ZeroOrMore : ArgumentArity . ZeroOrOne ;
132
129
133
- command . Argument = new Argument {
130
+ var argument = new Argument {
134
131
Name = argumentAttribute . Name ?? property . Name . ToLowerInvariant ( ) ,
135
132
Description = argumentAttribute . Help ,
136
133
ArgumentType = property . PropertyType ,
137
134
Arity = arity
138
135
} ;
139
- argument = property ;
136
+ command . AddArgument ( argument ) ;
137
+ arguments . Add ( ( property , argument ) ) ;
140
138
}
141
139
else
142
140
{
143
141
var optionAttribute = ( OptionAttribute ) property . GetCustomAttributes ( typeof ( OptionAttribute ) , inherit : false ) . SingleOrDefault ( ) ;
144
142
if ( optionAttribute != null )
145
143
{
146
- var option = new Option ( optionAttribute . Name ?? BuildAlias ( property . Name ) , optionAttribute . Help , new Argument { ArgumentType = property . PropertyType } ) ;
144
+ var option = new Option ( optionAttribute . Name ?? BuildAlias ( property . Name ) , optionAttribute . Help ) {
145
+ Argument = new Argument { ArgumentType = property . PropertyType }
146
+ } ;
147
147
command . AddOption ( option ) ;
148
148
properties . Add ( ( property , option ) ) ;
149
149
@@ -160,7 +160,7 @@ private void BuildCommands(CommandLineBuilder rootBuilder, Type type)
160
160
}
161
161
}
162
162
163
- var handler = new Handler ( this , commandAttribute . AliasExpansion , argument , properties , type ) ;
163
+ var handler = new Handler ( this , commandAttribute . AliasExpansion , arguments , properties , type ) ;
164
164
_commandHandlers . Add ( command . Name , handler ) ;
165
165
command . Handler = handler ;
166
166
@@ -201,18 +201,18 @@ class Handler : ICommandHandler
201
201
{
202
202
private readonly CommandProcessor _commandProcessor ;
203
203
private readonly string _aliasExpansion ;
204
- private readonly PropertyInfo _argument ;
204
+ private readonly IEnumerable < ( PropertyInfo Property , Argument Argument ) > _arguments ;
205
205
private readonly IEnumerable < ( PropertyInfo Property , Option Option ) > _properties ;
206
206
207
207
private readonly ConstructorInfo _constructor ;
208
208
private readonly MethodInfo _methodInfo ;
209
209
private readonly MethodInfo _methodInfoHelp ;
210
210
211
- public Handler ( CommandProcessor commandProcessor , string aliasExpansion , PropertyInfo argument , IEnumerable < ( PropertyInfo , Option ) > properties , Type type )
211
+ public Handler ( CommandProcessor commandProcessor , string aliasExpansion , IEnumerable < ( PropertyInfo , Argument ) > arguments , IEnumerable < ( PropertyInfo , Option ) > properties , Type type )
212
212
{
213
213
_commandProcessor = commandProcessor ;
214
214
_aliasExpansion = aliasExpansion ;
215
- _argument = argument ;
215
+ _arguments = arguments ;
216
216
_properties = properties ;
217
217
218
218
_constructor = type . GetConstructors ( ) . SingleOrDefault ( ( info ) => info . GetParameters ( ) . Length == 0 ) ??
@@ -303,19 +303,19 @@ private void SetProperties(InvocationContext context, object instance)
303
303
property . Property . SetValue ( instance , value ) ;
304
304
}
305
305
306
- if ( context != null && _argument != null )
306
+ if ( context != null )
307
307
{
308
- object value = null ;
309
- ArgumentResult result = context . ParseResult . CommandResult . ArgumentResult ;
310
- switch ( result )
308
+ IEnumerable < ArgumentResult > argumentResults = context . ParseResult . CommandResult . Children . OfType < ArgumentResult > ( ) ;
309
+
310
+ foreach ( ( PropertyInfo Property , Argument Argument ) argument in _arguments )
311
311
{
312
- case SuccessfulArgumentResult successful :
313
- value = successful . Value ;
314
- break ;
315
- case FailedArgumentResult failed :
316
- throw new InvalidOperationException ( failed . ErrorMessage ) ;
312
+ ArgumentResult argumentResult = argumentResults . Where ( ( result ) => result . Argument == argument . Argument ) . SingleOrDefault ( ) ;
313
+ if ( argumentResult != null )
314
+ {
315
+ object value = argumentResult . GetValueOrDefault ( ) ;
316
+ argument . Property . SetValue ( instance , value ) ;
317
+ }
317
318
}
318
- _argument . SetValue ( instance , value ) ;
319
319
}
320
320
}
321
321
0 commit comments