Skip to content

Commit

Permalink
Single and Double parameters now use an exact match rather than a sta…
Browse files Browse the repository at this point in the history
…rt with approach (#9)

* #8: Single and Double parameters now use an exact match rather than a start with approach, KeyValue preserve startwith due to KeyValuePairSeparator character

* Version update

* Documentation update
  • Loading branch information
mariomastrodicasa authored Oct 8, 2021
1 parent 449c659 commit 53d424c
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 8 deletions.
17 changes: 16 additions & 1 deletion src/CLIParser/ArgumentMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,21 @@ void IArgumentMetadataHelper.Check()
if (Default != null) Helper.TestValue(Default);
}

bool checkParam(string stringToTest)
{
bool result = false;
switch (Type)
{
case ArgumentType.KeyValue:
result = stringToTest.StartsWith(Helper.StartWith) || (!string.IsNullOrEmpty(Helper.ShortStartWith) ? stringToTest.StartsWith(Helper.ShortStartWith) : false);
break;
default:
result = stringToTest == Helper.StartWith || (!string.IsNullOrEmpty(Helper.ShortStartWith) ? stringToTest == Helper.ShortStartWith : false);
break;
}
return result;
}

IArgumentMetadataParsed IArgumentMetadataHelper.Parse(IList<string> args)
{
for (int i = 0; i < args.Count; i++)
Expand All @@ -606,7 +621,7 @@ IArgumentMetadataParsed IArgumentMetadataHelper.Parse(IList<string> args)
args.RemoveAt(i);
return parsedData;
}
else if (stringToTest.StartsWith(Helper.StartWith) || (!string.IsNullOrEmpty(Helper.ShortStartWith) ? stringToTest.StartsWith(Helper.ShortStartWith) : false))
else if (checkParam(stringToTest))
{
ArgumentMetadataParsed parsedData = new ArgumentMetadataParsed(this)
{
Expand Down
8 changes: 4 additions & 4 deletions src/CLIParser/CLIParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public static IEnumerable<IArgumentMetadataParsed> Override(this IEnumerable<IAr
/// <summary>
/// Filter the <paramref name="args"/> for existing <see cref="IArgumentMetadataParsed"/>
/// </summary>
/// <param name="args">Arguments to test using the list prepared using <see cref="Parse(string[])"/></param>
/// <param name="args">Arguments to test using the list prepared using <see cref="Parse"/></param>
/// <returns>A filtered list of <see cref="IArgumentMetadataParsed"/></returns>
public static IEnumerable<IArgumentMetadataParsed> Exists(this IEnumerable<IArgumentMetadataParsed> args)
{
Expand All @@ -186,7 +186,7 @@ public static IEnumerable<IArgumentMetadataParsed> Exists(this IEnumerable<IArgu
/// <summary>
/// Check the <paramref name="args"/> for existing <paramref name="name"/>
/// </summary>
/// <param name="args">Arguments to test using the list prepared using <see cref="Parse(string[])"/></param>
/// <param name="args">Arguments to test using the list prepared using <see cref="Parse"/></param>
/// <param name="name">Argument name to search</param>
/// <returns>true if the the argument with <paramref name="name"/> name exist</returns>
public static bool Exist(this IEnumerable<IArgumentMetadataParsed> args, string name)
Expand All @@ -202,7 +202,7 @@ public static bool Exist(this IEnumerable<IArgumentMetadataParsed> args, string
/// <summary>
/// Filter the <paramref name="args"/> for non existing <see cref="IArgumentMetadataParsed"/>
/// </summary>
/// <param name="args">Arguments to test using the list prepared using <see cref="Parse(string[])"/></param>
/// <param name="args">Arguments to test using the list prepared using <see cref="Parse"/></param>
/// <returns>A filtered list of <see cref="IArgumentMetadataParsed"/></returns>
public static IEnumerable<IArgumentMetadataParsed> NotExists(this IEnumerable<IArgumentMetadataParsed> args)
{
Expand Down Expand Up @@ -561,7 +561,7 @@ public int PaddingFromArguments()
/// Returns the help information
/// </summary>
/// <param name="width">The width of the help to write</param>
/// <returns>A <see cref="string with help information"/></returns>
/// <returns>A <see cref="string"/> with help information</returns>
public string HelpInfo(int? width = null)
{
int newWidth = Console.WindowWidth;
Expand Down
2 changes: 1 addition & 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.1.0</Version>
<Version>2.1.2.0</Version>
<Product>CLIParser</Product>
<GenerateAssemblyInfo>true</GenerateAssemblyInfo>
<TargetFrameworks>net461;netcoreapp3.1;net5.0;net5.0-windows</TargetFrameworks>
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.1.0</Version>
<Version>2.1.2.0</Version>
<TargetFrameworks>net461;netcoreapp3.1;net5.0;net5.0-windows</TargetFrameworks>
<OutputPath>..\..\bin\</OutputPath>
</PropertyGroup>
Expand Down
10 changes: 10 additions & 0 deletions tests/CLIParserTest/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,16 @@ static void Main(string[] args)
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);

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": "-enum \"Second,Third\" -range 9 -multivalue \"qui:quo:qua\" -tst true -tst false @MyFile ???"
"commandLineArgs": "-MyParam2 pippo -enum \"Second,Third\" -range 9 -multivalue \"qui:quo:qua\" -tst true -tst false @MyFile ???"
}
}
}

0 comments on commit 53d424c

Please sign in to comment.