@@ -453,73 +453,79 @@ public void parseArgs(String[] args, Object userData)
453
453
parseArgs (args , opts , userData );
454
454
}
455
455
456
- @ Override
457
- public void parseArgs (String [] args , Map <String , Object > attrs ,
458
- Object userData ) throws ArgumentParserException {
459
- parseArgs (args , 0 , attrs );
460
- for (Field field : userData .getClass ().getDeclaredFields ()) {
461
- Arg ann = field .getAnnotation (Arg .class );
462
- if (ann != null ) {
463
- String argDest = ann .dest ();
464
- if (argDest .isEmpty ()) {
465
- argDest = field .getName ();
466
- }
467
- if (!attrs .containsKey (argDest )) {
468
- continue ;
469
- }
470
- Object val = attrs .get (argDest );
471
- try {
472
- field .setAccessible (true );
473
- field .set (userData ,
474
- ReflectHelper .list2Array (field .getType (), val ));
475
- } catch (RuntimeException e ) {
476
- if (!ann .ignoreError ()) {
477
- throw e ;
478
- }
479
- } catch (Exception e ) {
480
- if (!ann .ignoreError ()) {
481
- throw new IllegalArgumentException (String .format (
482
- "Could not set %s to field %s" , val ,
483
- field .getName ()), e );
484
- }
485
- }
486
- }
487
- }
488
- for (Method method : userData .getClass ().getDeclaredMethods ()) {
489
- Arg ann = method .getAnnotation (Arg .class );
490
- if (ann != null ) {
491
- String argDest = ann .dest ();
492
- if (argDest .isEmpty ()) {
493
- argDest = method .getName ();
494
- }
495
- if (!attrs .containsKey (argDest )) {
496
- continue ;
497
- }
498
- Object val = attrs .get (argDest );
499
- Class <?> fargs [] = method .getParameterTypes ();
500
- if (fargs .length != 1 ) {
501
- throw new IllegalArgumentException (String .format (
502
- "Method %s must have one formal parameter" ,
503
- method .getName ()));
504
- }
505
- try {
506
- method .setAccessible (true );
507
- method .invoke (userData ,
508
- ReflectHelper .list2Array (fargs [0 ], val ));
509
- } catch (RuntimeException e ) {
510
- if (!ann .ignoreError ()) {
511
- throw e ;
512
- }
513
- } catch (Exception e ) {
514
- if (!ann .ignoreError ()) {
515
- throw new IllegalArgumentException (String .format (
516
- "Could not call method %s with %s" ,
517
- method .getName (), val ), e );
518
- }
519
- }
520
- }
521
- }
522
- }
456
+ @ Override
457
+ public void parseArgs (String [] args , Map <String , Object > attrs ,
458
+ Object userData ) throws ArgumentParserException {
459
+ parseArgs (args , 0 , attrs );
460
+
461
+ Class userClass = userData .getClass ();
462
+ while (userClass != null ) {
463
+ for (Field field : userClass .getDeclaredFields ()) {
464
+ Arg ann = field .getAnnotation (Arg .class );
465
+ if (ann != null ) {
466
+ String argDest = ann .dest ();
467
+ if (argDest .isEmpty ()) {
468
+ argDest = field .getName ();
469
+ }
470
+ if (!attrs .containsKey (argDest )) {
471
+ continue ;
472
+ }
473
+ Object val = attrs .get (argDest );
474
+ try {
475
+ field .setAccessible (true );
476
+ field .set (userData ,
477
+ ReflectHelper .list2Array (field .getType (), val ));
478
+ } catch (RuntimeException e ) {
479
+ if (!ann .ignoreError ()) {
480
+ throw e ;
481
+ }
482
+ } catch (Exception e ) {
483
+ if (!ann .ignoreError ()) {
484
+ throw new IllegalArgumentException (String .format (
485
+ "Could not set %s to field %s" , val ,
486
+ field .getName ()), e );
487
+ }
488
+ }
489
+ }
490
+ }
491
+ for (Method method : userClass .getDeclaredMethods ()) {
492
+ Arg ann = method .getAnnotation (Arg .class );
493
+ if (ann != null ) {
494
+ String argDest = ann .dest ();
495
+ if (argDest .isEmpty ()) {
496
+ argDest = method .getName ();
497
+ }
498
+ if (!attrs .containsKey (argDest )) {
499
+ continue ;
500
+ }
501
+ Object val = attrs .get (argDest );
502
+ Class <?> fargs [] = method .getParameterTypes ();
503
+ if (fargs .length != 1 ) {
504
+ throw new IllegalArgumentException (String .format (
505
+ "Method %s must have one formal parameter" ,
506
+ method .getName ()));
507
+ }
508
+ try {
509
+ method .setAccessible (true );
510
+ method .invoke (userData ,
511
+ ReflectHelper .list2Array (fargs [0 ], val ));
512
+ } catch (RuntimeException e ) {
513
+ if (!ann .ignoreError ()) {
514
+ throw e ;
515
+ }
516
+ } catch (Exception e ) {
517
+ if (!ann .ignoreError ()) {
518
+ throw new IllegalArgumentException (String .format (
519
+ "Could not call method %s with %s" ,
520
+ method .getName (), val ), e );
521
+ }
522
+ }
523
+ }
524
+ }
525
+ userClass = userClass .getSuperclass ();
526
+ }
527
+
528
+ }
523
529
524
530
public void parseArgs (String args [], int offset , Map <String , Object > attrs )
525
531
throws ArgumentParserException {
0 commit comments