From 0430490ea9099a385eaba7bf3d136eb583dac5f8 Mon Sep 17 00:00:00 2001 From: Kevin Bost Date: Sun, 18 Sep 2022 00:41:28 -0700 Subject: [PATCH] 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. --- ...ommandLine_api_is_not_changed.approved.txt | 1 + .../GlobalOptionTests.cs | 20 +++++++++++++++- .../ParsingValidationTests.cs | 24 +++++++++++++++++++ .../LocalizationResources.cs | 6 +++++ .../Parsing/ParseResultVisitor.cs | 5 ++-- .../Properties/Resources.Designer.cs | 9 +++++++ .../Properties/Resources.resx | 3 +++ .../Properties/xlf/Resources.cs.xlf | 5 ++++ .../Properties/xlf/Resources.de.xlf | 5 ++++ .../Properties/xlf/Resources.es.xlf | 5 ++++ .../Properties/xlf/Resources.fr.xlf | 5 ++++ .../Properties/xlf/Resources.it.xlf | 5 ++++ .../Properties/xlf/Resources.ja.xlf | 5 ++++ .../Properties/xlf/Resources.ko.xlf | 5 ++++ .../Properties/xlf/Resources.pl.xlf | 5 ++++ .../Properties/xlf/Resources.pt-BR.xlf | 5 ++++ .../Properties/xlf/Resources.ru.xlf | 5 ++++ .../Properties/xlf/Resources.tr.xlf | 5 ++++ .../Properties/xlf/Resources.zh-Hans.xlf | 5 ++++ .../Properties/xlf/Resources.zh-Hant.xlf | 5 ++++ 20 files changed, 130 insertions(+), 3 deletions(-) diff --git a/src/System.CommandLine.ApiCompatibility.Tests/ApiCompatibilityApprovalTests.System_CommandLine_api_is_not_changed.approved.txt b/src/System.CommandLine.ApiCompatibility.Tests/ApiCompatibilityApprovalTests.System_CommandLine_api_is_not_changed.approved.txt index 4317a19b64..bd25b25078 100644 --- a/src/System.CommandLine.ApiCompatibility.Tests/ApiCompatibilityApprovalTests.System_CommandLine_api_is_not_changed.approved.txt +++ b/src/System.CommandLine.ApiCompatibility.Tests/ApiCompatibilityApprovalTests.System_CommandLine_api_is_not_changed.approved.txt @@ -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 allowedValues) diff --git a/src/System.CommandLine.Tests/GlobalOptionTests.cs b/src/System.CommandLine.Tests/GlobalOptionTests.cs index f6884378fb..8a44bf30c7 100644 --- a/src/System.CommandLine.Tests/GlobalOptionTests.cs +++ b/src/System.CommandLine.Tests/GlobalOptionTests.cs @@ -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(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() { diff --git a/src/System.CommandLine.Tests/ParsingValidationTests.cs b/src/System.CommandLine.Tests/ParsingValidationTests.cs index d4645a62a4..e252bb5b3b 100644 --- a/src/System.CommandLine.Tests/ParsingValidationTests.cs +++ b/src/System.CommandLine.Tests/ParsingValidationTests.cs @@ -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(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")] diff --git a/src/System.CommandLine/LocalizationResources.cs b/src/System.CommandLine/LocalizationResources.cs index ee565ef513..0107ce06fe 100644 --- a/src/System.CommandLine/LocalizationResources.cs +++ b/src/System.CommandLine/LocalizationResources.cs @@ -98,6 +98,12 @@ symbolResult is CommandResult public virtual string RequiredCommandWasNotProvided() => GetResourceString(Properties.Resources.RequiredCommandWasNotProvided); + /// + /// Interpolates values into a localized string similar to Option '{0}' is required. + /// + public virtual string RequiredOptionWasNotProvided(Option option) => + GetResourceString(Properties.Resources.RequiredOptionWasNotProvided, option.Aliases.OrderByDescending(x => x.Length).First()); + /// /// Interpolates values into a localized string similar to Argument '{0}' not recognized. Must be one of:{1}. /// diff --git a/src/System.CommandLine/Parsing/ParseResultVisitor.cs b/src/System.CommandLine/Parsing/ParseResultVisitor.cs index ce4502f740..2ed41340ed 100644 --- a/src/System.CommandLine/Parsing/ParseResultVisitor.cs +++ b/src/System.CommandLine/Parsing/ParseResultVisitor.cs @@ -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)); } } } diff --git a/src/System.CommandLine/Properties/Resources.Designer.cs b/src/System.CommandLine/Properties/Resources.Designer.cs index 7d3ef75bf9..ddb11c322f 100644 --- a/src/System.CommandLine/Properties/Resources.Designer.cs +++ b/src/System.CommandLine/Properties/Resources.Designer.cs @@ -348,6 +348,15 @@ internal static string RequiredCommandWasNotProvided { } } + /// + /// Looks up a localized string similar to Option '{0}' is required.. + /// + internal static string RequiredOptionWasNotProvided { + get { + return ResourceManager.GetString("RequiredOptionWasNotProvided", resourceCulture); + } + } + /// /// Looks up a localized string similar to Response file not found '{0}'.. /// diff --git a/src/System.CommandLine/Properties/Resources.resx b/src/System.CommandLine/Properties/Resources.resx index f084edc5b0..f0b101ebba 100644 --- a/src/System.CommandLine/Properties/Resources.resx +++ b/src/System.CommandLine/Properties/Resources.resx @@ -231,4 +231,7 @@ Cannot parse argument '{0}' for option '{1}' as expected type '{2}'. + + Option '{0}' is required. + \ No newline at end of file diff --git a/src/System.CommandLine/Properties/xlf/Resources.cs.xlf b/src/System.CommandLine/Properties/xlf/Resources.cs.xlf index 5600551bac..86c81955a1 100644 --- a/src/System.CommandLine/Properties/xlf/Resources.cs.xlf +++ b/src/System.CommandLine/Properties/xlf/Resources.cs.xlf @@ -162,6 +162,11 @@ Required command was not provided. + + Option '{0}' is required. + Option '{0}' is required. + + Response file not found '{0}'. Response file not found '{0}'. diff --git a/src/System.CommandLine/Properties/xlf/Resources.de.xlf b/src/System.CommandLine/Properties/xlf/Resources.de.xlf index 6095ba13c5..e47d4bc1ee 100644 --- a/src/System.CommandLine/Properties/xlf/Resources.de.xlf +++ b/src/System.CommandLine/Properties/xlf/Resources.de.xlf @@ -162,6 +162,11 @@ Required command was not provided. + + Option '{0}' is required. + Option '{0}' is required. + + Response file not found '{0}'. Response file not found '{0}'. diff --git a/src/System.CommandLine/Properties/xlf/Resources.es.xlf b/src/System.CommandLine/Properties/xlf/Resources.es.xlf index 9c8605bcfe..2e85b00d2f 100644 --- a/src/System.CommandLine/Properties/xlf/Resources.es.xlf +++ b/src/System.CommandLine/Properties/xlf/Resources.es.xlf @@ -162,6 +162,11 @@ Required command was not provided. + + Option '{0}' is required. + Option '{0}' is required. + + Response file not found '{0}'. Response file not found '{0}'. diff --git a/src/System.CommandLine/Properties/xlf/Resources.fr.xlf b/src/System.CommandLine/Properties/xlf/Resources.fr.xlf index 26f41492c4..e8afe2a034 100644 --- a/src/System.CommandLine/Properties/xlf/Resources.fr.xlf +++ b/src/System.CommandLine/Properties/xlf/Resources.fr.xlf @@ -162,6 +162,11 @@ Required command was not provided. + + Option '{0}' is required. + Option '{0}' is required. + + Response file not found '{0}'. Response file not found '{0}'. diff --git a/src/System.CommandLine/Properties/xlf/Resources.it.xlf b/src/System.CommandLine/Properties/xlf/Resources.it.xlf index b69c678224..040135ce41 100644 --- a/src/System.CommandLine/Properties/xlf/Resources.it.xlf +++ b/src/System.CommandLine/Properties/xlf/Resources.it.xlf @@ -162,6 +162,11 @@ Required command was not provided. + + Option '{0}' is required. + Option '{0}' is required. + + Response file not found '{0}'. Response file not found '{0}'. diff --git a/src/System.CommandLine/Properties/xlf/Resources.ja.xlf b/src/System.CommandLine/Properties/xlf/Resources.ja.xlf index 7396e36bcd..aa9c63dce5 100644 --- a/src/System.CommandLine/Properties/xlf/Resources.ja.xlf +++ b/src/System.CommandLine/Properties/xlf/Resources.ja.xlf @@ -162,6 +162,11 @@ Required command was not provided. + + Option '{0}' is required. + Option '{0}' is required. + + Response file not found '{0}'. Response file not found '{0}'. diff --git a/src/System.CommandLine/Properties/xlf/Resources.ko.xlf b/src/System.CommandLine/Properties/xlf/Resources.ko.xlf index 3fc016a55a..f5a4950dca 100644 --- a/src/System.CommandLine/Properties/xlf/Resources.ko.xlf +++ b/src/System.CommandLine/Properties/xlf/Resources.ko.xlf @@ -162,6 +162,11 @@ Required command was not provided. + + Option '{0}' is required. + Option '{0}' is required. + + Response file not found '{0}'. Response file not found '{0}'. diff --git a/src/System.CommandLine/Properties/xlf/Resources.pl.xlf b/src/System.CommandLine/Properties/xlf/Resources.pl.xlf index 3821ee2f6f..b5cf7fd3f0 100644 --- a/src/System.CommandLine/Properties/xlf/Resources.pl.xlf +++ b/src/System.CommandLine/Properties/xlf/Resources.pl.xlf @@ -162,6 +162,11 @@ Required command was not provided. + + Option '{0}' is required. + Option '{0}' is required. + + Response file not found '{0}'. Response file not found '{0}'. diff --git a/src/System.CommandLine/Properties/xlf/Resources.pt-BR.xlf b/src/System.CommandLine/Properties/xlf/Resources.pt-BR.xlf index 74cc26d0e0..8f7e7af0d2 100644 --- a/src/System.CommandLine/Properties/xlf/Resources.pt-BR.xlf +++ b/src/System.CommandLine/Properties/xlf/Resources.pt-BR.xlf @@ -162,6 +162,11 @@ Required command was not provided. + + Option '{0}' is required. + Option '{0}' is required. + + Response file not found '{0}'. Response file not found '{0}'. diff --git a/src/System.CommandLine/Properties/xlf/Resources.ru.xlf b/src/System.CommandLine/Properties/xlf/Resources.ru.xlf index f9f6a823e5..6a8b8128f5 100644 --- a/src/System.CommandLine/Properties/xlf/Resources.ru.xlf +++ b/src/System.CommandLine/Properties/xlf/Resources.ru.xlf @@ -162,6 +162,11 @@ Required command was not provided. + + Option '{0}' is required. + Option '{0}' is required. + + Response file not found '{0}'. Response file not found '{0}'. diff --git a/src/System.CommandLine/Properties/xlf/Resources.tr.xlf b/src/System.CommandLine/Properties/xlf/Resources.tr.xlf index 661bb28dfb..c43d076b4d 100644 --- a/src/System.CommandLine/Properties/xlf/Resources.tr.xlf +++ b/src/System.CommandLine/Properties/xlf/Resources.tr.xlf @@ -162,6 +162,11 @@ Required command was not provided. + + Option '{0}' is required. + Option '{0}' is required. + + Response file not found '{0}'. Response file not found '{0}'. diff --git a/src/System.CommandLine/Properties/xlf/Resources.zh-Hans.xlf b/src/System.CommandLine/Properties/xlf/Resources.zh-Hans.xlf index e766fb4bb9..ca955abebc 100644 --- a/src/System.CommandLine/Properties/xlf/Resources.zh-Hans.xlf +++ b/src/System.CommandLine/Properties/xlf/Resources.zh-Hans.xlf @@ -162,6 +162,11 @@ Required command was not provided. + + Option '{0}' is required. + Option '{0}' is required. + + Response file not found '{0}'. Response file not found '{0}'. diff --git a/src/System.CommandLine/Properties/xlf/Resources.zh-Hant.xlf b/src/System.CommandLine/Properties/xlf/Resources.zh-Hant.xlf index c5f24eac38..3ceabfdb3d 100644 --- a/src/System.CommandLine/Properties/xlf/Resources.zh-Hant.xlf +++ b/src/System.CommandLine/Properties/xlf/Resources.zh-Hant.xlf @@ -162,6 +162,11 @@ Required command was not provided. + + Option '{0}' is required. + Option '{0}' is required. + + Response file not found '{0}'. Response file not found '{0}'.