@@ -26,10 +26,13 @@ internal class CommandLineApplication
26
26
// options.
27
27
private readonly bool _continueAfterUnexpectedArg ;
28
28
29
- public CommandLineApplication ( bool throwOnUnexpectedArg = true , bool continueAfterUnexpectedArg = false )
29
+ private readonly bool _treatUnmatchedOptionsAsArguments ;
30
+
31
+ public CommandLineApplication ( bool throwOnUnexpectedArg = true , bool continueAfterUnexpectedArg = false , bool treatUnmatchedOptionsAsArguments = false )
30
32
{
31
33
_throwOnUnexpectedArg = throwOnUnexpectedArg ;
32
34
_continueAfterUnexpectedArg = continueAfterUnexpectedArg ;
35
+ _treatUnmatchedOptionsAsArguments = treatUnmatchedOptionsAsArguments ;
33
36
Options = new List < CommandOption > ( ) ;
34
37
Arguments = new List < CommandArgument > ( ) ;
35
38
Commands = new List < CommandLineApplication > ( ) ;
@@ -136,6 +139,7 @@ public int Execute(params string[] args)
136
139
CommandLineApplication command = this ;
137
140
CommandOption option = null ;
138
141
IEnumerator < CommandArgument > arguments = null ;
142
+ var argumentsAssigned = false ;
139
143
140
144
for ( var index = 0 ; index < args . Length ; index ++ )
141
145
{
@@ -161,6 +165,25 @@ public int Execute(params string[] args)
161
165
var longOptionName = longOption [ 0 ] ;
162
166
option = command . GetOptions ( ) . SingleOrDefault ( opt => string . Equals ( opt . LongName , longOptionName , StringComparison . Ordinal ) ) ;
163
167
168
+ if ( option == null && _treatUnmatchedOptionsAsArguments )
169
+ {
170
+ if ( arguments == null )
171
+ {
172
+ arguments = new CommandArgumentEnumerator ( command . Arguments . GetEnumerator ( ) ) ;
173
+ }
174
+ if ( arguments . MoveNext ( ) )
175
+ {
176
+ processed = true ;
177
+ arguments . Current . Values . Add ( arg ) ;
178
+ argumentsAssigned = true ;
179
+ continue ;
180
+ }
181
+ //else
182
+ //{
183
+ // argumentsAssigned = false;
184
+ //}
185
+ }
186
+
164
187
if ( option == null )
165
188
{
166
189
var ignoreContinueAfterUnexpectedArg = false ;
@@ -221,6 +244,25 @@ public int Execute(params string[] args)
221
244
processed = true ;
222
245
option = command . GetOptions ( ) . SingleOrDefault ( opt => string . Equals ( opt . ShortName , shortOption [ 0 ] , StringComparison . Ordinal ) ) ;
223
246
247
+ if ( option == null && _treatUnmatchedOptionsAsArguments )
248
+ {
249
+ if ( arguments == null )
250
+ {
251
+ arguments = new CommandArgumentEnumerator ( command . Arguments . GetEnumerator ( ) ) ;
252
+ }
253
+ if ( arguments . MoveNext ( ) )
254
+ {
255
+ processed = true ;
256
+ arguments . Current . Values . Add ( arg ) ;
257
+ argumentsAssigned = true ;
258
+ continue ;
259
+ }
260
+ //else
261
+ //{
262
+ // argumentsAssigned = false;
263
+ //}
264
+ }
265
+
224
266
// If not a short option, try symbol option
225
267
if ( option == null )
226
268
{
@@ -278,7 +320,7 @@ public int Execute(params string[] args)
278
320
option = null ;
279
321
}
280
322
281
- if ( ! processed && arguments == null )
323
+ if ( ! processed && ! argumentsAssigned )
282
324
{
283
325
var currentCommand = command ;
284
326
foreach ( var subcommand in command . Commands )
0 commit comments