@@ -459,6 +459,76 @@ public void ConfigureTests()
459
459
Assert . False ( option . HasDefault ) ;
460
460
}
461
461
462
+ [ Theory ]
463
+ [ InlineData ( new string [ ] { "" } , "defaulttransformed" , false ) ]
464
+ [ InlineData ( new string [ ] { "-m" , "test" } , "testtransformed" , false ) ]
465
+ [ InlineData ( new string [ ] { "--message" , "test" } , "testtransformed" , false ) ]
466
+ public void TransformationWorksAsExpected ( string [ ] args , string expected , bool errors )
467
+ {
468
+ var parser = new CommandLineParser < AddOption > ( ) ;
469
+
470
+ parser . Configure ( a => a . Message )
471
+ . Name ( "m" , "message" )
472
+ . Required ( )
473
+ . Transform ( msg => $ "{ msg } transformed")
474
+ . Default ( "default" ) ;
475
+
476
+ var result = parser . Parse ( args ) ;
477
+
478
+ Assert . Equal ( errors , result . AssertNoErrors ( false ) ) ;
479
+
480
+ Assert . Equal ( expected , result . Result . Message ) ;
481
+ }
482
+
483
+ [ Theory ]
484
+ [ InlineData ( new string [ ] { "" } , 11 , false ) ]
485
+ [ InlineData ( new string [ ] { "-i" , "10" } , 20 , false ) ]
486
+ [ InlineData ( new string [ ] { "--int" , "10" } , 20 , false ) ]
487
+ public void TransformationWorksAsExpectedForInts ( string [ ] args , int expected , bool errors )
488
+ {
489
+ var parser = new CommandLineParser < IntOptions > ( ) ;
490
+
491
+ parser . Configure ( a => a . SomeInt )
492
+ . Name ( "i" , "int" )
493
+ . Required ( )
494
+ . Transform ( value => value + 10 )
495
+ . Default ( 1 ) ;
496
+
497
+ var result = parser . Parse ( args ) ;
498
+
499
+ Assert . Equal ( errors , result . AssertNoErrors ( false ) ) ;
500
+
501
+ Assert . Equal ( expected , result . Result . SomeInt ) ;
502
+ }
503
+
504
+ [ Theory ]
505
+ [ InlineData ( new string [ ] { "cmd" } , 11 , false ) ]
506
+ [ InlineData ( new string [ ] { "cmd" , "-i" , "10" } , 20 , false ) ]
507
+ [ InlineData ( new string [ ] { "cmd" , "--int" , "10" } , 20 , false ) ]
508
+ public void TransformationWorksAsExpectedForCommandOptions ( string [ ] args , int expected , bool errors )
509
+ {
510
+ int outcome = - 1 ;
511
+
512
+ var parser = new CommandLineParser ( ) ;
513
+
514
+ var cmd = parser . AddCommand < IntOptions > ( )
515
+ . Name ( "cmd" )
516
+ . Required ( )
517
+ . OnExecuting ( ( _ , i ) => outcome = i . SomeInt ) ;
518
+
519
+ cmd . Configure ( a => a . SomeInt )
520
+ . Name ( "i" , "int" )
521
+ . Required ( )
522
+ . Transform ( value => value + 10 )
523
+ . Default ( 1 ) ;
524
+
525
+ var result = parser . Parse ( args ) ;
526
+
527
+ Assert . Equal ( errors , result . AssertNoErrors ( false ) ) ;
528
+
529
+ Assert . Equal ( expected , outcome ) ;
530
+ }
531
+
462
532
private class ObjOption
463
533
{
464
534
[ Name ( "p" ) , Required ]
@@ -493,5 +563,10 @@ private class OptionsWithThreeParams<T>
493
563
public T Option2 { get ; set ; }
494
564
public T Option3 { get ; set ; }
495
565
}
566
+
567
+ private class IntOptions
568
+ {
569
+ public int SomeInt { get ; set ; }
570
+ }
496
571
}
497
572
}
0 commit comments