File tree Expand file tree Collapse file tree 3 files changed +35
-1
lines changed
System.CommandLine/Builder Expand file tree Collapse file tree 3 files changed +35
-1
lines changed Original file line number Diff line number Diff line change 22// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33
44using System . Collections . Generic ;
5+ using System . CommandLine . Builder ;
56using System . CommandLine . Parsing ;
67using FluentAssertions ;
78using System . Linq ;
@@ -32,6 +33,29 @@ public void Default_validation_messages_can_be_replaced_in_order_to_add_localiza
3233 . Contain ( "the-message" ) ;
3334 }
3435
36+ [ Fact ]
37+ public void Default_validation_messages_can_be_replaced_using_CommandLineBuilder_in_order_to_add_localization_support ( )
38+ {
39+ var messages = new FakeValidationMessages ( "the-message" ) ;
40+
41+ var parser = new CommandLineBuilder ( new Command ( "the-command" )
42+ {
43+ new Argument
44+ {
45+ Arity = ArgumentArity . ExactlyOne
46+ }
47+ } )
48+ . UseValidationMessages ( messages )
49+ . Build ( ) ;
50+
51+ var result = parser . Parse ( "the-command" ) ;
52+
53+ result . Errors
54+ . Select ( e => e . Message )
55+ . Should ( )
56+ . Contain ( "the-message" ) ;
57+ }
58+
3559 public class FakeValidationMessages : ValidationMessages
3660 {
3761 private readonly string message ;
Original file line number Diff line number Diff line change @@ -29,6 +29,8 @@ public CommandLineBuilder(Command rootCommand = null)
2929
3030 internal Option HelpOption { get ; set ; }
3131
32+ internal ValidationMessages ValidationMessages { get ; set ; }
33+
3234 public Parser Build ( )
3335 {
3436 var rootCommand = Command ;
@@ -38,7 +40,7 @@ public Parser Build()
3840 new [ ] { rootCommand } ,
3941 enablePosixBundling : EnablePosixBundling ,
4042 enableDirectives : EnableDirectives ,
41- validationMessages : ValidationMessages . Instance ,
43+ validationMessages : ValidationMessages ,
4244 responseFileHandling : ResponseFileHandling ,
4345 middlewarePipeline : _middlewareList ? . OrderBy ( m => m . order )
4446 . Select ( m => m . middleware )
Original file line number Diff line number Diff line change @@ -412,6 +412,14 @@ public static CommandLineBuilder UseTypoCorrections(
412412 return builder ;
413413 }
414414
415+ public static CommandLineBuilder UseValidationMessages (
416+ this CommandLineBuilder builder ,
417+ ValidationMessages validationMessages )
418+ {
419+ builder . ValidationMessages = validationMessages ;
420+ return builder ;
421+ }
422+
415423 public static CommandLineBuilder UseVersionOption (
416424 this CommandLineBuilder builder )
417425 {
You can’t perform that action at this time.
0 commit comments