From 4fc0c019829b8bd2e61ad35f7bc5ac8f9c081cb8 Mon Sep 17 00:00:00 2001 From: MASES Public Developers Team <94312179+masesdevelopers@users.noreply.github.com> Date: Tue, 14 Nov 2023 18:25:31 +0100 Subject: [PATCH] Code simplification with string interpolation and minor changes (#43) * Code simplification with string interpolation * Version update --- src/net/CLIParser/ArgumentMetadata.cs | 66 ++++++++++++------------ src/net/CLIParser/CLIParser.cs | 39 ++++++-------- src/net/CLIParser/CLIParser.csproj | 2 +- tests/CLIParserTest/CLIParserTest.csproj | 2 +- 4 files changed, 49 insertions(+), 60 deletions(-) diff --git a/src/net/CLIParser/ArgumentMetadata.cs b/src/net/CLIParser/ArgumentMetadata.cs index c18e237..809ee87 100644 --- a/src/net/CLIParser/ArgumentMetadata.cs +++ b/src/net/CLIParser/ArgumentMetadata.cs @@ -69,7 +69,7 @@ public enum ArgumentType /// KeyValue, /// - /// Represents an argument whose value if the next argument + /// Represents an argument whose value is the next command-line argument /// Double } @@ -462,7 +462,7 @@ string IArgumentMetadataHelper.Parameter() string description = Name; if (!string.IsNullOrEmpty(ShortName)) { - description += string.Format(" ({0})", ShortName); + description += $" ({ShortName})"; } return description; } @@ -480,11 +480,11 @@ string IArgumentMetadataHelper.DescriptionBuilder(int width) if (!description.EndsWith(" ")) description += " "; } - description += string.Format("The argument is {0}. ", IsMandatory ? "mandatory" : "optional"); + description += $"The argument is {(IsMandatory ? "mandatory" : "optional")}. "; if (Default != null) { - description += string.Format("Default: {0}. ", Default); + description += $"Default: {Default}. "; } string range = null; if (IsEnum) @@ -504,7 +504,7 @@ string IArgumentMetadataHelper.DescriptionBuilder(int width) case ArgumentValueType.Range: if (MinValue != null && MaxValue != null) { - range = string.Format("[{0}...{1}]", MinValue, MaxValue); + range = $"[{MinValue}...{MaxValue}]"; } break; case ArgumentValueType.Free: @@ -515,20 +515,19 @@ string IArgumentMetadataHelper.DescriptionBuilder(int width) switch (Type) { - case ArgumentType.Single: - break; case ArgumentType.KeyValue: if (!string.IsNullOrEmpty(range)) { - description += string.Format("{0}{1}=<{2}>.", Helper.GetPrefix(), Name, range); + description += $"{Helper.GetPrefix()}{Name}=<{range}>."; } break; case ArgumentType.Double: if (!string.IsNullOrEmpty(range)) { - description += string.Format("{0}{1} <{2}>.", Helper.GetPrefix(), Name, range); + description += $"{Helper.GetPrefix()}{Name} <{range}>."; } break; + case ArgumentType.Single: default: break; } @@ -555,50 +554,48 @@ void IArgumentMetadataHelper.Check() var dType = Default.GetType(); if (dType.GetInterface(typeof(System.Collections.IEnumerable).Name) != null) { - var elemType = dType.GetElementType(); - if (elemType == null) throw new ArgumentException(string.Format("Default type shall be an instance of {0}<{1}>.", typeof(System.Collections.IEnumerable).Name, DataType)); - if (elemType != DataType) throw new ArgumentException(string.Format("Default type shall be equal to {0}.", DataType)); + var elemType = dType.GetElementType() ?? throw new ArgumentException($"Default type shall be an instance of {typeof(System.Collections.IEnumerable).Name}<{DataType}>."); + if (elemType != DataType) throw new ArgumentException($"Default type shall be equal to {DataType}."); } - else throw new ArgumentException(string.Format("Default type shall be an instance of {0}<{1}>.", typeof(System.Collections.IEnumerable).Name, DataType)); + else throw new ArgumentException($"Default type shall be an instance of {typeof(System.Collections.IEnumerable).Name}<{DataType}>."); } else { - if (Default.GetType() != DataType) throw new ArgumentException(string.Format("Default type shall be equal to {0}.", DataType)); + if (Default.GetType() != DataType) throw new ArgumentException($"Default type shall be equal to {DataType}."); } } switch (ValueType) { - case ArgumentValueType.Free: - break; case ArgumentValueType.Array: if (ArrayValues == null || ArrayValues.Length == 0) throw new ArgumentException("Argument needs to set ArrayValues."); foreach (var item in ArrayValues) { - if (item.GetType() != DataType) throw new ArgumentException(string.Format("ArrayValues type shall be equal to {0}.", DataType)); + if (item.GetType() != DataType) throw new ArgumentException($"ArrayValues type shall be equal to {DataType}."); } break; case ArgumentValueType.Range: if (MinValue == null && MaxValue == null) throw new ArgumentException("Argument needs to set both MinValue and MaxValue."); - if (!(DataType.IsValueType)) throw new ArgumentException(string.Format("DataType shall be a ValueType, found {0}.", DataType)); - if (MinValue.GetType() != DataType) throw new ArgumentException(string.Format("MinValue type shall be equal to {0}.", DataType)); - if (MaxValue.GetType() != DataType) throw new ArgumentException(string.Format("MaxValue type shall be equal to {0}.", DataType)); + if (!(DataType.IsValueType)) throw new ArgumentException($"DataType shall be a ValueType, found {DataType}."); + if (MinValue.GetType() != DataType) throw new ArgumentException($"MinValue type shall be equal to {DataType}."); + if (MaxValue.GetType() != DataType) throw new ArgumentException("MaxValue type shall be equal to {DataType}."); break; + case ArgumentValueType.Free: default: break; } if (Default != null) Helper.TestValue(Default); } - bool checkParam(string stringToTest) + bool CheckParam(string stringToTest) { - bool result = false; + bool result; switch (Type) { case ArgumentType.KeyValue: - result = stringToTest.StartsWith(Helper.StartWith) || (!string.IsNullOrEmpty(Helper.ShortStartWith) ? stringToTest.StartsWith(Helper.ShortStartWith) : false); + result = stringToTest.StartsWith(Helper.StartWith) || (!string.IsNullOrEmpty(Helper.ShortStartWith) && stringToTest.StartsWith(Helper.ShortStartWith)); break; default: - result = stringToTest == Helper.StartWith || (!string.IsNullOrEmpty(Helper.ShortStartWith) ? stringToTest == Helper.ShortStartWith : false); + result = stringToTest == Helper.StartWith || (!string.IsNullOrEmpty(Helper.ShortStartWith) && stringToTest == Helper.ShortStartWith); break; } return result; @@ -621,7 +618,7 @@ IArgumentMetadataParsed IArgumentMetadataHelper.Parse(IList args) args.RemoveAt(i); return parsedData; } - else if (checkParam(stringToTest)) + else if (CheckParam(stringToTest)) { ArgumentMetadataParsed parsedData = new ArgumentMetadataParsed(this) { @@ -638,7 +635,8 @@ IArgumentMetadataParsed IArgumentMetadataHelper.Parse(IList args) break; case ArgumentType.KeyValue: { - string value = args[i]; + _ = args[i]; + string value; if (stringToTest.StartsWith(Helper.StartWith)) { value = args[i].Substring(Helper.StartWith.Length); @@ -649,7 +647,7 @@ IArgumentMetadataParsed IArgumentMetadataHelper.Parse(IList args) } if (string.IsNullOrEmpty(value)) { - throw new ArgumentException(string.Format("Parameter {0} needs a value", Name)); + throw new ArgumentException($"Parameter {Name} needs a value"); } if (IsEnum) { @@ -660,7 +658,7 @@ IArgumentMetadataParsed IArgumentMetadataHelper.Parse(IList args) } catch { - throw new ArgumentException(string.Format("Argument {0} shall be in {1}: {2} was found.", Name, string.Join(", ", Enum.GetNames(DataType)), value)); + throw new ArgumentException($"Argument {Name} shall be in {string.Join(", ", Enum.GetNames(DataType))}: {value} was found."); } } else if (IsMultiValue) @@ -697,7 +695,7 @@ IArgumentMetadataParsed IArgumentMetadataHelper.Parse(IList args) } catch { - throw new ArgumentException(string.Format("Argument {0} shall be in {1}: {2} was found.", Name, string.Join(", ", Enum.GetNames(DataType)), value)); + throw new ArgumentException($"Argument {Name} shall be in {string.Join(", ", Enum.GetNames(DataType))}: {value} was found."); } } else if (IsMultiValue) @@ -720,7 +718,7 @@ IArgumentMetadataParsed IArgumentMetadataHelper.Parse(IList args) args.RemoveAt(i); args.RemoveAt(i); } - else throw new ArgumentException(string.Format("Parameter {0} needs a value", Name)); + else throw new ArgumentException($"Parameter {Name} needs a value"); } break; default: @@ -732,7 +730,7 @@ IArgumentMetadataParsed IArgumentMetadataHelper.Parse(IList args) else continue; } - if (IsMandatory) throw new ArgumentException(string.Format("Parameter {0} is mandatory", Name)); + if (IsMandatory) throw new ArgumentException($"Parameter {Name} is mandatory"); return new ArgumentMetadataParsed(this) { @@ -746,7 +744,7 @@ void IArgumentMetadataHelper.TestValue(object value) { if (!IsFlag && !Enum.IsDefined(DataType, value)) { - throw new ArgumentException(string.Format("Argument {0} shall be in {1}: {2} was found.", Name, string.Join(", ", Enum.GetNames(DataType)), value)); + throw new ArgumentException($"Argument {Name} shall be in {string.Join(", ", Enum.GetNames(DataType))}: {value} was found."); } } else @@ -762,7 +760,7 @@ void IArgumentMetadataHelper.TestValue(object value) } if (!found) { - throw new ArgumentException(string.Format("Argument {0} shall be in {1}, {2} was found.", Name, string.Join(", ", ArrayValues), value)); + throw new ArgumentException($"Argument {Name} shall be in {string.Join(", ", ArrayValues)}, {value} was found."); } } break; @@ -772,7 +770,7 @@ void IArgumentMetadataHelper.TestValue(object value) { break; } - else throw new ArgumentException(string.Format("Argument {0} shall be in {1} - {2}, {3} was found.", Name, MinValue, MaxValue, value)); + else throw new ArgumentException($"Argument {Name} shall be in {MinValue} - {MaxValue}, {value} was found."); } case ArgumentValueType.Free: default: diff --git a/src/net/CLIParser/CLIParser.cs b/src/net/CLIParser/CLIParser.cs index 0520fb5..a966c13 100644 --- a/src/net/CLIParser/CLIParser.cs +++ b/src/net/CLIParser/CLIParser.cs @@ -24,6 +24,7 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Text; namespace MASES.CLIParser @@ -98,16 +99,9 @@ public static string ToString(this IArgumentMetadata metadata, params object[] v switch (metadata.Type) { - case ArgumentType.Single: - return $"{prefix}{metadata.Name}"; - case ArgumentType.Double: - { - return $"{prefix}{metadata.Name} {valueStr}"; - } - case ArgumentType.KeyValue: - { - return $"{prefix}{metadata.Name}{metadata.KeyValuePairSeparator}{valueStr}"; - } + case ArgumentType.Single: return $"{prefix}{metadata.Name}"; + case ArgumentType.Double: return $"{prefix}{metadata.Name} {valueStr}"; + case ArgumentType.KeyValue: return $"{prefix}{metadata.Name}{metadata.KeyValuePairSeparator}{valueStr}"; default: throw new ArgumentException($"Parameter {metadata.Name} does not have a correct Type: {metadata.Type}"); } } @@ -119,7 +113,7 @@ public static string ToString(this IArgumentMetadata metadata, params object[] v public static void Add(this IArgumentMetadata metadata) { IArgumentMetadataHelper helper = metadata as IArgumentMetadataHelper; - if (helper.Parser == null) throw new ArgumentException(string.Format("Parameter {0} does not have any associated Parser", metadata.Name)); + if (helper.Parser == null) throw new ArgumentException($"Parameter {metadata.Name} does not have any associated Parser"); helper.Parser.Add(metadata); } /// @@ -219,7 +213,7 @@ public static T Get(this IEnumerable args, int index public static T Get(this IArgumentMetadataParsed arg) { if (arg == null) throw new ArgumentNullException("arg cannot be null."); - if (!typeof(T).IsAssignableFrom(arg.DataType)) throw new ArgumentException(string.Format("{0} is incomplatible wirh {1}.", typeof(T), arg.DataType)); + if (!typeof(T).IsAssignableFrom(arg.DataType)) throw new ArgumentException($"{typeof(T)} is incomplatible wirh {arg.DataType}."); if (arg.Value != null) { return (T)arg.Value; @@ -257,7 +251,7 @@ public static IEnumerable Override(this IEnumerable Parse(string[] args) { IArgumentMetadataParsed dataParsed = item.Parse(lstArgs); - if (parsedArgs.ContainsKey(dataParsed.Name)) throw new ArgumentException(string.Format("Parameter {0} is duplicated", dataParsed.Name)); + if (parsedArgs.ContainsKey(dataParsed.Name)) throw new ArgumentException($"Parameter {dataParsed.Name} is duplicated"); if (dataParsed != null) { @@ -490,15 +484,12 @@ public IEnumerable Parse(string[] args) if (Settings.CheckUnwanted && lstArgs.Count != 0) { - throw new ArgumentException(string.Format("Parameter{0} {1} are not managed", lstArgs.Count == 1 ? string.Empty : "s", string.Join(", ", lstArgs))); + throw new ArgumentException($"Parameter {(lstArgs.Count == 1 ? string.Empty : "s")} {string.Join(", ", lstArgs)} are not managed"); } foreach (var item in parsedArgs.Values) { - if (item.CrossCheck != null) - { - item.CrossCheck(parsedArgs.Values); - } + item.CrossCheck?.Invoke(parsedArgs.Values); } UnparsedArgs = new List(lstArgs).ToArray(); @@ -519,7 +510,7 @@ public IEnumerable Parse(IArgumentMetadataParsed arg) IList lstArgs = new List(arg.Value as IEnumerable); - foreach (IArgumentMetadataHelper item in Arguments) + foreach (IArgumentMetadataHelper item in Arguments.Cast()) { IArgumentMetadataParsed dataParsed = item.Parse(lstArgs); @@ -675,7 +666,7 @@ public IEnumerable NotExists(IEnumerable()) { len = Math.Max(len, item.Parameter().Length); } @@ -697,7 +688,7 @@ public string HelpInfo(int? width = null) catch { } if (width.HasValue) newWidth = width.Value; StringBuilder builder = new StringBuilder(); - foreach (IArgumentMetadataHelper item in Arguments) + foreach (IArgumentMetadataHelper item in Arguments.Cast()) { builder.AppendLine(item.DescriptionBuilder(newWidth)); } diff --git a/src/net/CLIParser/CLIParser.csproj b/src/net/CLIParser/CLIParser.csproj index d0366e6..60fa47c 100644 --- a/src/net/CLIParser/CLIParser.csproj +++ b/src/net/CLIParser/CLIParser.csproj @@ -8,7 +8,7 @@ MASES s.r.l. MASES s.r.l. MASES s.r.l. - 3.1.2.0 + 3.2.0.0 CLIParser true netstandard2.0 diff --git a/tests/CLIParserTest/CLIParserTest.csproj b/tests/CLIParserTest/CLIParserTest.csproj index 15a2562..fd4ec31 100644 --- a/tests/CLIParserTest/CLIParserTest.csproj +++ b/tests/CLIParserTest/CLIParserTest.csproj @@ -9,7 +9,7 @@ Copyright © MASES s.r.l. 2023 MASES s.r.l. MASES s.r.l. - 3.1.2.0 + 3.2.0.0 net462;net6.0;net7.0 ..\..\bin\