Skip to content

Commit

Permalink
Implemented #2 and #10 (#11)
Browse files Browse the repository at this point in the history
* Update nuget package generation with fix available on dotnet/sourcelink#572

* #10: added UnparsedArgs property

* #2: added management of arguments cross check

* Reverted back launchSettings.json after test of #2

* Capture all exceptions until tests are managed from workflow (#2 tests)
  • Loading branch information
mariomastrodicasa authored Oct 28, 2021
1 parent fb1e971 commit e2caf35
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 77 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
14 changes: 14 additions & 0 deletions src/CLIParser/CLIParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,10 @@ public static Parser CreateInstance(Settings settings = null)
/// </summary>
public Settings Settings { get; private set; }
/// <summary>
/// The arguments in command-line not parsed, i.e. the possible extra arguments to be used from the application for other scopes
/// </summary>
public string[] UnparsedArgs { get; private set; }
/// <summary>
/// Available <see cref="IArgumentMetadata"/> for parsing
/// </summary>
public IReadOnlyList<IArgumentMetadata> Arguments { get { return new List<IArgumentMetadata>(arguments.Values); } }
Expand Down Expand Up @@ -378,6 +382,16 @@ 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
10 changes: 9 additions & 1 deletion src/CLIParser/CLIParser.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<Owners>MASES s.r.l.</Owners>
<Authors>MASES s.r.l.</Authors>
<Company>MASES s.r.l.</Company>
<Version>2.1.2.0</Version>
<Version>2.2.0.0</Version>
<Product>CLIParser</Product>
<GenerateAssemblyInfo>true</GenerateAssemblyInfo>
<TargetFrameworks>net461;netcoreapp3.1;net5.0;net5.0-windows</TargetFrameworks>
Expand All @@ -28,6 +28,14 @@
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>..\Common\CLIParser.snk</AssemblyOriginatorKeyFile>
</PropertyGroup>
<!-- Fix start https://github.com/dotnet/sourcelink/issues/572 -->
<PropertyGroup>
<TargetFrameworkMonikerAssemblyAttributesPath>$([System.IO.Path]::Combine('$(IntermediateOutputPath)','$(TargetFrameworkMoniker).AssemblyAttributes$(DefaultLanguageSourceExtension)'))</TargetFrameworkMonikerAssemblyAttributesPath>
</PropertyGroup>
<ItemGroup>
<EmbeddedFiles Include="$(GeneratedAssemblyInfoFile)"/>
</ItemGroup>
<!-- Fix end -->
<PropertyGroup Condition="'$(GITHUB_ACTIONS)' == 'true'">
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
</PropertyGroup>
Expand Down
2 changes: 1 addition & 1 deletion tests/CLIParserTest/CLIParserTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<Copyright>Copyright © MASES s.r.l. 2021</Copyright>
<Authors>MASES s.r.l.</Authors>
<Company>MASES s.r.l.</Company>
<Version>2.1.2.0</Version>
<Version>2.2.0.0</Version>
<TargetFrameworks>net461;netcoreapp3.1;net5.0;net5.0-windows</TargetFrameworks>
<OutputPath>..\..\bin\</OutputPath>
</PropertyGroup>
Expand Down
146 changes: 80 additions & 66 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,88 +38,101 @@ 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()
try
{
CheckUnwanted = false
});
Parser parser = Parser.CreateInstance(new Settings()
{
CheckUnwanted = false
});

new ArgumentMetadata<MyValues>(parser)
{
Name = "enum",
Default = MyValues.First,
Help = "this is an enum test",
Type = ArgumentType.Double,
}.Add();
parser.Add(new ArgumentMetadata<bool>()
{
Name = "test",
ShortName = "tst",
Help = "this is a test",
Type = ArgumentType.Double,
ValueType = ArgumentValueType.Free,
});
parser.Add(new ArgumentMetadata<int>(parser)
{
Name = "range",
Default = 9,
Type = ArgumentType.Double,
ValueType = ArgumentValueType.Range,
MinValue = 2,
MaxValue = 10,
});
parser.Add(new ArgumentMetadata<string>(parser)
{
Name = "multivalue",
Type = ArgumentType.Double,
IsMultiValue = true,
Default = new string[] { "a", "b" }
});
parser.Add(new ArgumentMetadata<string>(parser)
{
Name = "myval",
Type = ArgumentType.Single,
});
parser.Add(new ArgumentMetadata<string>(parser)
{
Name = "MyParam",
Type = ArgumentType.Double,
});
parser.Add(new ArgumentMetadata<string>(parser)
{
Name = "MyParam2",
Type = ArgumentType.Double,
});
new ArgumentMetadata<MyValues>(parser)
{
Name = "enum",
Default = MyValues.First,
Help = "this is an enum test",
Type = ArgumentType.Double,
}.Add();
parser.Add(new ArgumentMetadata<bool>()
{
Name = "test",
ShortName = "tst",
Help = "this is a test",
Type = ArgumentType.Double,
CrossCheck = crossCheckExample,
ValueType = ArgumentValueType.Free,
});
parser.Add(new ArgumentMetadata<int>(parser)
{
Name = "range",
Default = 9,
Type = ArgumentType.Double,
ValueType = ArgumentValueType.Range,
MinValue = 2,
MaxValue = 10,
});
parser.Add(new ArgumentMetadata<string>(parser)
{
Name = "multivalue",
Type = ArgumentType.Double,
IsMultiValue = true,
Default = new string[] { "a", "b" }
});
parser.Add(new ArgumentMetadata<string>(parser)
{
Name = "myval",
Type = ArgumentType.Single,
});
parser.Add(new ArgumentMetadata<string>(parser)
{
Name = "MyParam",
Type = ArgumentType.Double,
});
parser.Add(new ArgumentMetadata<string>(parser)
{
Name = "MyParam2",
Type = ArgumentType.Double,
});

var result = parser.Parse(args);
var result = parser.Parse(args);

var fileInfo = parser.FromFile(result);
var fileInfo = parser.FromFile(result);

parser.Override(result, fileInfo);
parser.Override(result, fileInfo);

var noFile = parser.RemoveFile(result);
var noFile = parser.RemoveFile(result);

foreach (var item in parser.Exists(noFile))
{
if (item.Name == "enum")
foreach (var item in parser.Exists(noFile))
{
Console.WriteLine("Testing method extension: {0} is {1}", item.Name, item.Get<MyValues>());
}
if (item.Name == "enum")
{
Console.WriteLine("Testing method extension: {0} is {1}", item.Name, item.Get<MyValues>());
}

if (!item.IsMultiValue)
{
Console.WriteLine("{0} is {1}", item.Name, item.Value);
if (!item.IsMultiValue)
{
Console.WriteLine("{0} is {1}", item.Name, item.Value);
}
else
{
Console.WriteLine("{0} is {1}", item.Name, string.Join(", ", (object[])item.Value));
}
}
else

foreach (var item in parser.NotExists(noFile))
{
Console.WriteLine("{0} is {1}", item.Name, string.Join(", ", (object[])item.Value));
Console.WriteLine("{0} not exist", item.Name);
}
}

foreach (var item in parser.NotExists(noFile))
catch (Exception e)
{
Console.WriteLine("{0} not exist", item.Name);
Console.WriteLine(e.Message);
}
}
}
Expand Down

0 comments on commit e2caf35

Please sign in to comment.