From 0e7dd3c8f2da4a0cb40d41ec21b6330d97d75faa Mon Sep 17 00:00:00 2001 From: Carsten Igel Date: Wed, 16 Jun 2021 20:21:40 +0200 Subject: [PATCH 1/3] Added NameExtension for ValueSpecification --- src/CommandLine/Core/NameExtensions.cs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/CommandLine/Core/NameExtensions.cs b/src/CommandLine/Core/NameExtensions.cs index 405e44f5..7cb806e7 100644 --- a/src/CommandLine/Core/NameExtensions.cs +++ b/src/CommandLine/Core/NameExtensions.cs @@ -20,12 +20,21 @@ public static NameInfo FromOptionSpecification(this OptionSpecification specific specification.LongName); } + public static NameInfo FromValueSpecification(this ValueSpecification specification) + { + return new NameInfo( + string.Empty, + specification.MetaName); + } + public static NameInfo FromSpecification(this Specification specification) { switch (specification.Tag) { case SpecificationType.Option: return FromOptionSpecification((OptionSpecification)specification); + case SpecificationType.Value: + return FromValueSpecification((ValueSpecification)specification); default: return NameInfo.EmptyName; } From 929152138c6fef9ee426fe3d52f6c218dd866f18 Mon Sep 17 00:00:00 2001 From: Carsten Igel Date: Wed, 16 Jun 2021 20:39:05 +0200 Subject: [PATCH 2/3] Required values will produce their own error A required value, i.e. a positional argument, can be missing. The error message should reflect this issue. --- .../LocalizableSentenceBuilder.cs | 248 +++--- .../Properties/Resources.Designer.cs | 756 +++++++++--------- .../Properties/Resources.resx | 453 +++++------ .../Core/SpecificationPropertyRules.cs | 4 +- src/CommandLine/Error.cs | 22 + src/CommandLine/Text/SentenceBuilder.cs | 5 + 6 files changed, 762 insertions(+), 726 deletions(-) diff --git a/demo/ReadText.LocalizedDemo/LocalizableSentenceBuilder.cs b/demo/ReadText.LocalizedDemo/LocalizableSentenceBuilder.cs index bf2b7c56..1e5aad18 100644 --- a/demo/ReadText.LocalizedDemo/LocalizableSentenceBuilder.cs +++ b/demo/ReadText.LocalizedDemo/LocalizableSentenceBuilder.cs @@ -1,122 +1,126 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using CommandLine; -using CommandLine.Text; - -namespace ReadText.LocalizedDemo -{ - public class LocalizableSentenceBuilder : SentenceBuilder - { - public override Func RequiredWord - { - get { return () => Properties.Resources.SentenceRequiredWord; } - } - - public override Func ErrorsHeadingText - { - // Cannot be pluralized - get { return () => Properties.Resources.SentenceErrorsHeadingText; } - } - - public override Func UsageHeadingText - { - get { return () => Properties.Resources.SentenceUsageHeadingText; } - } - - public override Func HelpCommandText - { - get - { - return isOption => isOption - ? Properties.Resources.SentenceHelpCommandTextOption - : Properties.Resources.SentenceHelpCommandTextVerb; - } - } - - public override Func VersionCommandText - { - get { return _ => Properties.Resources.SentenceVersionCommandText; } - } - - public override Func FormatError - { - get - { - return error => - { - switch (error.Tag) - { - case ErrorType.BadFormatTokenError: - return String.Format(Properties.Resources.SentenceBadFormatTokenError, ((BadFormatTokenError)error).Token); - case ErrorType.MissingValueOptionError: - return String.Format(Properties.Resources.SentenceMissingValueOptionError, ((MissingValueOptionError)error).NameInfo.NameText); - case ErrorType.UnknownOptionError: - return String.Format(Properties.Resources.SentenceUnknownOptionError, ((UnknownOptionError)error).Token); - case ErrorType.MissingRequiredOptionError: - var errMisssing = ((MissingRequiredOptionError)error); - return errMisssing.NameInfo.Equals(NameInfo.EmptyName) - ? Properties.Resources.SentenceMissingRequiredOptionError - : String.Format(Properties.Resources.SentenceMissingRequiredOptionError, errMisssing.NameInfo.NameText); - case ErrorType.BadFormatConversionError: - var badFormat = ((BadFormatConversionError)error); - return badFormat.NameInfo.Equals(NameInfo.EmptyName) - ? Properties.Resources.SentenceBadFormatConversionErrorValue - : String.Format(Properties.Resources.SentenceBadFormatConversionErrorOption, badFormat.NameInfo.NameText); - case ErrorType.SequenceOutOfRangeError: - var seqOutRange = ((SequenceOutOfRangeError)error); - return seqOutRange.NameInfo.Equals(NameInfo.EmptyName) - ? Properties.Resources.SentenceSequenceOutOfRangeErrorValue - : String.Format(Properties.Resources.SentenceSequenceOutOfRangeErrorOption, - seqOutRange.NameInfo.NameText); - case ErrorType.BadVerbSelectedError: - return String.Format(Properties.Resources.SentenceBadVerbSelectedError, ((BadVerbSelectedError)error).Token); - case ErrorType.NoVerbSelectedError: - return Properties.Resources.SentenceNoVerbSelectedError; - case ErrorType.RepeatedOptionError: - return String.Format(Properties.Resources.SentenceRepeatedOptionError, ((RepeatedOptionError)error).NameInfo.NameText); - case ErrorType.SetValueExceptionError: - var setValueError = (SetValueExceptionError)error; - return String.Format(Properties.Resources.SentenceSetValueExceptionError, setValueError.NameInfo.NameText, setValueError.Exception.Message); - } - throw new InvalidOperationException(); - }; - } - } - - public override Func, string> FormatMutuallyExclusiveSetErrors - { - get - { - return errors => - { - var bySet = from e in errors - group e by e.SetName into g - select new { SetName = g.Key, Errors = g.ToList() }; - - var msgs = bySet.Select( - set => - { - var names = String.Join( - String.Empty, - (from e in set.Errors select String.Format("'{0}', ", e.NameInfo.NameText)).ToArray()); - var namesCount = set.Errors.Count(); - - var incompat = String.Join( - String.Empty, - (from x in - (from s in bySet where !s.SetName.Equals(set.SetName) from e in s.Errors select e) - .Distinct() - select String.Format("'{0}', ", x.NameInfo.NameText)).ToArray()); - //TODO: Pluralize by namesCount - return - String.Format(Properties.Resources.SentenceMutuallyExclusiveSetErrors, - names.Substring(0, names.Length - 2), incompat.Substring(0, incompat.Length - 2)); - }).ToArray(); - return string.Join(Environment.NewLine, msgs); - }; - } - } - } -} +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using CommandLine; +using CommandLine.Text; + +namespace ReadText.LocalizedDemo +{ + public class LocalizableSentenceBuilder : SentenceBuilder + { + public override Func RequiredWord + { + get { return () => Properties.Resources.SentenceRequiredWord; } + } + + public override Func ErrorsHeadingText + { + // Cannot be pluralized + get { return () => Properties.Resources.SentenceErrorsHeadingText; } + } + + public override Func UsageHeadingText + { + get { return () => Properties.Resources.SentenceUsageHeadingText; } + } + + public override Func HelpCommandText + { + get + { + return isOption => isOption + ? Properties.Resources.SentenceHelpCommandTextOption + : Properties.Resources.SentenceHelpCommandTextVerb; + } + } + + public override Func VersionCommandText + { + get { return _ => Properties.Resources.SentenceVersionCommandText; } + } + + public override Func FormatError + { + get + { + return error => + { + switch (error.Tag) + { + case ErrorType.BadFormatTokenError: + return String.Format(Properties.Resources.SentenceBadFormatTokenError, ((BadFormatTokenError)error).Token); + case ErrorType.MissingValueOptionError: + return String.Format(Properties.Resources.SentenceMissingValueOptionError, ((MissingValueOptionError)error).NameInfo.NameText); + case ErrorType.UnknownOptionError: + return String.Format(Properties.Resources.SentenceUnknownOptionError, ((UnknownOptionError)error).Token); + case ErrorType.MissingRequiredOptionError: + var errMisssing = ((MissingRequiredOptionError)error); + return errMisssing.NameInfo.Equals(NameInfo.EmptyName) + ? Properties.Resources.SentenceMissingRequiredOptionError + : String.Format(Properties.Resources.SentenceMissingRequiredOptionError, errMisssing.NameInfo.NameText); + case ErrorType.MissingRequiredValueError: + var errMissing = ((MissingRequiredValueError)error); + return String.Format(Properties.Resources.SentenceMissingRequiredValueError, + errMisssing.NameInfo.Equals(NameInfo.EmptyName) ? errMissing.NameInfo.NameText : Convert.ToString(error.ValuePosition)); + case ErrorType.BadFormatConversionError: + var badFormat = ((BadFormatConversionError)error); + return badFormat.NameInfo.Equals(NameInfo.EmptyName) + ? Properties.Resources.SentenceBadFormatConversionErrorValue + : String.Format(Properties.Resources.SentenceBadFormatConversionErrorOption, badFormat.NameInfo.NameText); + case ErrorType.SequenceOutOfRangeError: + var seqOutRange = ((SequenceOutOfRangeError)error); + return seqOutRange.NameInfo.Equals(NameInfo.EmptyName) + ? Properties.Resources.SentenceSequenceOutOfRangeErrorValue + : String.Format(Properties.Resources.SentenceSequenceOutOfRangeErrorOption, + seqOutRange.NameInfo.NameText); + case ErrorType.BadVerbSelectedError: + return String.Format(Properties.Resources.SentenceBadVerbSelectedError, ((BadVerbSelectedError)error).Token); + case ErrorType.NoVerbSelectedError: + return Properties.Resources.SentenceNoVerbSelectedError; + case ErrorType.RepeatedOptionError: + return String.Format(Properties.Resources.SentenceRepeatedOptionError, ((RepeatedOptionError)error).NameInfo.NameText); + case ErrorType.SetValueExceptionError: + var setValueError = (SetValueExceptionError)error; + return String.Format(Properties.Resources.SentenceSetValueExceptionError, setValueError.NameInfo.NameText, setValueError.Exception.Message); + } + throw new InvalidOperationException(); + }; + } + } + + public override Func, string> FormatMutuallyExclusiveSetErrors + { + get + { + return errors => + { + var bySet = from e in errors + group e by e.SetName into g + select new { SetName = g.Key, Errors = g.ToList() }; + + var msgs = bySet.Select( + set => + { + var names = String.Join( + String.Empty, + (from e in set.Errors select String.Format("'{0}', ", e.NameInfo.NameText)).ToArray()); + var namesCount = set.Errors.Count(); + + var incompat = String.Join( + String.Empty, + (from x in + (from s in bySet where !s.SetName.Equals(set.SetName) from e in s.Errors select e) + .Distinct() + select String.Format("'{0}', ", x.NameInfo.NameText)).ToArray()); + //TODO: Pluralize by namesCount + return + String.Format(Properties.Resources.SentenceMutuallyExclusiveSetErrors, + names.Substring(0, names.Length - 2), incompat.Substring(0, incompat.Length - 2)); + }).ToArray(); + return string.Join(Environment.NewLine, msgs); + }; + } + } + } +} diff --git a/demo/ReadText.LocalizedDemo/Properties/Resources.Designer.cs b/demo/ReadText.LocalizedDemo/Properties/Resources.Designer.cs index cc414359..5c8b92bf 100644 --- a/demo/ReadText.LocalizedDemo/Properties/Resources.Designer.cs +++ b/demo/ReadText.LocalizedDemo/Properties/Resources.Designer.cs @@ -1,378 +1,378 @@ -//------------------------------------------------------------------------------ -// -// This code was generated by a tool. -// Runtime Version:4.0.30319.42000 -// -// Changes to this file may cause incorrect behavior and will be lost if -// the code is regenerated. -// -//------------------------------------------------------------------------------ - -namespace ReadText.LocalizedDemo.Properties { - using System; - - - /// - /// A strongly-typed resource class, for looking up localized strings, etc. - /// - // This class was auto-generated by the StronglyTypedResourceBuilder - // class via a tool like ResGen or Visual Studio. - // To add or remove a member, edit your .ResX file then rerun ResGen - // with the /str option, or rebuild your VS project. - [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "15.0.0.0")] - [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] - [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] - public class Resources { - - private static global::System.Resources.ResourceManager resourceMan; - - private static global::System.Globalization.CultureInfo resourceCulture; - - [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] - internal Resources() { - } - - /// - /// Returns the cached ResourceManager instance used by this class. - /// - [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] - public static global::System.Resources.ResourceManager ResourceManager { - get { - if (object.ReferenceEquals(resourceMan, null)) { - global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("ReadText.LocalizedDemo.Properties.Resources", typeof(Resources).Assembly); - resourceMan = temp; - } - return resourceMan; - } - } - - /// - /// Overrides the current thread's CurrentUICulture property for all - /// resource lookups using this strongly typed resource class. - /// - [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] - public static global::System.Globalization.CultureInfo Culture { - get { - return resourceCulture; - } - set { - resourceCulture = value; - } - } - - /// - /// Looks up a localized string similar to bytes. - /// - public static string Bytes { - get { - return ResourceManager.GetString("Bytes", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to normal scenario. - /// - public static string ExamplesNormalScenario { - get { - return ResourceManager.GetString("ExamplesNormalScenario", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to read more lines. - /// - public static string ExamplesReadMoreLines { - get { - return ResourceManager.GetString("ExamplesReadMoreLines", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to specify bytes. - /// - public static string ExamplesSpecifyBytes { - get { - return ResourceManager.GetString("ExamplesSpecifyBytes", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to suppress summary. - /// - public static string ExamplesSuppressSummary { - get { - return ResourceManager.GetString("ExamplesSuppressSummary", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to from bottom:. - /// - public static string FromBottom { - get { - return ResourceManager.GetString("FromBottom", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to from top:. - /// - public static string FromTop { - get { - return ResourceManager.GetString("FromTop", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Bytes to be printed from the beginning or end of the file.. - /// - public static string HelpTextBytes { - get { - return ResourceManager.GetString("HelpTextBytes", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Input file to be processed.. - /// - public static string HelpTextFileName { - get { - return ResourceManager.GetString("HelpTextFileName", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Lines to be printed from the beginning or end of the file.. - /// - public static string HelpTextLines { - get { - return ResourceManager.GetString("HelpTextLines", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Suppresses summary messages.. - /// - public static string HelpTextQuiet { - get { - return ResourceManager.GetString("HelpTextQuiet", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Displays first lines of a file.. - /// - public static string HelpTextVerbHead { - get { - return ResourceManager.GetString("HelpTextVerbHead", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Displays last lines of a file.. - /// - public static string HelpTextVerbTail { - get { - return ResourceManager.GetString("HelpTextVerbTail", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to lines. - /// - public static string Lines { - get { - return ResourceManager.GetString("Lines", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Reading . - /// - public static string Reading { - get { - return ResourceManager.GetString("Reading", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Možnost '{0}' je definována ve špatném formátu.. - /// - public static string SentenceBadFormatConversionErrorOption { - get { - return ResourceManager.GetString("SentenceBadFormatConversionErrorOption", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to A value not bound to option name is defined with a bad format.. - /// - public static string SentenceBadFormatConversionErrorValue { - get { - return ResourceManager.GetString("SentenceBadFormatConversionErrorValue", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Token '{0}' is not recognized.. - /// - public static string SentenceBadFormatTokenError { - get { - return ResourceManager.GetString("SentenceBadFormatTokenError", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Verb '{0}' is not recognized.. - /// - public static string SentenceBadVerbSelectedError { - get { - return ResourceManager.GetString("SentenceBadVerbSelectedError", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to ERROR(S):. - /// - public static string SentenceErrorsHeadingText { - get { - return ResourceManager.GetString("SentenceErrorsHeadingText", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Display this help screen.. - /// - public static string SentenceHelpCommandTextOption { - get { - return ResourceManager.GetString("SentenceHelpCommandTextOption", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Display more information on a specific command.. - /// - public static string SentenceHelpCommandTextVerb { - get { - return ResourceManager.GetString("SentenceHelpCommandTextVerb", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Required option '{0}' is missing.. - /// - public static string SentenceMissingRequiredOptionError { - get { - return ResourceManager.GetString("SentenceMissingRequiredOptionError", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to A required value not bound to option name is missing.. - /// - public static string SentenceMissingRequiredValueError { - get { - return ResourceManager.GetString("SentenceMissingRequiredValueError", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Option '{0}' has no value.. - /// - public static string SentenceMissingValueOptionError { - get { - return ResourceManager.GetString("SentenceMissingValueOptionError", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Options: {0} are not compatible with {1}.. - /// - public static string SentenceMutuallyExclusiveSetErrors { - get { - return ResourceManager.GetString("SentenceMutuallyExclusiveSetErrors", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to No verb selected.. - /// - public static string SentenceNoVerbSelectedError { - get { - return ResourceManager.GetString("SentenceNoVerbSelectedError", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Option '{0}' is defined multiple times.. - /// - public static string SentenceRepeatedOptionError { - get { - return ResourceManager.GetString("SentenceRepeatedOptionError", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Required.. - /// - public static string SentenceRequiredWord { - get { - return ResourceManager.GetString("SentenceRequiredWord", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to A sequence option '{0}' is defined with fewer or more items than required.. - /// - public static string SentenceSequenceOutOfRangeErrorOption { - get { - return ResourceManager.GetString("SentenceSequenceOutOfRangeErrorOption", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to A sequence value not bound to option name is defined with few items than required.. - /// - public static string SentenceSequenceOutOfRangeErrorValue { - get { - return ResourceManager.GetString("SentenceSequenceOutOfRangeErrorValue", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Error setting value to option '{0}': {1}. - /// - public static string SentenceSetValueExceptionError { - get { - return ResourceManager.GetString("SentenceSetValueExceptionError", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Option '{0}' is unknown.. - /// - public static string SentenceUnknownOptionError { - get { - return ResourceManager.GetString("SentenceUnknownOptionError", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to USAGE:. - /// - public static string SentenceUsageHeadingText { - get { - return ResourceManager.GetString("SentenceUsageHeadingText", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Display version information.. - /// - public static string SentenceVersionCommandText { - get { - return ResourceManager.GetString("SentenceVersionCommandText", resourceCulture); - } - } - } -} +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.42000 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace ReadText.LocalizedDemo.Properties { + using System; + + + /// + /// A strongly-typed resource class, for looking up localized strings, etc. + /// + // This class was auto-generated by the StronglyTypedResourceBuilder + // class via a tool like ResGen or Visual Studio. + // To add or remove a member, edit your .ResX file then rerun ResGen + // with the /str option, or rebuild your VS project. + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "15.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + public class Resources { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal Resources() { + } + + /// + /// Returns the cached ResourceManager instance used by this class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + public static global::System.Resources.ResourceManager ResourceManager { + get { + if (object.ReferenceEquals(resourceMan, null)) { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("ReadText.LocalizedDemo.Properties.Resources", typeof(Resources).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + /// + /// Overrides the current thread's CurrentUICulture property for all + /// resource lookups using this strongly typed resource class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + public static global::System.Globalization.CultureInfo Culture { + get { + return resourceCulture; + } + set { + resourceCulture = value; + } + } + + /// + /// Looks up a localized string similar to bytes. + /// + public static string Bytes { + get { + return ResourceManager.GetString("Bytes", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to normal scenario. + /// + public static string ExamplesNormalScenario { + get { + return ResourceManager.GetString("ExamplesNormalScenario", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to read more lines. + /// + public static string ExamplesReadMoreLines { + get { + return ResourceManager.GetString("ExamplesReadMoreLines", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to specify bytes. + /// + public static string ExamplesSpecifyBytes { + get { + return ResourceManager.GetString("ExamplesSpecifyBytes", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to suppress summary. + /// + public static string ExamplesSuppressSummary { + get { + return ResourceManager.GetString("ExamplesSuppressSummary", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to from bottom:. + /// + public static string FromBottom { + get { + return ResourceManager.GetString("FromBottom", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to from top:. + /// + public static string FromTop { + get { + return ResourceManager.GetString("FromTop", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Bytes to be printed from the beginning or end of the file.. + /// + public static string HelpTextBytes { + get { + return ResourceManager.GetString("HelpTextBytes", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Input file to be processed.. + /// + public static string HelpTextFileName { + get { + return ResourceManager.GetString("HelpTextFileName", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Lines to be printed from the beginning or end of the file.. + /// + public static string HelpTextLines { + get { + return ResourceManager.GetString("HelpTextLines", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Suppresses summary messages.. + /// + public static string HelpTextQuiet { + get { + return ResourceManager.GetString("HelpTextQuiet", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Displays first lines of a file.. + /// + public static string HelpTextVerbHead { + get { + return ResourceManager.GetString("HelpTextVerbHead", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Displays last lines of a file.. + /// + public static string HelpTextVerbTail { + get { + return ResourceManager.GetString("HelpTextVerbTail", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to lines. + /// + public static string Lines { + get { + return ResourceManager.GetString("Lines", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Reading . + /// + public static string Reading { + get { + return ResourceManager.GetString("Reading", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Možnost '{0}' je definována ve špatném formátu.. + /// + public static string SentenceBadFormatConversionErrorOption { + get { + return ResourceManager.GetString("SentenceBadFormatConversionErrorOption", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to A value not bound to option name is defined with a bad format.. + /// + public static string SentenceBadFormatConversionErrorValue { + get { + return ResourceManager.GetString("SentenceBadFormatConversionErrorValue", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Token '{0}' is not recognized.. + /// + public static string SentenceBadFormatTokenError { + get { + return ResourceManager.GetString("SentenceBadFormatTokenError", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Verb '{0}' is not recognized.. + /// + public static string SentenceBadVerbSelectedError { + get { + return ResourceManager.GetString("SentenceBadVerbSelectedError", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to ERROR(S):. + /// + public static string SentenceErrorsHeadingText { + get { + return ResourceManager.GetString("SentenceErrorsHeadingText", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Display this help screen.. + /// + public static string SentenceHelpCommandTextOption { + get { + return ResourceManager.GetString("SentenceHelpCommandTextOption", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Display more information on a specific command.. + /// + public static string SentenceHelpCommandTextVerb { + get { + return ResourceManager.GetString("SentenceHelpCommandTextVerb", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Required option '{0}' is missing.. + /// + public static string SentenceMissingRequiredOptionError { + get { + return ResourceManager.GetString("SentenceMissingRequiredOptionError", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to A required value not bound to option name is missing.. + /// + public static string SentenceMissingRequiredValueError { + get { + return ResourceManager.GetString("SentenceMissingRequiredValueError", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Option '{0}' has no value.. + /// + public static string SentenceMissingValueOptionError { + get { + return ResourceManager.GetString("SentenceMissingValueOptionError", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Options: {0} are not compatible with {1}.. + /// + public static string SentenceMutuallyExclusiveSetErrors { + get { + return ResourceManager.GetString("SentenceMutuallyExclusiveSetErrors", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to No verb selected.. + /// + public static string SentenceNoVerbSelectedError { + get { + return ResourceManager.GetString("SentenceNoVerbSelectedError", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Option '{0}' is defined multiple times.. + /// + public static string SentenceRepeatedOptionError { + get { + return ResourceManager.GetString("SentenceRepeatedOptionError", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Required.. + /// + public static string SentenceRequiredWord { + get { + return ResourceManager.GetString("SentenceRequiredWord", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to A sequence option '{0}' is defined with fewer or more items than required.. + /// + public static string SentenceSequenceOutOfRangeErrorOption { + get { + return ResourceManager.GetString("SentenceSequenceOutOfRangeErrorOption", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to A sequence value not bound to option name is defined with few items than required.. + /// + public static string SentenceSequenceOutOfRangeErrorValue { + get { + return ResourceManager.GetString("SentenceSequenceOutOfRangeErrorValue", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Error setting value to option '{0}': {1}. + /// + public static string SentenceSetValueExceptionError { + get { + return ResourceManager.GetString("SentenceSetValueExceptionError", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Option '{0}' is unknown.. + /// + public static string SentenceUnknownOptionError { + get { + return ResourceManager.GetString("SentenceUnknownOptionError", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to USAGE:. + /// + public static string SentenceUsageHeadingText { + get { + return ResourceManager.GetString("SentenceUsageHeadingText", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Display version information.. + /// + public static string SentenceVersionCommandText { + get { + return ResourceManager.GetString("SentenceVersionCommandText", resourceCulture); + } + } + } +} diff --git a/demo/ReadText.LocalizedDemo/Properties/Resources.resx b/demo/ReadText.LocalizedDemo/Properties/Resources.resx index afdea3d0..c462526e 100644 --- a/demo/ReadText.LocalizedDemo/Properties/Resources.resx +++ b/demo/ReadText.LocalizedDemo/Properties/Resources.resx @@ -1,225 +1,228 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - text/microsoft-resx - - - 2.0 - - - System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - Reading - - - lines - - - bytes - - - from top: - - - from bottom: - - - Lines to be printed from the beginning or end of the file. - - - Bytes to be printed from the beginning or end of the file. - - - Suppresses summary messages. - - - Input file to be processed. - - - Displays first lines of a file. - - - normal scenario - - - specify bytes - - - suppress summary - - - read more lines - - - Displays last lines of a file. - - - Možnost '{0}' je definována ve špatném formátu. - - - A value not bound to option name is defined with a bad format. - - - Token '{0}' is not recognized. - - - Verb '{0}' is not recognized. - - - ERROR(S): - - - Display this help screen. - - - Display more information on a specific command. - - - Required option '{0}' is missing. - - - A required value not bound to option name is missing. - - - Option '{0}' has no value. - - - Options: {0} are not compatible with {1}. - - - No verb selected. - - - Option '{0}' is defined multiple times. - - - Required. - - - A sequence option '{0}' is defined with fewer or more items than required. - - - A sequence value not bound to option name is defined with few items than required. - - - Error setting value to option '{0}': {1} - - - Option '{0}' is unknown. - - - USAGE: - - - Display version information. - - \ No newline at end of file + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + Reading + + + lines + + + bytes + + + from top: + + + from bottom: + + + Lines to be printed from the beginning or end of the file. + + + Bytes to be printed from the beginning or end of the file. + + + Suppresses summary messages. + + + Input file to be processed. + + + Displays first lines of a file. + + + normal scenario + + + specify bytes + + + suppress summary + + + read more lines + + + Displays last lines of a file. + + + Možnost '{0}' je definována ve špatném formátu. + + + A value not bound to option name is defined with a bad format. + + + Token '{0}' is not recognized. + + + Verb '{0}' is not recognized. + + + ERROR(S): + + + Display this help screen. + + + Display more information on a specific command. + + + Required option '{0}' is missing. + + + A required value not bound to option name is missing. + + + Option '{0}' has no value. + + + Options: {0} are not compatible with {1}. + + + No verb selected. + + + Option '{0}' is defined multiple times. + + + Required. + + + A sequence option '{0}' is defined with fewer or more items than required. + + + A sequence value not bound to option name is defined with few items than required. + + + Error setting value to option '{0}': {1} + + + Option '{0}' is unknown. + + + USAGE: + + + Display version information. + + + Required positional argument '{0}' does not provide a value. + + diff --git a/src/CommandLine/Core/SpecificationPropertyRules.cs b/src/CommandLine/Core/SpecificationPropertyRules.cs index 5dc1a406..d50f617d 100644 --- a/src/CommandLine/Core/SpecificationPropertyRules.cs +++ b/src/CommandLine/Core/SpecificationPropertyRules.cs @@ -148,7 +148,9 @@ where sp.Value.IsNothing() select sp.Specification); return from sp in missing - select new MissingRequiredOptionError(sp.FromSpecification()); + select + sp.IsOption() ? new MissingRequiredOptionError(sp.FromSpecification()) as Error + : new MissingRequiredValueError(sp.FromSpecification(), ((ValueSpecification)sp).Index) as Error; }; } diff --git a/src/CommandLine/Error.cs b/src/CommandLine/Error.cs index 21c2fdcd..8ea7072c 100644 --- a/src/CommandLine/Error.cs +++ b/src/CommandLine/Error.cs @@ -28,6 +28,10 @@ public enum ErrorType /// MissingRequiredOptionError, /// + /// Value of type. + /// + MissingRequiredValueError, + /// /// Value of type. /// MutuallyExclusiveSetError, @@ -355,6 +359,24 @@ internal MissingRequiredOptionError(NameInfo nameInfo) } } + /// + /// Models an error generated when a required value is mssing. + /// + public sealed class MissingRequiredValueError : NamedError + { + internal MissingRequiredValueError(NameInfo nameInfo, int valuePostion) + : base(ErrorType.MissingRequiredValueError, nameInfo) + { + this.ValuePosition = valuePostion; + } + + /// + /// Gets the value position of the missing positional argument. + /// + /// + public int ValuePosition { get; } + } + /// /// Models an error generated when a an option from another set is defined. /// diff --git a/src/CommandLine/Text/SentenceBuilder.cs b/src/CommandLine/Text/SentenceBuilder.cs index c8537542..f49c76fc 100644 --- a/src/CommandLine/Text/SentenceBuilder.cs +++ b/src/CommandLine/Text/SentenceBuilder.cs @@ -130,6 +130,11 @@ public override Func FormatError return errMisssing.NameInfo.Equals(NameInfo.EmptyName) ? "A required value not bound to option name is missing." : "Required option '".JoinTo(errMisssing.NameInfo.NameText, "' is missing."); + case ErrorType.MissingRequiredValueError: + var errMissing = ((MissingRequiredValueError)error); + return errMissing.NameInfo.Equals(NameInfo.EmptyName) + ? "A required value at position '".JoinTo(Convert.ToString(errMissing.ValuePosition), "' is missing.") + : "Positional argument '".JoinTo(errMissing.NameInfo.NameText, "' is missing."); case ErrorType.BadFormatConversionError: var badFormat = ((BadFormatConversionError)error); return badFormat.NameInfo.Equals(NameInfo.EmptyName) From a7159fbcab2911280a71fe7e6e9840e84b3f1dc3 Mon Sep 17 00:00:00 2001 From: Carsten Igel Date: Wed, 16 Jun 2021 20:43:06 +0200 Subject: [PATCH 3/3] Tests adjusted The tests have been adjusted. The command line positional arguments will raise a different error. This is semantically a breaking change. --- tests/CommandLine.Tests/Unit/Core/InstanceBuilderTests.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/CommandLine.Tests/Unit/Core/InstanceBuilderTests.cs b/tests/CommandLine.Tests/Unit/Core/InstanceBuilderTests.cs index 3ef33261..77e1c2df 100644 --- a/tests/CommandLine.Tests/Unit/Core/InstanceBuilderTests.cs +++ b/tests/CommandLine.Tests/Unit/Core/InstanceBuilderTests.cs @@ -564,10 +564,10 @@ public void Omitting_names_assumes_identifier_as_long_name(string[] arguments, s } [Fact] - public void Breaking_required_constraint_in_string_scalar_as_value_generates_MissingRequiredOptionError() + public void Breaking_required_constraint_in_string_scalar_as_value_generates_MissingRequiredValueError() { // Fixture setup - var expectedResult = new[] { new MissingRequiredOptionError(NameInfo.EmptyName) }; + var expectedResult = new[] { new MissingRequiredValueError(NameInfo.EmptyName, 0) }; // Exercize system var result = InvokeBuild(