Skip to content

Commit 749ae7c

Browse files
committed
added ability to set command line keys when binding properties to command line arguments.
1 parent fab3d92 commit 749ae7c

File tree

2 files changed

+25
-13
lines changed

2 files changed

+25
-13
lines changed

Binder.Tests/support/BaseTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ protected void Initialize( TestConfig testConfig )
3636
Allocator = allocator;
3737
}
3838

39-
protected void Bind<TTarget, TProp>( Expression<Func<TTarget, TProp>> propSelector, bool bindNonPublic = false )
39+
protected void Bind<TTarget, TProp>( Expression<Func<TTarget, TProp>> propSelector )
4040
where TTarget : class, new()
4141
{
42-
Options!.Bind( propSelector, out var option, bindNonPublic )
42+
Options!.Bind( propSelector, out var option )
4343
.Should()
4444
.BeTrue();
4545

Binder/OptionCollection.cs

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public IOption Add(string contextPath)
8383
public bool Bind<TTarget, TProp>(
8484
Expression<Func<TTarget, TProp>> propertySelector,
8585
out Option? result,
86-
bool bindNonPublic = false)
86+
params string[] cmdLineKeys )
8787
where TTarget : class, new()
8888
{
8989
result = null;
@@ -101,7 +101,7 @@ public bool Bind<TTarget, TProp>(
101101
case MemberExpression memExpr:
102102
var propInfo = (PropertyInfo) memExpr.Member;
103103

104-
if( !ValidateProperty( propInfo, bindNonPublic, out var curStyle ) )
104+
if( !ValidateProperty( propInfo, out var curStyle ) )
105105
{
106106
Logger.Log( $"Property '{propInfo.Name}' is invalid");
107107
return false;
@@ -121,7 +121,7 @@ public bool Bind<TTarget, TProp>(
121121
{
122122
var propInfo2 = (PropertyInfo)unaryMemExpr.Member;
123123

124-
if (!ValidateProperty(propInfo2, bindNonPublic, out var curStyle2))
124+
if (!ValidateProperty(propInfo2, out var curStyle2))
125125
{
126126
Logger.Log($"Property '{propInfo2.Name}' is invalid");
127127
return false;
@@ -145,14 +145,17 @@ public bool Bind<TTarget, TProp>(
145145
break;
146146
}
147147

148-
//if (!ValidateProperty(propElements.First(), bindNonPublic, out var style))
149-
// return false;
150-
151148
propElements.Reverse();
152149

153150
result = new TypeBoundOption<TTarget>(this, GetContextPath(propElements), MasterText);
151+
154152
result.SetStyle(firstStyle!.Value);
155153

154+
foreach( var key in ValidateCommandLineKeys( cmdLineKeys ) )
155+
{
156+
result.AddCommandLineKey( key );
157+
}
158+
156159
Options.Add(result);
157160

158161
return true;
@@ -191,14 +194,14 @@ public IOption? this[ string key ]
191194
}
192195
}
193196

194-
private bool ValidateProperty(PropertyInfo propInfo, bool bindNonPublic, out OptionStyle? style)
197+
private bool ValidateProperty(PropertyInfo propInfo, out OptionStyle? style)
195198
{
196199
style = null;
197200

198-
if (!ValidateAccessMethod(propInfo.GetMethod, bindNonPublic, propInfo.Name, 0))
201+
if (!ValidateAccessMethod(propInfo.GetMethod, propInfo.Name, 0))
199202
return false;
200203

201-
if (!ValidateAccessMethod(propInfo.SetMethod, bindNonPublic, propInfo.Name, 1))
204+
if (!ValidateAccessMethod(propInfo.SetMethod, propInfo.Name, 1))
202205
return false;
203206

204207
if (propInfo.PropertyType.IsEnum)
@@ -272,15 +275,15 @@ private bool ValidateType(Type toCheck)
272275
return false;
273276
}
274277

275-
private bool ValidateAccessMethod(MethodInfo? methodInfo, bool bindNonPublic, string propName, int allowedParams)
278+
private bool ValidateAccessMethod(MethodInfo? methodInfo, string propName, int allowedParams)
276279
{
277280
if (methodInfo == null)
278281
{
279282
Logger.Log($"Property '{propName}' does not have a get or set method");
280283
return false;
281284
}
282285

283-
if (!methodInfo.IsPublic && !bindNonPublic)
286+
if (!methodInfo.IsPublic )
284287
{
285288
Logger.Log($"Property '{propName}::{methodInfo.Name}' is not bindable");
286289
return false;
@@ -295,6 +298,15 @@ private bool ValidateAccessMethod(MethodInfo? methodInfo, bool bindNonPublic, st
295298
return true;
296299
}
297300

301+
private IEnumerable<string> ValidateCommandLineKeys( string[] cmdLineKeys )
302+
{
303+
foreach( var key in cmdLineKeys )
304+
{
305+
if( !UsesCommandLineKey( key ) )
306+
yield return key;
307+
}
308+
}
309+
298310
private string GetContextPath(List<PropertyInfo> propElements) =>
299311
propElements.Aggregate(
300312
new StringBuilder(),

0 commit comments

Comments
 (0)