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 2
2
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
3
4
4
using System . Collections . Generic ;
5
+ using System . CommandLine . Builder ;
5
6
using System . CommandLine . Parsing ;
6
7
using FluentAssertions ;
7
8
using System . Linq ;
@@ -32,6 +33,29 @@ public void Default_validation_messages_can_be_replaced_in_order_to_add_localiza
32
33
. Contain ( "the-message" ) ;
33
34
}
34
35
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
+
35
59
public class FakeValidationMessages : ValidationMessages
36
60
{
37
61
private readonly string message ;
Original file line number Diff line number Diff line change @@ -29,6 +29,8 @@ public CommandLineBuilder(Command rootCommand = null)
29
29
30
30
internal Option HelpOption { get ; set ; }
31
31
32
+ internal ValidationMessages ValidationMessages { get ; set ; }
33
+
32
34
public Parser Build ( )
33
35
{
34
36
var rootCommand = Command ;
@@ -38,7 +40,7 @@ public Parser Build()
38
40
new [ ] { rootCommand } ,
39
41
enablePosixBundling : EnablePosixBundling ,
40
42
enableDirectives : EnableDirectives ,
41
- validationMessages : ValidationMessages . Instance ,
43
+ validationMessages : ValidationMessages ,
42
44
responseFileHandling : ResponseFileHandling ,
43
45
middlewarePipeline : _middlewareList ? . OrderBy ( m => m . order )
44
46
. Select ( m => m . middleware )
Original file line number Diff line number Diff line change @@ -412,6 +412,14 @@ public static CommandLineBuilder UseTypoCorrections(
412
412
return builder ;
413
413
}
414
414
415
+ public static CommandLineBuilder UseValidationMessages (
416
+ this CommandLineBuilder builder ,
417
+ ValidationMessages validationMessages )
418
+ {
419
+ builder . ValidationMessages = validationMessages ;
420
+ return builder ;
421
+ }
422
+
415
423
public static CommandLineBuilder UseVersionOption (
416
424
this CommandLineBuilder builder )
417
425
{
You can’t perform that action at this time.
0 commit comments