Skip to content

Commit

Permalink
Localizing required option not provided message
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Keboo authored and jonsequitur committed Sep 18, 2022
1 parent 1d98a75 commit c0efeb4
Show file tree
Hide file tree
Showing 20 changed files with 130 additions and 3 deletions.
Expand Up @@ -188,6 +188,7 @@ System.CommandLine
public System.String NoArgumentProvided(System.CommandLine.Parsing.SymbolResult symbolResult)
public System.String RequiredArgumentMissing(System.CommandLine.Parsing.SymbolResult symbolResult)
public System.String RequiredCommandWasNotProvided()
public System.String RequiredOptionWasNotProvided(Option option)
public System.String ResponseFileNotFound(System.String filePath)
public System.String SuggestionsTokenNotMatched(System.String token)
public System.String UnrecognizedArgument(System.String unrecognizedArg, System.Collections.Generic.IReadOnlyCollection<System.String> allowedValues)
Expand Down
20 changes: 19 additions & 1 deletion src/System.CommandLine.Tests/GlobalOptionTests.cs
Expand Up @@ -43,7 +43,25 @@ public void When_a_required_global_option_is_omitted_it_results_in_an_error()
.ContainSingle()
.Which.Message.Should().Be("Option '--i-must-be-set' is required.");
}


[Fact]
public void When_a_required_global_option_has_multiple_aliases_the_error_message_uses_longest()
{
var rootCommand = new RootCommand();
var requiredOption = new Option<bool>(new[] { "-i", "--i-must-be-set" })
{
IsRequired = true
};
rootCommand.AddGlobalOption(requiredOption);

var result = rootCommand.Parse("");

result.Errors
.Should()
.ContainSingle()
.Which.Message.Should().Be("Option '--i-must-be-set' is required.");
}

[Fact]
public void When_a_required_global_option_is_present_on_child_of_command_it_was_added_to_it_does_not_result_in_an_error()
{
Expand Down
24 changes: 24 additions & 0 deletions src/System.CommandLine.Tests/ParsingValidationTests.cs
Expand Up @@ -177,6 +177,30 @@ public void When_a_required_option_is_not_supplied_then_an_error_is_returned()
.Be("Option '-x' is required.");
}

[Fact]
public void When_a_required_option_has_multiple_aliases_the_error_message_uses_longest()
{
var command = new Command("command")
{
new Option<string>(new[] {"-x", "--xray" })
{
IsRequired = true
}
};

var result = command.Parse("");

result.Errors
.Should()
.HaveCount(1)
.And
.Contain(e => e.SymbolResult.Symbol == command)
.Which
.Message
.Should()
.Be("Option '--xray' is required.");
}

[Theory]
[InlineData("subcommand -x arg")]
[InlineData("-x arg subcommand")]
Expand Down
6 changes: 6 additions & 0 deletions src/System.CommandLine/LocalizationResources.cs
Expand Up @@ -98,6 +98,12 @@ protected LocalizationResources()
public virtual string RequiredCommandWasNotProvided() =>
GetResourceString(Properties.Resources.RequiredCommandWasNotProvided);

/// <summary>
/// Interpolates values into a localized string similar to Option '{0}' is required.
/// </summary>
public virtual string RequiredOptionWasNotProvided(Option option) =>
GetResourceString(Properties.Resources.RequiredOptionWasNotProvided, option.Aliases.OrderByDescending(x => x.Length).First());

/// <summary>
/// Interpolates values into a localized string similar to Argument &apos;{0}&apos; not recognized. Must be one of:{1}.
/// </summary>
Expand Down
5 changes: 3 additions & 2 deletions src/System.CommandLine/Parsing/ParseResultVisitor.cs
Expand Up @@ -360,8 +360,9 @@ private void ValidateCommandResult()
{
AddErrorToResult(
_innermostCommandResult,
new ParseError($"Option '{option.Aliases.First()}' is required.",
_innermostCommandResult));
new ParseError(
_rootCommandResult.LocalizationResources.RequiredOptionWasNotProvided(option),
_innermostCommandResult));
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions src/System.CommandLine/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/System.CommandLine/Properties/Resources.resx
Expand Up @@ -231,4 +231,7 @@
<data name="ArgumentConversionCannotParseForOption" xml:space="preserve">
<value>Cannot parse argument '{0}' for option '{1}' as expected type '{2}'.</value>
</data>
<data name="RequiredOptionWasNotProvided" xml:space="preserve">
<value>Option '{0}' is required.</value>
</data>
</root>
5 changes: 5 additions & 0 deletions src/System.CommandLine/Properties/xlf/Resources.cs.xlf
Expand Up @@ -162,6 +162,11 @@
<target state="new">Required command was not provided.</target>
<note />
</trans-unit>
<trans-unit id="RequiredOptionWasNotProvided">
<source>Option '{0}' is required.</source>
<target state="new">Option '{0}' is required.</target>
<note />
</trans-unit>
<trans-unit id="ResponseFileNotFound">
<source>Response file not found '{0}'.</source>
<target state="new">Response file not found '{0}'.</target>
Expand Down
5 changes: 5 additions & 0 deletions src/System.CommandLine/Properties/xlf/Resources.de.xlf
Expand Up @@ -162,6 +162,11 @@
<target state="new">Required command was not provided.</target>
<note />
</trans-unit>
<trans-unit id="RequiredOptionWasNotProvided">
<source>Option '{0}' is required.</source>
<target state="new">Option '{0}' is required.</target>
<note />
</trans-unit>
<trans-unit id="ResponseFileNotFound">
<source>Response file not found '{0}'.</source>
<target state="new">Response file not found '{0}'.</target>
Expand Down
5 changes: 5 additions & 0 deletions src/System.CommandLine/Properties/xlf/Resources.es.xlf
Expand Up @@ -162,6 +162,11 @@
<target state="new">Required command was not provided.</target>
<note />
</trans-unit>
<trans-unit id="RequiredOptionWasNotProvided">
<source>Option '{0}' is required.</source>
<target state="new">Option '{0}' is required.</target>
<note />
</trans-unit>
<trans-unit id="ResponseFileNotFound">
<source>Response file not found '{0}'.</source>
<target state="new">Response file not found '{0}'.</target>
Expand Down
5 changes: 5 additions & 0 deletions src/System.CommandLine/Properties/xlf/Resources.fr.xlf
Expand Up @@ -162,6 +162,11 @@
<target state="new">Required command was not provided.</target>
<note />
</trans-unit>
<trans-unit id="RequiredOptionWasNotProvided">
<source>Option '{0}' is required.</source>
<target state="new">Option '{0}' is required.</target>
<note />
</trans-unit>
<trans-unit id="ResponseFileNotFound">
<source>Response file not found '{0}'.</source>
<target state="new">Response file not found '{0}'.</target>
Expand Down
5 changes: 5 additions & 0 deletions src/System.CommandLine/Properties/xlf/Resources.it.xlf
Expand Up @@ -162,6 +162,11 @@
<target state="new">Required command was not provided.</target>
<note />
</trans-unit>
<trans-unit id="RequiredOptionWasNotProvided">
<source>Option '{0}' is required.</source>
<target state="new">Option '{0}' is required.</target>
<note />
</trans-unit>
<trans-unit id="ResponseFileNotFound">
<source>Response file not found '{0}'.</source>
<target state="new">Response file not found '{0}'.</target>
Expand Down
5 changes: 5 additions & 0 deletions src/System.CommandLine/Properties/xlf/Resources.ja.xlf
Expand Up @@ -162,6 +162,11 @@
<target state="new">Required command was not provided.</target>
<note />
</trans-unit>
<trans-unit id="RequiredOptionWasNotProvided">
<source>Option '{0}' is required.</source>
<target state="new">Option '{0}' is required.</target>
<note />
</trans-unit>
<trans-unit id="ResponseFileNotFound">
<source>Response file not found '{0}'.</source>
<target state="new">Response file not found '{0}'.</target>
Expand Down
5 changes: 5 additions & 0 deletions src/System.CommandLine/Properties/xlf/Resources.ko.xlf
Expand Up @@ -162,6 +162,11 @@
<target state="new">Required command was not provided.</target>
<note />
</trans-unit>
<trans-unit id="RequiredOptionWasNotProvided">
<source>Option '{0}' is required.</source>
<target state="new">Option '{0}' is required.</target>
<note />
</trans-unit>
<trans-unit id="ResponseFileNotFound">
<source>Response file not found '{0}'.</source>
<target state="new">Response file not found '{0}'.</target>
Expand Down
5 changes: 5 additions & 0 deletions src/System.CommandLine/Properties/xlf/Resources.pl.xlf
Expand Up @@ -162,6 +162,11 @@
<target state="new">Required command was not provided.</target>
<note />
</trans-unit>
<trans-unit id="RequiredOptionWasNotProvided">
<source>Option '{0}' is required.</source>
<target state="new">Option '{0}' is required.</target>
<note />
</trans-unit>
<trans-unit id="ResponseFileNotFound">
<source>Response file not found '{0}'.</source>
<target state="new">Response file not found '{0}'.</target>
Expand Down
5 changes: 5 additions & 0 deletions src/System.CommandLine/Properties/xlf/Resources.pt-BR.xlf
Expand Up @@ -162,6 +162,11 @@
<target state="new">Required command was not provided.</target>
<note />
</trans-unit>
<trans-unit id="RequiredOptionWasNotProvided">
<source>Option '{0}' is required.</source>
<target state="new">Option '{0}' is required.</target>
<note />
</trans-unit>
<trans-unit id="ResponseFileNotFound">
<source>Response file not found '{0}'.</source>
<target state="new">Response file not found '{0}'.</target>
Expand Down
5 changes: 5 additions & 0 deletions src/System.CommandLine/Properties/xlf/Resources.ru.xlf
Expand Up @@ -162,6 +162,11 @@
<target state="new">Required command was not provided.</target>
<note />
</trans-unit>
<trans-unit id="RequiredOptionWasNotProvided">
<source>Option '{0}' is required.</source>
<target state="new">Option '{0}' is required.</target>
<note />
</trans-unit>
<trans-unit id="ResponseFileNotFound">
<source>Response file not found '{0}'.</source>
<target state="new">Response file not found '{0}'.</target>
Expand Down
5 changes: 5 additions & 0 deletions src/System.CommandLine/Properties/xlf/Resources.tr.xlf
Expand Up @@ -162,6 +162,11 @@
<target state="new">Required command was not provided.</target>
<note />
</trans-unit>
<trans-unit id="RequiredOptionWasNotProvided">
<source>Option '{0}' is required.</source>
<target state="new">Option '{0}' is required.</target>
<note />
</trans-unit>
<trans-unit id="ResponseFileNotFound">
<source>Response file not found '{0}'.</source>
<target state="new">Response file not found '{0}'.</target>
Expand Down
5 changes: 5 additions & 0 deletions src/System.CommandLine/Properties/xlf/Resources.zh-Hans.xlf
Expand Up @@ -162,6 +162,11 @@
<target state="new">Required command was not provided.</target>
<note />
</trans-unit>
<trans-unit id="RequiredOptionWasNotProvided">
<source>Option '{0}' is required.</source>
<target state="new">Option '{0}' is required.</target>
<note />
</trans-unit>
<trans-unit id="ResponseFileNotFound">
<source>Response file not found '{0}'.</source>
<target state="new">Response file not found '{0}'.</target>
Expand Down
5 changes: 5 additions & 0 deletions src/System.CommandLine/Properties/xlf/Resources.zh-Hant.xlf
Expand Up @@ -162,6 +162,11 @@
<target state="new">Required command was not provided.</target>
<note />
</trans-unit>
<trans-unit id="RequiredOptionWasNotProvided">
<source>Option '{0}' is required.</source>
<target state="new">Option '{0}' is required.</target>
<note />
</trans-unit>
<trans-unit id="ResponseFileNotFound">
<source>Response file not found '{0}'.</source>
<target state="new">Response file not found '{0}'.</target>
Expand Down

0 comments on commit c0efeb4

Please sign in to comment.