Skip to content

Commit 2b278c1

Browse files
committed
Simpliefied how TypeBoundOptions are created. Fixed problem with log message enumerator. Identified bug in allocation system (quotes not being removed).
1 parent 51c94e1 commit 2b278c1

File tree

4 files changed

+17
-23
lines changed

4 files changed

+17
-23
lines changed

Binder.Tests/support/BaseTest.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,8 @@ protected void Initialize( TestConfig testConfig )
2222
protected void Bind<TTarget, TProp>( Expression<Func<TTarget, TProp>> propSelector )
2323
where TTarget : class, new()
2424
{
25-
Options.Bind( propSelector, out var option )
26-
.Should()
27-
.BeTrue();
25+
var option = Options.Bind( propSelector );
26+
option.Should().NotBeNull();
2827

2928
var optConfig = TestConfig!.OptionConfigurations
3029
.FirstOrDefault( x =>

Binder/CommandLineLogger.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class LogEntry
1818
public void LogError( string mesg ) => _messages.Add( new LogEntry { IsError = true, Message = mesg } );
1919
public void LogInformation( string mesg) => _messages.Add(new LogEntry { Message = mesg });
2020

21-
public IEnumerator<string> GetMessages( bool errorsOnly = true )
21+
public IEnumerable<string> GetMessages( bool errorsOnly = true )
2222
{
2323
foreach( var entry in _messages.Where(m=>m.IsError == errorsOnly ) )
2424
{

Binder/option/TypeBoundOption.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@ public override string? ContextPath
2323
if( base.ContextPath == null )
2424
return null;
2525

26-
return Container.TargetsMultipleTypes
27-
? $"{Container.GetContextPathPrefix<TTarget>()}:{base.ContextPath}"
28-
: base.ContextPath;
26+
return $"{Container.GetTypePrefix<TTarget>()}{base.ContextPath}";
2927
}
3028
}
3129
}

Binder/options/OptionCollection.cs

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public OptionCollection(
7272
public ReadOnlyCollection<IOption> Options => _options.AsReadOnly();
7373
public int Count => _options.Count;
7474

75-
public void SetTypeContextKeyPrefix<TTarget>(string prefix)
75+
public void SetTypePrefix<TTarget>(string prefix)
7676
where TTarget : class, new()
7777
{
7878
var type = typeof(TTarget);
@@ -83,15 +83,15 @@ public void SetTypeContextKeyPrefix<TTarget>(string prefix)
8383
_typePrefixes.Add(type, prefix);
8484
}
8585

86-
public string GetContextPathPrefix<TTarget>()
86+
public string GetTypePrefix<TTarget>()
8787
where TTarget : class, new()
8888
{
8989
var type = typeof(TTarget);
9090

91-
if (_typePrefixes.ContainsKey(type))
92-
return _typePrefixes[type];
91+
if( _typePrefixes.ContainsKey( type ) )
92+
return $"{_typePrefixes[ type ]}:";
9393

94-
return type.Name;
94+
return TargetsMultipleTypes ? $"{type.Name}:" : string.Empty;
9595
}
9696

9797
public bool TargetsMultipleTypes => _options.Cast<ITypeBoundOption>().Distinct(_comparer).Count() > 1;
@@ -105,14 +105,11 @@ public IOption Add(string contextPath)
105105
return retVal;
106106
}
107107

108-
public bool Bind<TTarget, TProp>(
108+
public Option? Bind<TTarget, TProp>(
109109
Expression<Func<TTarget, TProp>> propertySelector,
110-
out Option? result,
111110
params string[] cmdLineKeys )
112111
where TTarget : class, new()
113112
{
114-
result = null;
115-
116113
// walk the expression tree to extract the PropertyInfo objects defining
117114
// the path to the property of interest
118115
var propElements = new List<PropertyInfo>();
@@ -129,7 +126,7 @@ public bool Bind<TTarget, TProp>(
129126
if( !ValidateProperty( propInfo, out var curStyle ) )
130127
{
131128
Log.LogError( $"Property '{propInfo.Name}' is invalid");
132-
return false;
129+
return null;
133130
}
134131

135132
firstStyle ??= curStyle;
@@ -149,7 +146,7 @@ public bool Bind<TTarget, TProp>(
149146
if (!ValidateProperty(propInfo2, out var curStyle2))
150147
{
151148
Log.LogError($"Property '{propInfo2.Name}' is invalid");
152-
return false;
149+
return null;
153150
}
154151

155152
firstStyle ??= curStyle2;
@@ -172,18 +169,18 @@ public bool Bind<TTarget, TProp>(
172169

173170
propElements.Reverse();
174171

175-
result = new TypeBoundOption<TTarget>(this, GetContextPath(propElements), MasterText);
172+
var retVal = new TypeBoundOption<TTarget>(this, GetContextPath(propElements), MasterText);
176173

177-
result.SetStyle(firstStyle!.Value);
174+
retVal.SetStyle(firstStyle!.Value);
178175

179176
foreach( var key in ValidateCommandLineKeys( cmdLineKeys ) )
180177
{
181-
result.AddCommandLineKey( key );
178+
retVal.AddCommandLineKey( key );
182179
}
183180

184-
_options.Add(result);
181+
_options.Add(retVal);
185182

186-
return true;
183+
return retVal;
187184
}
188185

189186
// determines whether or not a key is being used by an existing option, honoring whatever

0 commit comments

Comments
 (0)