From ff5a6870cf5b066c6d17a4ab477ca5fe89eaa0a5 Mon Sep 17 00:00:00 2001 From: AntonC9019 Date: Wed, 22 Jun 2022 23:11:54 +0300 Subject: [PATCH 1/2] Make use of nameof, use is with pattern matching instead of as and null check, typos --- src/CommandLine/Core/SpecificationProperty.cs | 2 +- src/CommandLine/Core/Token.cs | 3 +- src/CommandLine/Error.cs | 5 ++-- .../Infrastructure/ReflectionHelper.cs | 9 +----- src/CommandLine/NameInfo.cs | 9 +++--- src/CommandLine/NullInstance.cs | 4 +-- src/CommandLine/OptionAttribute.cs | 6 ++-- src/CommandLine/Parser.cs | 16 +++++----- src/CommandLine/Text/CopyrightInfo.cs | 4 +-- src/CommandLine/Text/Example.cs | 9 +++--- src/CommandLine/Text/HeadingInfo.cs | 4 +-- src/CommandLine/Text/HelpText.cs | 30 +++++++++---------- src/CommandLine/Text/SentenceBuilder.cs | 6 ++-- .../CultureInfoExtensions.cs | 4 +-- 14 files changed, 50 insertions(+), 61 deletions(-) diff --git a/src/CommandLine/Core/SpecificationProperty.cs b/src/CommandLine/Core/SpecificationProperty.cs index f6cea24f..dd216cd8 100644 --- a/src/CommandLine/Core/SpecificationProperty.cs +++ b/src/CommandLine/Core/SpecificationProperty.cs @@ -21,7 +21,7 @@ private SpecificationProperty(Specification specification, PropertyInfo property public static SpecificationProperty Create(Specification specification, PropertyInfo property, Maybe value) { - if (value == null) throw new ArgumentNullException("value"); + if (value == null) throw new ArgumentNullException(nameof(value)); return new SpecificationProperty(specification, property, value); } diff --git a/src/CommandLine/Core/Token.cs b/src/CommandLine/Core/Token.cs index 90bbe54d..fcc6d5f6 100644 --- a/src/CommandLine/Core/Token.cs +++ b/src/CommandLine/Core/Token.cs @@ -62,8 +62,7 @@ public Name(string text) public override bool Equals(object obj) { - var other = obj as Name; - if (other != null) + if (obj is Name other) { return Equals(other); } diff --git a/src/CommandLine/Error.cs b/src/CommandLine/Error.cs index 21c2fdcd..6c821a9d 100644 --- a/src/CommandLine/Error.cs +++ b/src/CommandLine/Error.cs @@ -189,7 +189,7 @@ public abstract class TokenError : Error, IEquatable protected internal TokenError(ErrorType tag, string token) : base(tag) { - if (token == null) throw new ArgumentNullException("token"); + if (token == null) throw new ArgumentNullException(nameof(token)); this.token = token; } @@ -209,8 +209,7 @@ public string Token /// true if the specified is equal to the current ; otherwise, false. public override bool Equals(object obj) { - var other = obj as TokenError; - if (other != null) + if (obj is TokenError other) { return Equals(other); } diff --git a/src/CommandLine/Infrastructure/ReflectionHelper.cs b/src/CommandLine/Infrastructure/ReflectionHelper.cs index 47fe70ea..d5ebb439 100644 --- a/src/CommandLine/Infrastructure/ReflectionHelper.cs +++ b/src/CommandLine/Infrastructure/ReflectionHelper.cs @@ -29,14 +29,7 @@ static class ReflectionHelper /// public static void SetAttributeOverride(IEnumerable overrides) { - if (overrides != null) - { - _overrides = overrides.ToDictionary(attr => attr.GetType(), attr => attr); - } - else - { - _overrides = null; - } + _overrides = overrides?.ToDictionary(attr => attr.GetType(), attr => attr); } public static Maybe GetAttribute() diff --git a/src/CommandLine/NameInfo.cs b/src/CommandLine/NameInfo.cs index baf259ef..fa7d73b2 100644 --- a/src/CommandLine/NameInfo.cs +++ b/src/CommandLine/NameInfo.cs @@ -20,8 +20,8 @@ public sealed class NameInfo : IEquatable internal NameInfo(string shortName, string longName) { - if (shortName == null) throw new ArgumentNullException("shortName"); - if (longName == null) throw new ArgumentNullException("longName"); + if (shortName == null) throw new ArgumentNullException(nameof(shortName)); + if (longName == null) throw new ArgumentNullException(nameof(longName)); this.longName = longName; this.shortName = shortName; @@ -65,8 +65,7 @@ public string NameText /// true if the specified is equal to the current ; otherwise, false. public override bool Equals(object obj) { - var other = obj as NameInfo; - if (other != null) + if (obj is NameInfo other) { return Equals(other); } @@ -98,4 +97,4 @@ public bool Equals(NameInfo other) return ShortName.Equals(other.ShortName) && LongName.Equals(other.LongName); } } -} \ No newline at end of file +} diff --git a/src/CommandLine/NullInstance.cs b/src/CommandLine/NullInstance.cs index c2ebd77a..820a2f30 100644 --- a/src/CommandLine/NullInstance.cs +++ b/src/CommandLine/NullInstance.cs @@ -3,10 +3,10 @@ namespace CommandLine { /// - /// Models a null result when constructing a in a faling verbs scenario. + /// Models a null result when constructing a in a failing verbs scenario. /// public sealed class NullInstance { internal NullInstance() { } } -} \ No newline at end of file +} diff --git a/src/CommandLine/OptionAttribute.cs b/src/CommandLine/OptionAttribute.cs index 6ae51dac..c56a989f 100644 --- a/src/CommandLine/OptionAttribute.cs +++ b/src/CommandLine/OptionAttribute.cs @@ -21,8 +21,8 @@ public sealed class OptionAttribute : BaseAttribute private OptionAttribute(string shortName, string longName) : base() { - if (shortName == null) throw new ArgumentNullException("shortName"); - if (longName == null) throw new ArgumentNullException("longName"); + if (shortName == null) throw new ArgumentNullException(nameof(shortName)); + if (longName == null) throw new ArgumentNullException(nameof(longName)); this.shortName = shortName; this.longName = longName; @@ -91,7 +91,7 @@ public string SetName get { return setName; } set { - if (value == null) throw new ArgumentNullException("value"); + if (value == null) throw new ArgumentNullException(nameof(value)); setName = value; } diff --git a/src/CommandLine/Parser.cs b/src/CommandLine/Parser.cs index 4301aa52..55d18276 100644 --- a/src/CommandLine/Parser.cs +++ b/src/CommandLine/Parser.cs @@ -37,7 +37,7 @@ public Parser() /// aspects and behaviors of the parser. public Parser(Action configuration) { - if (configuration == null) throw new ArgumentNullException("configuration"); + if (configuration == null) throw new ArgumentNullException(nameof(configuration)); settings = new ParserSettings(); configuration(settings); @@ -85,7 +85,7 @@ public ParserSettings Settings /// Thrown if one or more arguments are null. public ParserResult ParseArguments(IEnumerable args) { - if (args == null) throw new ArgumentNullException("args"); + if (args == null) throw new ArgumentNullException(nameof(args)); var factory = typeof(T).IsMutable() ? Maybe.Just>(Activator.CreateInstance) @@ -118,9 +118,9 @@ public ParserResult ParseArguments(IEnumerable args) /// Thrown if one or more arguments are null. public ParserResult ParseArguments(Func factory, IEnumerable args) { - if (factory == null) throw new ArgumentNullException("factory"); - if (!typeof(T).IsMutable()) throw new ArgumentException("factory"); - if (args == null) throw new ArgumentNullException("args"); + if (factory == null) throw new ArgumentNullException(nameof(factory)); + if (!typeof(T).IsMutable()) throw new ArgumentException(nameof(factory)); + if (args == null) throw new ArgumentNullException(nameof(args)); return MakeParserResult( InstanceBuilder.Build( @@ -151,9 +151,9 @@ public ParserResult ParseArguments(Func factory, IEnumerable ar /// All types must expose a parameterless constructor. It's strongly recommended to use a generic overload. public ParserResult ParseArguments(IEnumerable args, params Type[] types) { - if (args == null) throw new ArgumentNullException("args"); - if (types == null) throw new ArgumentNullException("types"); - if (types.Length == 0) throw new ArgumentOutOfRangeException("types"); + if (args == null) throw new ArgumentNullException(nameof(args)); + if (types == null) throw new ArgumentNullException(nameof(types)); + if (types.Length == 0) throw new ArgumentOutOfRangeException(nameof(types)); return MakeParserResult( InstanceChooser.Choose( diff --git a/src/CommandLine/Text/CopyrightInfo.cs b/src/CommandLine/Text/CopyrightInfo.cs index c8bc3593..dd4a780e 100644 --- a/src/CommandLine/Text/CopyrightInfo.cs +++ b/src/CommandLine/Text/CopyrightInfo.cs @@ -71,8 +71,8 @@ public CopyrightInfo(string author, params int[] years) /// Thrown when parameter is not supplied. public CopyrightInfo(bool isSymbolUpper, string author, params int[] copyrightYears) { - if (string.IsNullOrWhiteSpace(author)) throw new ArgumentException("author"); - if (copyrightYears.Length == 0) throw new ArgumentOutOfRangeException("copyrightYears"); + if (string.IsNullOrWhiteSpace(author)) throw new ArgumentException(nameof(author)); + if (copyrightYears.Length == 0) throw new ArgumentOutOfRangeException(nameof(copyrightYears)); const int ExtraLength = 10; this.isSymbolUpper = isSymbolUpper; diff --git a/src/CommandLine/Text/Example.cs b/src/CommandLine/Text/Example.cs index 4d73b754..adbe2983 100644 --- a/src/CommandLine/Text/Example.cs +++ b/src/CommandLine/Text/Example.cs @@ -23,9 +23,9 @@ public sealed class Example : IEquatable /// A sample instance. public Example(string helpText, IEnumerable formatStyles, object sample) { - if (string.IsNullOrEmpty(helpText)) throw new ArgumentException("helpText can't be null or empty", "helpText"); - if (formatStyles == null) throw new ArgumentNullException("formatStyles"); - if (sample == null) throw new ArgumentNullException("sample"); + if (string.IsNullOrEmpty(helpText)) throw new ArgumentException("helpText can't be null or empty", nameof(helpText)); + if (formatStyles == null) throw new ArgumentNullException(nameof(formatStyles)); + if (sample == null) throw new ArgumentNullException(nameof(sample)); this.helpText = helpText; this.formatStyles = formatStyles; @@ -84,8 +84,7 @@ public object Sample /// true if the specified is equal to the current ; otherwise, false. public override bool Equals(object obj) { - var other = obj as Example; - if (other != null) + if (obj is Example other) { return Equals(other); } diff --git a/src/CommandLine/Text/HeadingInfo.cs b/src/CommandLine/Text/HeadingInfo.cs index 41e264f7..5be770a4 100644 --- a/src/CommandLine/Text/HeadingInfo.cs +++ b/src/CommandLine/Text/HeadingInfo.cs @@ -102,8 +102,8 @@ public override string ToString() /// Thrown when parameter is null. public void WriteMessage(string message, TextWriter writer) { - if (string.IsNullOrWhiteSpace("message")) throw new ArgumentException("message"); - if (writer == null) throw new ArgumentNullException("writer"); + if (string.IsNullOrWhiteSpace(message)) throw new ArgumentException(nameof(message)); + if (writer == null) throw new ArgumentNullException(nameof(writer)); writer.WriteLine( new StringBuilder(programName.Length + message.Length + 2) diff --git a/src/CommandLine/Text/HelpText.cs b/src/CommandLine/Text/HelpText.cs index f5e9a7b9..3c400356 100644 --- a/src/CommandLine/Text/HelpText.cs +++ b/src/CommandLine/Text/HelpText.cs @@ -177,9 +177,9 @@ public HelpText(string heading, string copyright) /// Thrown when one or more parameters are null or empty strings. public HelpText(SentenceBuilder sentenceBuilder, string heading, string copyright) { - if (sentenceBuilder == null) throw new ArgumentNullException("sentenceBuilder"); - if (heading == null) throw new ArgumentNullException("heading"); - if (copyright == null) throw new ArgumentNullException("copyright"); + if (sentenceBuilder == null) throw new ArgumentNullException(nameof(sentenceBuilder)); + if (heading == null) throw new ArgumentNullException(nameof(heading)); + if (copyright == null) throw new ArgumentNullException(nameof(copyright)); preOptionsHelp = new StringBuilder(BuilderCapacity); postOptionsHelp = new StringBuilder(BuilderCapacity); @@ -211,7 +211,7 @@ public string Heading get { return heading; } set { - if (value == null) throw new ArgumentNullException("value"); + if (value == null) throw new ArgumentNullException(nameof(value)); heading = value; } @@ -226,7 +226,7 @@ public string Copyright get { return copyright; } set { - if (value == null) throw new ArgumentNullException("value"); + if (value == null) throw new ArgumentNullException(nameof(value)); copyright = value; } @@ -419,7 +419,7 @@ public static HelpText AutoBuild(ParserResult parserResult, int maxDisplay public static HelpText AutoBuild(ParserResult parserResult, Func onError, int maxDisplayWidth = DefaultMaximumLength) { if (parserResult.Tag != ParserResultType.NotParsed) - throw new ArgumentException("Excepting NotParsed type.", "parserResult"); + throw new ArgumentException("Excepting NotParsed type.", nameof(parserResult)); var errors = ((NotParsed)parserResult).Errors; @@ -455,8 +455,8 @@ public static HelpText AutoBuild(ParserResult parserResult, FuncThe instance. public static HelpText DefaultParsingErrorsHandler(ParserResult parserResult, HelpText current) { - if (parserResult == null) throw new ArgumentNullException("parserResult"); - if (current == null) throw new ArgumentNullException("current"); + if (parserResult == null) throw new ArgumentNullException(nameof(parserResult)); + if (current == null) throw new ArgumentNullException(nameof(current)); if (((NotParsed)parserResult).Errors.OnlyMeaningfulOnes().Empty()) return current; @@ -558,7 +558,7 @@ public HelpText AddPostOptionsText(string text) /// Thrown when parameter is null. public HelpText AddOptions(ParserResult result) { - if (result == null) throw new ArgumentNullException("result"); + if (result == null) throw new ArgumentNullException(nameof(result)); return AddOptionsImpl( GetSpecificationsFromType(result.TypeInfo.Current), @@ -575,8 +575,8 @@ public HelpText AddOptions(ParserResult result) /// Thrown if array is empty. public HelpText AddVerbs(params Type[] types) { - if (types == null) throw new ArgumentNullException("types"); - if (types.Length == 0) throw new ArgumentOutOfRangeException("types"); + if (types == null) throw new ArgumentNullException(nameof(types)); + if (types.Length == 0) throw new ArgumentOutOfRangeException(nameof(types)); return AddOptionsImpl( AdaptVerbsToSpecifications(types), @@ -593,7 +593,7 @@ public HelpText AddVerbs(params Type[] types) /// Thrown when parameter is null. public HelpText AddOptions(int maximumLength, ParserResult result) { - if (result == null) throw new ArgumentNullException("result"); + if (result == null) throw new ArgumentNullException(nameof(result)); return AddOptionsImpl( GetSpecificationsFromType(result.TypeInfo.Current), @@ -611,8 +611,8 @@ public HelpText AddOptions(int maximumLength, ParserResult result) /// Thrown if array is empty. public HelpText AddVerbs(int maximumLength, params Type[] types) { - if (types == null) throw new ArgumentNullException("types"); - if (types.Length == 0) throw new ArgumentOutOfRangeException("types"); + if (types == null) throw new ArgumentNullException(nameof(types)); + if (types.Length == 0) throw new ArgumentOutOfRangeException(nameof(types)); return AddOptionsImpl( AdaptVerbsToSpecifications(types), @@ -654,7 +654,7 @@ public static IEnumerable RenderParsingErrorsTextAsLines( Func, string> formatMutuallyExclusiveSetErrors, int indent) { - if (parserResult == null) throw new ArgumentNullException("parserResult"); + if (parserResult == null) throw new ArgumentNullException(nameof(parserResult)); var meaningfulErrors = ((NotParsed)parserResult).Errors.OnlyMeaningfulOnes(); diff --git a/src/CommandLine/Text/SentenceBuilder.cs b/src/CommandLine/Text/SentenceBuilder.cs index c8537542..2d9a2ba5 100644 --- a/src/CommandLine/Text/SentenceBuilder.cs +++ b/src/CommandLine/Text/SentenceBuilder.cs @@ -126,10 +126,10 @@ public override Func FormatError case ErrorType.UnknownOptionError: return "Option '".JoinTo(((UnknownOptionError)error).Token, "' is unknown."); case ErrorType.MissingRequiredOptionError: - var errMisssing = ((MissingRequiredOptionError)error); - return errMisssing.NameInfo.Equals(NameInfo.EmptyName) + var errMissing = ((MissingRequiredOptionError)error); + return errMissing.NameInfo.Equals(NameInfo.EmptyName) ? "A required value not bound to option name is missing." - : "Required option '".JoinTo(errMisssing.NameInfo.NameText, "' is missing."); + : "Required option '".JoinTo(errMissing.NameInfo.NameText, "' is missing."); case ErrorType.BadFormatConversionError: var badFormat = ((BadFormatConversionError)error); return badFormat.NameInfo.Equals(NameInfo.EmptyName) diff --git a/tests/CommandLine.Tests/CultureInfoExtensions.cs b/tests/CommandLine.Tests/CultureInfoExtensions.cs index c49357ed..29bca882 100644 --- a/tests/CommandLine.Tests/CultureInfoExtensions.cs +++ b/tests/CommandLine.Tests/CultureInfoExtensions.cs @@ -16,11 +16,11 @@ static class CultureInfoExtensions { public static CultureHandlers MakeCultureHandlers(this CultureInfo newCulture) { - var currentCulutre = Thread.CurrentThread.CurrentCulture; + var currentClutre = Thread.CurrentThread.CurrentCulture; Action changer = () => Thread.CurrentThread.CurrentCulture = newCulture; - Action resetter = () => Thread.CurrentThread.CurrentCulture = currentCulutre; + Action resetter = () => Thread.CurrentThread.CurrentCulture = currentClutre; return new CultureHandlers { ChangeCulture = changer, ResetCulture = resetter }; } From 30a902199f8861ce15abcf721b7dbaab94ecc046 Mon Sep 17 00:00:00 2001 From: AntonC9019 Date: Wed, 22 Jun 2022 23:13:42 +0300 Subject: [PATCH 2/2] Current culture was misspelt --- tests/CommandLine.Tests/CultureInfoExtensions.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/CommandLine.Tests/CultureInfoExtensions.cs b/tests/CommandLine.Tests/CultureInfoExtensions.cs index 29bca882..c5c174c4 100644 --- a/tests/CommandLine.Tests/CultureInfoExtensions.cs +++ b/tests/CommandLine.Tests/CultureInfoExtensions.cs @@ -16,11 +16,11 @@ static class CultureInfoExtensions { public static CultureHandlers MakeCultureHandlers(this CultureInfo newCulture) { - var currentClutre = Thread.CurrentThread.CurrentCulture; + var currentCulture = Thread.CurrentThread.CurrentCulture; Action changer = () => Thread.CurrentThread.CurrentCulture = newCulture; - Action resetter = () => Thread.CurrentThread.CurrentCulture = currentClutre; + Action resetter = () => Thread.CurrentThread.CurrentCulture = currentCulture; return new CultureHandlers { ChangeCulture = changer, ResetCulture = resetter }; }