Skip to content

Commit ac1ae96

Browse files
committed
fix #818
1 parent 687d7b4 commit ac1ae96

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

src/System.CommandLine.Tests/ValidationMessageLocalizationTests.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

44
using System.Collections.Generic;
5+
using System.CommandLine.Builder;
56
using System.CommandLine.Parsing;
67
using FluentAssertions;
78
using 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;

src/System.CommandLine/Builder/CommandLineBuilder.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff 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)

src/System.CommandLine/Builder/CommandLineBuilderExtensions.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff 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
{

0 commit comments

Comments
 (0)