Skip to content

Commit 51c94e1

Browse files
committed
Made OptionCollection the central point for the entire process and simplified how it must be created. Also general cleanup.
1 parent 33f88c6 commit 51c94e1

21 files changed

+97
-308
lines changed

Binder.Tests/EmbeddedProperties.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System.Collections.Generic;
2-
using FluentAssertions;
32
using Xunit;
43

54
namespace J4JSoftware.Binder.Tests

Binder.Tests/SingleProperties.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System.Linq;
2-
using Xunit;
1+
using Xunit;
32

43
namespace J4JSoftware.Binder.Tests
54
{
@@ -11,7 +10,7 @@ public void Allocations( TestConfig config )
1110
{
1211
Initialize( config );
1312

14-
Options!.CreateOptionsFromContextKeys( TestConfig!.OptionConfigurations);
13+
Options.CreateOptionsFromContextKeys( config.OptionConfigurations);
1514

1615
ValidateAllocations();
1716
}
@@ -22,7 +21,7 @@ public void Parsing( TestConfig config )
2221
{
2322
Initialize( config );
2423

25-
Options!.CreateOptionsFromContextKeys(TestConfig!.OptionConfigurations);
24+
Options.CreateOptionsFromContextKeys(config.OptionConfigurations);
2625

2726
ValidateConfiguration<BasicTarget>();
2827
}

Binder.Tests/support/BaseTest.cs

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
using System;
22
using System.Linq;
33
using System.Linq.Expressions;
4-
using System.Reflection;
5-
using System.Reflection.Metadata;
6-
using System.Text.Json;
74
using FluentAssertions;
85
using J4JSoftware.CommandLine;
96
using Microsoft.Extensions.Configuration;
@@ -13,33 +10,19 @@ namespace J4JSoftware.Binder.Tests
1310
{
1411
public class BaseTest
1512
{
16-
private CommandLineLogger? _cmdLineErrors;
17-
private IConfigurationBuilder? _configBuilder;
18-
1913
protected TestConfig? TestConfig { get; private set; }
20-
protected IAllocator? Allocator { get; private set; }
21-
protected OptionCollection? Options { get; private set; }
14+
protected OptionCollection Options { get; } = new();
2215

2316
protected void Initialize( TestConfig testConfig )
2417
{
2518
TestConfig = testConfig;
26-
27-
_configBuilder = new ConfigurationBuilder().AddJ4JCommandLineWindows(
28-
TestConfig!.CommandLine,
29-
out var options,
30-
out var allocator,
31-
out var errors );
32-
33-
errors.HasMessages.Should().BeFalse();
34-
35-
Options = options;
36-
Allocator = allocator;
19+
Options.Log.HasMessages().Should().BeFalse();
3720
}
3821

3922
protected void Bind<TTarget, TProp>( Expression<Func<TTarget, TProp>> propSelector )
4023
where TTarget : class, new()
4124
{
42-
Options!.Bind( propSelector, out var option )
25+
Options.Bind( propSelector, out var option )
4326
.Should()
4427
.BeTrue();
4528

@@ -60,7 +43,7 @@ protected void Bind<TTarget, TProp>( Expression<Func<TTarget, TProp>> propSelect
6043

6144
protected void ValidateAllocations()
6245
{
63-
var result = Allocator!.AllocateCommandLine(TestConfig!.CommandLine!, Options!);
46+
var result = Options.Allocator.AllocateCommandLine(TestConfig!.CommandLine!, Options!);
6447

6548
result.UnknownKeys.Count.Should().Be( TestConfig.UnknownKeys );
6649
result.UnkeyedParameters.Count.Should().Be( TestConfig.UnkeyedParameters );
@@ -74,8 +57,12 @@ protected void ValidateAllocations()
7457
protected void ValidateConfiguration<TParsed>()
7558
where TParsed : class, new()
7659
{
77-
var config = _configBuilder!.Build();
78-
60+
var config = new ConfigurationBuilder()
61+
.AddJ4JCommandLine( TestConfig!.CommandLine, Options )
62+
.Build();
63+
64+
Options.Log.HasMessages().Should().BeFalse();
65+
7966
TParsed? parsed = null;
8067

8168
if( TestConfig!.OptionConfigurations.Any( x => x.ParsingWillFail ) )

Binder.Tests/support/CompositionRoot.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using Autofac;
3-
using J4JSoftware.CommandLine;
43
using J4JSoftware.DependencyInjection;
54
using J4JSoftware.Logging;
65
using Microsoft.Extensions.Configuration;

Binder.Tests/test-data/TestConfig.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System.Collections;
2-
using System.Collections.Generic;
1+
using System.Collections.Generic;
32
#pragma warning disable 8618
43

54
namespace J4JSoftware.Binder.Tests

Binder.Tests/test-data/TestDataSource.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using System.IO;
44
using System.Text.Json;
55
using System.Text.Json.Serialization;
6-
using J4JSoftware.CommandLine;
76

87
namespace J4JSoftware.Binder.Tests
98
{

Binder/CommandLineLogger.cs

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,29 @@
1-
using System;
2-
using System.Collections;
3-
using System.Collections.Generic;
1+
using System.Collections.Generic;
42
using System.Linq;
5-
using System.Text;
6-
using System.Threading.Tasks;
73

84
namespace J4JSoftware.CommandLine
95
{
10-
public class CommandLineLogger : IEnumerable<string>
6+
public class CommandLineLogger
117
{
12-
private readonly List<string> _messages = new List<string>();
8+
public class LogEntry
9+
{
10+
public bool IsError { get; set; }
11+
public string Message { get; set; }
12+
}
13+
14+
private readonly List<LogEntry> _messages = new List<LogEntry>();
15+
16+
public bool HasMessages( bool errorsOnly = true ) => _messages.Any( m => m.IsError == errorsOnly );
1317

14-
public bool HasMessages => _messages.Count > 0;
18+
public void LogError( string mesg ) => _messages.Add( new LogEntry { IsError = true, Message = mesg } );
19+
public void LogInformation( string mesg) => _messages.Add(new LogEntry { Message = mesg });
1520

16-
public void Log( string mesg ) => _messages.Add( mesg );
17-
public IEnumerator<string> GetEnumerator()
21+
public IEnumerator<string> GetMessages( bool errorsOnly = true )
1822
{
19-
foreach( var mesg in _messages )
23+
foreach( var entry in _messages.Where(m=>m.IsError == errorsOnly ) )
2024
{
21-
yield return mesg;
25+
yield return $"[{(entry.IsError ? "Error" : "Information")}]:{entry.Message}" ;
2226
}
2327
}
24-
25-
IEnumerator IEnumerable.GetEnumerator()
26-
{
27-
return GetEnumerator();
28-
}
2928
}
3029
}

Binder/J4JCommandLineExtensions.cs

Lines changed: 3 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System.Runtime.InteropServices.ComTypes;
2-
using Microsoft.Extensions.Configuration;
1+
using Microsoft.Extensions.Configuration;
32

43
namespace J4JSoftware.CommandLine
54
{
@@ -8,127 +7,11 @@ public static class J4JCommandLineExtensions
87
public static IConfigurationBuilder AddJ4JCommandLine(
98
this IConfigurationBuilder builder,
109
string cmdLine,
11-
MasterTextCollection masterText,
12-
IAllocator allocator,
13-
OptionCollection options,
14-
CommandLineLogger logger )
10+
OptionCollection options )
1511
{
16-
builder.Add( new J4JCommandLineSource( options, cmdLine, allocator ) );
12+
builder.Add( new J4JCommandLineSource( options, cmdLine ) );
1713

1814
return builder;
1915
}
20-
21-
public static IConfigurationBuilder AddJ4JCommandLineWindows(
22-
this IConfigurationBuilder builder,
23-
string cmdLine,
24-
out OptionCollection options,
25-
out CommandLineLogger logger,
26-
IAllocator? allocator = null )
27-
{
28-
logger = new CommandLineLogger();
29-
30-
var mt = MasterTextCollection.GetDefault( CommandLineStyle.Windows );
31-
32-
allocator ??= new Allocator(
33-
new ElementTerminator( mt, logger ),
34-
new KeyPrefixer( mt, logger ),
35-
logger );
36-
37-
options = new OptionCollection( mt, logger );
38-
39-
builder.AddJ4JCommandLine(
40-
cmdLine,
41-
mt,
42-
allocator,
43-
options,
44-
logger );
45-
46-
return builder;
47-
}
48-
49-
public static IConfigurationBuilder AddJ4JCommandLineWindows(
50-
this IConfigurationBuilder builder,
51-
string cmdLine,
52-
out OptionCollection options,
53-
out IAllocator allocator,
54-
out CommandLineLogger logger )
55-
{
56-
logger = new CommandLineLogger();
57-
58-
var mt = MasterTextCollection.GetDefault(CommandLineStyle.Windows);
59-
60-
allocator = new Allocator(
61-
new ElementTerminator(mt, logger),
62-
new KeyPrefixer(mt, logger),
63-
logger);
64-
65-
options = new OptionCollection(mt, logger);
66-
67-
builder.AddJ4JCommandLine(
68-
cmdLine,
69-
mt,
70-
allocator,
71-
options,
72-
logger);
73-
74-
return builder;
75-
}
76-
77-
public static IConfigurationBuilder AddJ4JCommandLineLinux(
78-
this IConfigurationBuilder builder,
79-
string cmdLine,
80-
out OptionCollection options,
81-
out CommandLineLogger logger,
82-
IAllocator? allocator = null)
83-
{
84-
logger = new CommandLineLogger();
85-
86-
var mt = MasterTextCollection.GetDefault( CommandLineStyle.Linux );
87-
88-
allocator ??= new Allocator(
89-
new ElementTerminator(mt, logger),
90-
new KeyPrefixer(mt, logger),
91-
logger);
92-
93-
options = new OptionCollection(mt, logger);
94-
95-
builder.AddJ4JCommandLine(
96-
cmdLine,
97-
mt,
98-
allocator,
99-
options,
100-
logger);
101-
102-
return builder;
103-
}
104-
105-
public static IConfigurationBuilder AddJ4JCommandLineLinux(
106-
this IConfigurationBuilder builder,
107-
string cmdLine,
108-
out OptionCollection options,
109-
out IAllocator allocator,
110-
out CommandLineLogger logger)
111-
{
112-
logger = new CommandLineLogger();
113-
114-
var mt = MasterTextCollection.GetDefault(CommandLineStyle.Linux);
115-
116-
allocator = new Allocator(
117-
new ElementTerminator(mt, logger),
118-
new KeyPrefixer(mt, logger),
119-
logger);
120-
121-
options = new OptionCollection(mt, logger);
122-
123-
builder.AddJ4JCommandLine(
124-
cmdLine,
125-
mt,
126-
allocator,
127-
options,
128-
logger);
129-
130-
return builder;
131-
}
132-
13316
}
13417
}

Binder/J4JCommandLineProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public J4JCommandLineProvider( J4JCommandLineSource source )
1414

1515
public override void Load()
1616
{
17-
Source.Allocator.AllocateCommandLine( Source.CommandLine, Source.Options );
17+
Source.Options.Allocator.AllocateCommandLine( Source.CommandLine, Source.Options );
1818

1919
foreach( var option in Source.Options )
2020
{

Binder/J4JCommandLineSource.cs

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,28 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
6-
using Microsoft.Extensions.Configuration;
7-
using Microsoft.Extensions.Primitives;
1+
using Microsoft.Extensions.Configuration;
82

93
namespace J4JSoftware.CommandLine
104
{
115
public class J4JCommandLineSource : IConfigurationSource
126
{
137
public J4JCommandLineSource(
148
OptionCollection options,
15-
string cmdLine,
16-
IAllocator allocator
9+
string cmdLine
1710
)
1811
{
1912
Options = options;
2013
CommandLine = cmdLine;
21-
Allocator = allocator;
2214
}
2315

2416
public J4JCommandLineSource(
2517
OptionCollection options,
26-
string[] args,
27-
IAllocator allocator
18+
string[] args
2819
)
29-
: this( options, string.Join( " ", args ), allocator )
20+
: this( options, string.Join( " ", args ) )
3021
{
3122
}
3223

3324
public OptionCollection Options { get; }
3425
public string CommandLine { get; }
35-
public IAllocator Allocator { get; }
3626

3727
public IConfigurationProvider Build( IConfigurationBuilder builder )
3828
{

0 commit comments

Comments
 (0)