@@ -106,7 +106,7 @@ private Analyser(Processor.Context context) {
106
106
.addModifiers (PRIVATE , FINAL )
107
107
.build ();
108
108
this .readOption = readOptionMethod (keysClass , longNames , shortNames , option .optionClass );
109
- this .read = readMethod (keysClass , readOption , readArgument , optMapType , sMapType , flagsType ,
109
+ this .read = readMethod (context , keysClass , readOption , readArgument , optMapType , sMapType , flagsType ,
110
110
option .optionClass , optionType , optionTypeClass , removeFirstFlag );
111
111
}
112
112
@@ -258,6 +258,7 @@ private static MethodSpec readArgumentMethod() {
258
258
}
259
259
260
260
private static MethodSpec readMethod (
261
+ Context context ,
261
262
ClassName keysClass ,
262
263
MethodSpec readOption ,
263
264
MethodSpec readArgument ,
@@ -281,14 +282,27 @@ private static MethodSpec readMethod(
281
282
ParameterSpec token = ParameterSpec .builder (STRING , "token" ).build ();
282
283
ParameterSpec option = ParameterSpec .builder (optionClass , "option" ).build ();
283
284
ParameterSpec ignore = ParameterSpec .builder (optionClass , "__" ).build ();
284
- //@formatter:off
285
+
285
286
CodeBlock .Builder builder = CodeBlock .builder ()
286
287
.addStatement ("$T $N = $N" , STRING , token , originalToken )
287
- .addStatement ("$T $N = $N($N, $N)" , option .type , option , readOption , names , token )
288
- .beginControlFlow ("if ($N == null)" , option )
289
- .addStatement ("$N.add($N)" , otherTokens , token )
290
- .addStatement ("return" )
291
- .endControlFlow ()
288
+ .addStatement ("$T $N = $N($N, $N)" , option .type , option , readOption , names , token );
289
+
290
+ // @formatter:off
291
+ // handle "other" token
292
+ builder .beginControlFlow ("if ($N == null)" , option );
293
+ if (context .otherTokens ) {
294
+ builder .addStatement ("$N.add($N)" , otherTokens , token )
295
+ .addStatement ("return" );
296
+ } else {
297
+ builder .addStatement ("throw new $T($S + $N)" , IllegalArgumentException .class ,
298
+ "Unknown token: " , originalToken );
299
+ }
300
+ builder .endControlFlow ();
301
+ // @formatter:on
302
+
303
+ // @formatter:off
304
+ // handle flag or option group
305
+ builder
292
306
.beginControlFlow ("while ($N.$N == $T.$L)" , option , optionType , optionTypeClass , OptionType .FLAG )
293
307
.beginControlFlow ("if (!$N.add($N))" , flags , option )
294
308
.add (repetitionErrorInGroup (option , originalToken ))
@@ -302,7 +316,12 @@ private static MethodSpec readMethod(
302
316
.addStatement ("throw new $T($S + $N)" , IllegalArgumentException .class ,
303
317
"Missing value after token: " , originalToken )
304
318
.endControlFlow ()
305
- .endControlFlow ()
319
+ .endControlFlow ();
320
+ // @formatter:on
321
+
322
+ // @formatter:off
323
+ // handle option
324
+ builder
306
325
.addStatement ("$T $N = $N($N, $N)" , argument .type , argument , readArgument , token , it )
307
326
.beginControlFlow ("if ($N.$N == $T.$L)" , option , optionType , optionTypeClass , OptionType .OPTIONAL )
308
327
.beginControlFlow ("if ($N.containsKey($N))" , sMap , option )
@@ -321,8 +340,8 @@ private static MethodSpec readMethod(
321
340
bucket .type , bucket , optMap , option , ignore , ArrayList .class )
322
341
.addStatement ("$N.add($N)" , bucket , argument )
323
342
.endControlFlow ();
343
+ // @formatter:on
324
344
325
- //@formatter:on
326
345
return MethodSpec .methodBuilder ("read" )
327
346
.addParameters (Arrays .asList (originalToken , names , optMap , sMap , flags , otherTokens , it ))
328
347
.addModifiers (STATIC , PRIVATE )
0 commit comments