Skip to content

Commit

Permalink
masesgroup#2: added management of arguments cross check
Browse files Browse the repository at this point in the history
  • Loading branch information
mariomastrodicasa committed Oct 28, 2021
1 parent c7bb100 commit 5df5bb2
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 10 deletions.
25 changes: 16 additions & 9 deletions src/CLIParser/ArgumentMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,23 @@ namespace MASES.CLIParser
public enum ArgumentPrefix
{
/// <summary>
/// Represent an argument without a prefix
/// Represents an argument without a prefix
/// </summary>
None = 0x0,
/// <summary>
/// Represent a "-"
/// Represents a "-"
/// </summary>
Dash = 0x1,
/// <summary>
/// Represent a "--"
/// Represents a "--"
/// </summary>
DoubleDash = 0x2,
/// <summary>
/// Represent a "/"
/// Represents a "/"
/// </summary>
Slash = 0x4,
/// <summary>
/// Represent a custom prefix
/// Represents a custom prefix
/// </summary>
Custom = 0x8,
}
Expand All @@ -69,7 +69,7 @@ public enum ArgumentType
/// </summary>
KeyValue,
/// <summary>
/// Reperesent an argument whose value if the next argument
/// Represents an argument whose value if the next argument
/// </summary>
Double
}
Expand All @@ -83,11 +83,11 @@ public enum ArgumentValueType
/// </summary>
Free,
/// <summary>
/// Represent a possible value into an array
/// Represents a possible value into an array
/// </summary>
Array,
/// <summary>
/// Represent a range of values
/// Represents a range of values
/// </summary>
Range
}
Expand Down Expand Up @@ -169,10 +169,14 @@ public interface IArgumentMetadata
/// </summary>
object MinValue { get; set; }
/// <summary>
/// The ,aximum value for the argument if <see cref="ValueType"/> is <see cref="ArgumentValueType.Range"/>
/// The maximum value for the argument if <see cref="ValueType"/> is <see cref="ArgumentValueType.Range"/>
/// </summary>
object MaxValue { get; set; }
/// <summary>
/// An <see cref="Action"/> to cross check multiple values. If something is not correct during check an <see cref="ArgumentException"/> shall be raised.
/// </summary>
Action<IEnumerable<IArgumentMetadataParsed>> CrossCheck { get; set; }
/// <summary>
/// The <see cref="Type"/> of the parameter.
/// </summary>
Type DataType { get; }
Expand Down Expand Up @@ -291,6 +295,8 @@ internal ArgumentMetadataBase()
/// <inheritdoc/>
public virtual object MaxValue { get; set; }
/// <inheritdoc/>
public virtual Action<IEnumerable<IArgumentMetadataParsed>> CrossCheck { get; set; }
/// <inheritdoc/>
public virtual Type DataType { get; protected set; }
}

Expand All @@ -316,6 +322,7 @@ public ArgumentMetadataParsed(IArgumentMetadata reference)
ArrayValues = reference.ArrayValues;
MinValue = reference.MinValue;
MaxValue = reference.MaxValue;
CrossCheck = reference.CrossCheck;
DataType = reference.DataType;
}

Expand Down
8 changes: 8 additions & 0 deletions src/CLIParser/CLIParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,14 @@ public IEnumerable<IArgumentMetadataParsed> 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<string>(lstArgs).ToArray();

return parsedArgs.Values;
Expand Down
7 changes: 7 additions & 0 deletions tests/CLIParserTest/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

using MASES.CLIParser;
using System;
using System.Collections.Generic;

namespace MASES.CLIParserTest
{
Expand All @@ -37,6 +38,11 @@ public enum MyValues
Third = 0x4
};

static void crossCheckExample(IEnumerable<IArgumentMetadataParsed> args)
{
if (!args.Exist("range")) throw new ArgumentException("range is mandatory for test.");
}

static void Main(string[] args)
{
Parser parser = Parser.CreateInstance(new Settings()
Expand All @@ -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<int>(parser)
Expand Down
2 changes: 1 addition & 1 deletion tests/CLIParserTest/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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 ???"
}
}
}

0 comments on commit 5df5bb2

Please sign in to comment.