Skip to content

Commit c0efeb4

Browse files
Keboojonsequitur
authored andcommitted
Localizing required option not provided message
There is a slight behavior change for options that have multiple aliases as it will select the longest alias. This is to make it more consistent with how the Name property on the option works.
1 parent 1d98a75 commit c0efeb4

20 files changed

+130
-3
lines changed

src/System.CommandLine.ApiCompatibility.Tests/ApiCompatibilityApprovalTests.System_CommandLine_api_is_not_changed.approved.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ System.CommandLine
188188
public System.String NoArgumentProvided(System.CommandLine.Parsing.SymbolResult symbolResult)
189189
public System.String RequiredArgumentMissing(System.CommandLine.Parsing.SymbolResult symbolResult)
190190
public System.String RequiredCommandWasNotProvided()
191+
public System.String RequiredOptionWasNotProvided(Option option)
191192
public System.String ResponseFileNotFound(System.String filePath)
192193
public System.String SuggestionsTokenNotMatched(System.String token)
193194
public System.String UnrecognizedArgument(System.String unrecognizedArg, System.Collections.Generic.IReadOnlyCollection<System.String> allowedValues)

src/System.CommandLine.Tests/GlobalOptionTests.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,25 @@ public void When_a_required_global_option_is_omitted_it_results_in_an_error()
4343
.ContainSingle()
4444
.Which.Message.Should().Be("Option '--i-must-be-set' is required.");
4545
}
46-
46+
47+
[Fact]
48+
public void When_a_required_global_option_has_multiple_aliases_the_error_message_uses_longest()
49+
{
50+
var rootCommand = new RootCommand();
51+
var requiredOption = new Option<bool>(new[] { "-i", "--i-must-be-set" })
52+
{
53+
IsRequired = true
54+
};
55+
rootCommand.AddGlobalOption(requiredOption);
56+
57+
var result = rootCommand.Parse("");
58+
59+
result.Errors
60+
.Should()
61+
.ContainSingle()
62+
.Which.Message.Should().Be("Option '--i-must-be-set' is required.");
63+
}
64+
4765
[Fact]
4866
public void When_a_required_global_option_is_present_on_child_of_command_it_was_added_to_it_does_not_result_in_an_error()
4967
{

src/System.CommandLine.Tests/ParsingValidationTests.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,30 @@ public void When_a_required_option_is_not_supplied_then_an_error_is_returned()
177177
.Be("Option '-x' is required.");
178178
}
179179

180+
[Fact]
181+
public void When_a_required_option_has_multiple_aliases_the_error_message_uses_longest()
182+
{
183+
var command = new Command("command")
184+
{
185+
new Option<string>(new[] {"-x", "--xray" })
186+
{
187+
IsRequired = true
188+
}
189+
};
190+
191+
var result = command.Parse("");
192+
193+
result.Errors
194+
.Should()
195+
.HaveCount(1)
196+
.And
197+
.Contain(e => e.SymbolResult.Symbol == command)
198+
.Which
199+
.Message
200+
.Should()
201+
.Be("Option '--xray' is required.");
202+
}
203+
180204
[Theory]
181205
[InlineData("subcommand -x arg")]
182206
[InlineData("-x arg subcommand")]

src/System.CommandLine/LocalizationResources.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,12 @@ symbolResult is CommandResult
9898
public virtual string RequiredCommandWasNotProvided() =>
9999
GetResourceString(Properties.Resources.RequiredCommandWasNotProvided);
100100

101+
/// <summary>
102+
/// Interpolates values into a localized string similar to Option '{0}' is required.
103+
/// </summary>
104+
public virtual string RequiredOptionWasNotProvided(Option option) =>
105+
GetResourceString(Properties.Resources.RequiredOptionWasNotProvided, option.Aliases.OrderByDescending(x => x.Length).First());
106+
101107
/// <summary>
102108
/// Interpolates values into a localized string similar to Argument &apos;{0}&apos; not recognized. Must be one of:{1}.
103109
/// </summary>

src/System.CommandLine/Parsing/ParseResultVisitor.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,8 +360,9 @@ private void ValidateCommandResult()
360360
{
361361
AddErrorToResult(
362362
_innermostCommandResult,
363-
new ParseError($"Option '{option.Aliases.First()}' is required.",
364-
_innermostCommandResult));
363+
new ParseError(
364+
_rootCommandResult.LocalizationResources.RequiredOptionWasNotProvided(option),
365+
_innermostCommandResult));
365366
}
366367
}
367368
}

src/System.CommandLine/Properties/Resources.Designer.cs

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/System.CommandLine/Properties/Resources.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,4 +231,7 @@
231231
<data name="ArgumentConversionCannotParseForOption" xml:space="preserve">
232232
<value>Cannot parse argument '{0}' for option '{1}' as expected type '{2}'.</value>
233233
</data>
234+
<data name="RequiredOptionWasNotProvided" xml:space="preserve">
235+
<value>Option '{0}' is required.</value>
236+
</data>
234237
</root>

src/System.CommandLine/Properties/xlf/Resources.cs.xlf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,11 @@
162162
<target state="new">Required command was not provided.</target>
163163
<note />
164164
</trans-unit>
165+
<trans-unit id="RequiredOptionWasNotProvided">
166+
<source>Option '{0}' is required.</source>
167+
<target state="new">Option '{0}' is required.</target>
168+
<note />
169+
</trans-unit>
165170
<trans-unit id="ResponseFileNotFound">
166171
<source>Response file not found '{0}'.</source>
167172
<target state="new">Response file not found '{0}'.</target>

src/System.CommandLine/Properties/xlf/Resources.de.xlf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,11 @@
162162
<target state="new">Required command was not provided.</target>
163163
<note />
164164
</trans-unit>
165+
<trans-unit id="RequiredOptionWasNotProvided">
166+
<source>Option '{0}' is required.</source>
167+
<target state="new">Option '{0}' is required.</target>
168+
<note />
169+
</trans-unit>
165170
<trans-unit id="ResponseFileNotFound">
166171
<source>Response file not found '{0}'.</source>
167172
<target state="new">Response file not found '{0}'.</target>

src/System.CommandLine/Properties/xlf/Resources.es.xlf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,11 @@
162162
<target state="new">Required command was not provided.</target>
163163
<note />
164164
</trans-unit>
165+
<trans-unit id="RequiredOptionWasNotProvided">
166+
<source>Option '{0}' is required.</source>
167+
<target state="new">Option '{0}' is required.</target>
168+
<note />
169+
</trans-unit>
165170
<trans-unit id="ResponseFileNotFound">
166171
<source>Response file not found '{0}'.</source>
167172
<target state="new">Response file not found '{0}'.</target>

src/System.CommandLine/Properties/xlf/Resources.fr.xlf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,11 @@
162162
<target state="new">Required command was not provided.</target>
163163
<note />
164164
</trans-unit>
165+
<trans-unit id="RequiredOptionWasNotProvided">
166+
<source>Option '{0}' is required.</source>
167+
<target state="new">Option '{0}' is required.</target>
168+
<note />
169+
</trans-unit>
165170
<trans-unit id="ResponseFileNotFound">
166171
<source>Response file not found '{0}'.</source>
167172
<target state="new">Response file not found '{0}'.</target>

src/System.CommandLine/Properties/xlf/Resources.it.xlf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,11 @@
162162
<target state="new">Required command was not provided.</target>
163163
<note />
164164
</trans-unit>
165+
<trans-unit id="RequiredOptionWasNotProvided">
166+
<source>Option '{0}' is required.</source>
167+
<target state="new">Option '{0}' is required.</target>
168+
<note />
169+
</trans-unit>
165170
<trans-unit id="ResponseFileNotFound">
166171
<source>Response file not found '{0}'.</source>
167172
<target state="new">Response file not found '{0}'.</target>

src/System.CommandLine/Properties/xlf/Resources.ja.xlf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,11 @@
162162
<target state="new">Required command was not provided.</target>
163163
<note />
164164
</trans-unit>
165+
<trans-unit id="RequiredOptionWasNotProvided">
166+
<source>Option '{0}' is required.</source>
167+
<target state="new">Option '{0}' is required.</target>
168+
<note />
169+
</trans-unit>
165170
<trans-unit id="ResponseFileNotFound">
166171
<source>Response file not found '{0}'.</source>
167172
<target state="new">Response file not found '{0}'.</target>

src/System.CommandLine/Properties/xlf/Resources.ko.xlf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,11 @@
162162
<target state="new">Required command was not provided.</target>
163163
<note />
164164
</trans-unit>
165+
<trans-unit id="RequiredOptionWasNotProvided">
166+
<source>Option '{0}' is required.</source>
167+
<target state="new">Option '{0}' is required.</target>
168+
<note />
169+
</trans-unit>
165170
<trans-unit id="ResponseFileNotFound">
166171
<source>Response file not found '{0}'.</source>
167172
<target state="new">Response file not found '{0}'.</target>

src/System.CommandLine/Properties/xlf/Resources.pl.xlf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,11 @@
162162
<target state="new">Required command was not provided.</target>
163163
<note />
164164
</trans-unit>
165+
<trans-unit id="RequiredOptionWasNotProvided">
166+
<source>Option '{0}' is required.</source>
167+
<target state="new">Option '{0}' is required.</target>
168+
<note />
169+
</trans-unit>
165170
<trans-unit id="ResponseFileNotFound">
166171
<source>Response file not found '{0}'.</source>
167172
<target state="new">Response file not found '{0}'.</target>

src/System.CommandLine/Properties/xlf/Resources.pt-BR.xlf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,11 @@
162162
<target state="new">Required command was not provided.</target>
163163
<note />
164164
</trans-unit>
165+
<trans-unit id="RequiredOptionWasNotProvided">
166+
<source>Option '{0}' is required.</source>
167+
<target state="new">Option '{0}' is required.</target>
168+
<note />
169+
</trans-unit>
165170
<trans-unit id="ResponseFileNotFound">
166171
<source>Response file not found '{0}'.</source>
167172
<target state="new">Response file not found '{0}'.</target>

src/System.CommandLine/Properties/xlf/Resources.ru.xlf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,11 @@
162162
<target state="new">Required command was not provided.</target>
163163
<note />
164164
</trans-unit>
165+
<trans-unit id="RequiredOptionWasNotProvided">
166+
<source>Option '{0}' is required.</source>
167+
<target state="new">Option '{0}' is required.</target>
168+
<note />
169+
</trans-unit>
165170
<trans-unit id="ResponseFileNotFound">
166171
<source>Response file not found '{0}'.</source>
167172
<target state="new">Response file not found '{0}'.</target>

src/System.CommandLine/Properties/xlf/Resources.tr.xlf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,11 @@
162162
<target state="new">Required command was not provided.</target>
163163
<note />
164164
</trans-unit>
165+
<trans-unit id="RequiredOptionWasNotProvided">
166+
<source>Option '{0}' is required.</source>
167+
<target state="new">Option '{0}' is required.</target>
168+
<note />
169+
</trans-unit>
165170
<trans-unit id="ResponseFileNotFound">
166171
<source>Response file not found '{0}'.</source>
167172
<target state="new">Response file not found '{0}'.</target>

src/System.CommandLine/Properties/xlf/Resources.zh-Hans.xlf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,11 @@
162162
<target state="new">Required command was not provided.</target>
163163
<note />
164164
</trans-unit>
165+
<trans-unit id="RequiredOptionWasNotProvided">
166+
<source>Option '{0}' is required.</source>
167+
<target state="new">Option '{0}' is required.</target>
168+
<note />
169+
</trans-unit>
165170
<trans-unit id="ResponseFileNotFound">
166171
<source>Response file not found '{0}'.</source>
167172
<target state="new">Response file not found '{0}'.</target>

src/System.CommandLine/Properties/xlf/Resources.zh-Hant.xlf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,11 @@
162162
<target state="new">Required command was not provided.</target>
163163
<note />
164164
</trans-unit>
165+
<trans-unit id="RequiredOptionWasNotProvided">
166+
<source>Option '{0}' is required.</source>
167+
<target state="new">Option '{0}' is required.</target>
168+
<note />
169+
</trans-unit>
165170
<trans-unit id="ResponseFileNotFound">
166171
<source>Response file not found '{0}'.</source>
167172
<target state="new">Response file not found '{0}'.</target>

0 commit comments

Comments
 (0)