Skip to content

Commit 61c6ddc

Browse files
bug fix named params
1 parent b340602 commit 61c6ddc

File tree

5 files changed

+95
-25
lines changed

5 files changed

+95
-25
lines changed

UltraMapper.CommandLine.UnitTest/RealworldBugs.cs

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
using System.Collections.Generic;
23

34
namespace UltraMapper.CommandLine.UnitTest
45
{
@@ -48,10 +49,67 @@ public void CircularReference()
4849

4950
[TestMethod]
5051
[Ignore] //this should throw and it's ok, but it's not easy to understand where the problem is
51-
public void ComplexAnonymousParamOnSimpleParam()
52+
public void ComplexAnonymousParamOnSimpleParamOrderOvverride()
5253
{
54+
//order is set overridden manually so the order should be:
55+
//a=aa b=bb (a=aaa b=bbb)
56+
//when checking for order, also check for param type???
57+
5358
var args = "--move (a=aaa b=bbb) a=aa b=bb";
5459
var parsed = CommandLine.Instance.Parse<Commands2>( args );
5560
}
61+
62+
public class CustomerInfo
63+
{
64+
public class BankAccountInfo
65+
{
66+
public class CreditCardInfo
67+
{
68+
public string CardNumber { get; set; }
69+
public double MonthlyLimit { get; set; }
70+
}
71+
72+
[Option( Name = "number" )]
73+
public string AccountNumber { get; set; }
74+
public double Balance { get; set; }
75+
76+
public List<CreditCardInfo> CreditCards { get; set; }
77+
}
78+
79+
public string Name { get; set; }
80+
public int Age { get; set; }
81+
82+
public BankAccountInfo Account { get; set; }
83+
}
84+
85+
[TestMethod]
86+
public void GitHubExample()
87+
{
88+
var args = "--add (\"John Smith\" 26 account=(number=AC2903X balance=3500.00 creditcards=[(CRD01 1000.00) (CRD02 2000.00)]))";
89+
var parsed = CommandLine.Instance.Parse<CustomerCommands>( args );
90+
}
91+
92+
[TestMethod]
93+
public void GitHubExample2()
94+
{
95+
var args = "--add (\"John Smith\" 26 (number=AC2903X balance=3500.00 creditcards=[(CRD01 1000.00) (CRD02 2000.00)]))";
96+
var parsed = CommandLine.Instance.Parse<CustomerCommands>( args );
97+
}
98+
99+
public class CustomerCommands
100+
{
101+
[Option( Name = "add", HelpText = "Adds new customer to db" )]
102+
public void AddToDatabase( CustomerInfo customer )
103+
{
104+
Assert.IsTrue( customer.Name == "John Smith" );
105+
Assert.IsTrue( customer.Age == 26 );
106+
Assert.IsTrue( customer.Account.AccountNumber == "AC2903X" );
107+
Assert.IsTrue( customer.Account.Balance == 3500 );
108+
Assert.IsTrue( customer.Account.CreditCards[ 0 ].CardNumber == "CRD01" );
109+
Assert.IsTrue( customer.Account.CreditCards[ 0 ].MonthlyLimit == 1000 );
110+
Assert.IsTrue( customer.Account.CreditCards[ 1 ].CardNumber == "CRD02" );
111+
Assert.IsTrue( customer.Account.CreditCards[ 1 ].MonthlyLimit == 2000 );
112+
}
113+
}
56114
}
57115
}

UltraMapper.CommandLine.UnitTest/UltraMapper.CommandLine.Tests.csproj

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,17 @@
88
<Copyright>2021</Copyright>
99
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
1010
<langversion>latest</langversion>
11+
<Platforms>AnyCpu</Platforms>
1112
</PropertyGroup>
1213

1314
<ItemGroup>
1415
<Compile Remove="ParsedParamToObject.cs" />
1516
</ItemGroup>
1617

1718
<ItemGroup>
18-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
19-
<PackageReference Include="MSTest.TestAdapter" Version="2.2.3" />
20-
<PackageReference Include="MSTest.TestFramework" Version="2.2.3" />
19+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
20+
<PackageReference Include="MSTest.TestAdapter" Version="2.2.8" />
21+
<PackageReference Include="MSTest.TestFramework" Version="2.2.8" />
2122
</ItemGroup>
2223

2324
<ItemGroup>

UltraMapper.CommandLine/ParsedParametersAdapter.cs

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ public void Checks( ParsedCommand param, Type target, ParameterDefinition[] para
245245
CheckThrowParamNumber( param, paramsDef );
246246
CheckThrowNamedParamsExist( param, target, paramsDef );
247247
CheckThrowNamedParamsOrder( param );
248+
//CheckThrowParamsTypeOrder (if cp provided and sp is expected number is still ok but should throw)
248249
}
249250

250251
public void CheckThrowCommandExists( ParsedCommand command, Type target, ParameterDefinition[] commandDefs )
@@ -284,27 +285,35 @@ protected void CheckThrowNamedParamsExist( ParsedCommand command, Type target, P
284285
var availableParamNames = longNames
285286
.Where( i => !String.IsNullOrWhiteSpace( i ) );
286287

287-
if( command.Param is ComplexParam cp )
288-
{
289-
var providedParams = cp.SubParams.Where( l => !String.IsNullOrWhiteSpace( l.Name ) )
290-
.Select( p => p.Name.ToLower() );
291-
292-
foreach( var providedParam in providedParams )
293-
{
294-
var isCorrectParam = availableParamNames.Contains( providedParam );
295-
if( !isCorrectParam )
296-
throw new UndefinedParameterException( target, providedParam );
297-
}
298-
}
299-
else if( command.Param != null )
288+
if( !String.IsNullOrEmpty( command.Param?.Name ) )
300289
{
301-
if( !String.IsNullOrWhiteSpace( command.Param.Name ) )
302-
{
303-
var isCorrectParam = availableParamNames.Contains( command.Param.Name.ToLower() );
304-
if( !isCorrectParam )
305-
throw new UndefinedParameterException( target, command.Param.Name );
306-
}
290+
var isCorrectParam = availableParamNames.Contains( command.Param.Name );
291+
if( !isCorrectParam )
292+
throw new UndefinedParameterException( target, command.Param.Name );
307293
}
294+
295+
//if( command.Param is ComplexParam cp )
296+
//{
297+
// var providedParams = cp.SubParams.Where( l => !String.IsNullOrWhiteSpace( l.Name ) )
298+
// .Select( p => p.Name.ToLower() );
299+
300+
// foreach( var providedParam in providedParams )
301+
// {
302+
// var isCorrectParam = availableParamNames.Contains( providedParam );
303+
// if( !isCorrectParam )
304+
// throw new UndefinedParameterException( target, providedParam );
305+
// }
306+
//}
307+
//else
308+
//if( command.Param != null )
309+
//{
310+
// if( !String.IsNullOrWhiteSpace( command.Param.Name ) )
311+
// {
312+
// var isCorrectParam = availableParamNames.Contains( command.Param.Name.ToLower() );
313+
// if( !isCorrectParam )
314+
// throw new UndefinedParameterException( target, command.Param.Name );
315+
// }
316+
//}
308317
}
309318

310319
protected void CheckThrowNamedParamsOrder( ParsedCommand command )
@@ -377,6 +386,7 @@ public void Checks( ComplexParam param, Type target, ParameterDefinition[] param
377386
CheckThrowParamNumber( param, paramsDef );
378387
CheckThrowNamedParamsExist( param, target, paramsDef );
379388
CheckThrowNamedParamsOrder( param );
389+
//CheckThrowParamsTypeOrder (if cp provided and sp is expected number is still ok but should throw)
380390
}
381391

382392
private void CheckThrowArgumentNameCollisions( ComplexParam param )

UltraMapper.CommandLine/Parsers/DefaultParser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ public IEnumerable<ParsedCommand> Parse( string commandLine )
167167
{
168168
theparam = new ComplexParam()
169169
{
170-
Name = cmdName,
170+
//Name = cmdName,
171171
Index = paramIndex,
172172
SubParams = parameters.ToArray()
173173
};

UltraMapper.CommandLine/UltraMapper.CommandLine.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>netstandard2.0;net45;net46;net47;net5.0</TargetFrameworks>
4+
<TargetFrameworks>netstandard2.0;net452;net46;net47;net5.0</TargetFrameworks>
55
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
66
<Authors>Mauro Sampietro</Authors>
77
<Copyright>2020</Copyright>
@@ -16,6 +16,7 @@
1616
<Description>A commandline parser supporting direct method calls taking as input an unlimited number of primitive and complex-type parameters</Description>
1717
<UserSecretsId>f04337b3-a5f3-4f7d-bb7b-7d4c5824a530</UserSecretsId>
1818
<AssemblyVersion>1.0.0.0</AssemblyVersion>
19+
<Platforms>AnyCPU</Platforms>
1920
</PropertyGroup>
2021

2122
<ItemGroup>

0 commit comments

Comments
 (0)