From 5df5bb2aba4a84e3efc976792813b9e11c5f740f Mon Sep 17 00:00:00 2001 From: Mario Mastrodicasa Date: Thu, 28 Oct 2021 03:13:14 +0200 Subject: [PATCH] #2: added management of arguments cross check --- src/CLIParser/ArgumentMetadata.cs | 25 ++++++++++++------- src/CLIParser/CLIParser.cs | 8 ++++++ tests/CLIParserTest/Program.cs | 7 ++++++ .../Properties/launchSettings.json | 2 +- 4 files changed, 32 insertions(+), 10 deletions(-) diff --git a/src/CLIParser/ArgumentMetadata.cs b/src/CLIParser/ArgumentMetadata.cs index 1cfac07..0e9bbe7 100644 --- a/src/CLIParser/ArgumentMetadata.cs +++ b/src/CLIParser/ArgumentMetadata.cs @@ -35,23 +35,23 @@ namespace MASES.CLIParser public enum ArgumentPrefix { /// - /// Represent an argument without a prefix + /// Represents an argument without a prefix /// None = 0x0, /// - /// Represent a "-" + /// Represents a "-" /// Dash = 0x1, /// - /// Represent a "--" + /// Represents a "--" /// DoubleDash = 0x2, /// - /// Represent a "/" + /// Represents a "/" /// Slash = 0x4, /// - /// Represent a custom prefix + /// Represents a custom prefix /// Custom = 0x8, } @@ -69,7 +69,7 @@ public enum ArgumentType /// KeyValue, /// - /// Reperesent an argument whose value if the next argument + /// Represents an argument whose value if the next argument /// Double } @@ -83,11 +83,11 @@ public enum ArgumentValueType /// Free, /// - /// Represent a possible value into an array + /// Represents a possible value into an array /// Array, /// - /// Represent a range of values + /// Represents a range of values /// Range } @@ -169,10 +169,14 @@ public interface IArgumentMetadata /// object MinValue { get; set; } /// - /// The ,aximum value for the argument if is + /// The maximum value for the argument if is /// object MaxValue { get; set; } /// + /// An to cross check multiple values. If something is not correct during check an shall be raised. + /// + Action> CrossCheck { get; set; } + /// /// The of the parameter. /// Type DataType { get; } @@ -291,6 +295,8 @@ internal ArgumentMetadataBase() /// public virtual object MaxValue { get; set; } /// + public virtual Action> CrossCheck { get; set; } + /// public virtual Type DataType { get; protected set; } } @@ -316,6 +322,7 @@ public ArgumentMetadataParsed(IArgumentMetadata reference) ArrayValues = reference.ArrayValues; MinValue = reference.MinValue; MaxValue = reference.MaxValue; + CrossCheck = reference.CrossCheck; DataType = reference.DataType; } diff --git a/src/CLIParser/CLIParser.cs b/src/CLIParser/CLIParser.cs index 69089b7..c3a91f6 100644 --- a/src/CLIParser/CLIParser.cs +++ b/src/CLIParser/CLIParser.cs @@ -382,6 +382,14 @@ public IEnumerable Parse(string[] args) throw new ArgumentException(string.Format("Parameter{0} {1} are not managed", lstArgs.Count == 1 ? string.Empty : "s", string.Join(", ", lstArgs))); } + foreach (var item in parsedArgs.Values) + { + if (item.CrossCheck != null) + { + item.CrossCheck(parsedArgs.Values); + } + } + UnparsedArgs = new List(lstArgs).ToArray(); return parsedArgs.Values; diff --git a/tests/CLIParserTest/Program.cs b/tests/CLIParserTest/Program.cs index 9539bd3..eb2e6ea 100644 --- a/tests/CLIParserTest/Program.cs +++ b/tests/CLIParserTest/Program.cs @@ -24,6 +24,7 @@ using MASES.CLIParser; using System; +using System.Collections.Generic; namespace MASES.CLIParserTest { @@ -37,6 +38,11 @@ public enum MyValues Third = 0x4 }; + static void crossCheckExample(IEnumerable args) + { + if (!args.Exist("range")) throw new ArgumentException("range is mandatory for test."); + } + static void Main(string[] args) { Parser parser = Parser.CreateInstance(new Settings() @@ -57,6 +63,7 @@ static void Main(string[] args) ShortName = "tst", Help = "this is a test", Type = ArgumentType.Double, + CrossCheck = crossCheckExample, ValueType = ArgumentValueType.Free, }); parser.Add(new ArgumentMetadata(parser) diff --git a/tests/CLIParserTest/Properties/launchSettings.json b/tests/CLIParserTest/Properties/launchSettings.json index 47a615c..85d0ca2 100644 --- a/tests/CLIParserTest/Properties/launchSettings.json +++ b/tests/CLIParserTest/Properties/launchSettings.json @@ -2,7 +2,7 @@ "profiles": { "CLIParserTest": { "commandName": "Project", - "commandLineArgs": "-MyParam2 pippo -enum \"Second,Third\" -range 9 -multivalue \"qui:quo:qua\" -tst true -tst false @MyFile ???" + "commandLineArgs": "-MyParam2 pippo -enum \"Second,Third\" -multivalue \"qui:quo:qua\" -tst true -tst false @MyFile ???" } } } \ No newline at end of file