Skip to content

Commit a5d35ec

Browse files
committed
prep for location change
1 parent f9576e7 commit a5d35ec

File tree

5 files changed

+162
-44
lines changed

5 files changed

+162
-44
lines changed

Binder.Tests/support/BaseTest.cs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,23 @@ namespace J4JSoftware.Binder.Tests
1212
{
1313
public class BaseTest
1414
{
15-
private IAllocator? _allocator;
16-
15+
private readonly CommandLineLogger _cmdLineErrors = new CommandLineLogger();
16+
1717
protected TestConfig? TestConfig { get; private set; }
18+
protected IAllocator? Allocator { get; private set; }
1819
protected OptionCollection? Options { get; private set; }
1920

2021
protected void Initialize( TestConfig testConfig )
2122
{
22-
Options = CompositionRoot.Default.GetOptions();
23-
_allocator = CompositionRoot.Default.GetAllocator();
23+
TestConfig = testConfig;
24+
25+
Options = new OptionCollection( MasterTextCollection.WindowsDefault, _cmdLineErrors );
26+
27+
Allocator = new Allocator(
28+
new ElementTerminator( MasterTextCollection.WindowsDefault, _cmdLineErrors ),
29+
new KeyPrefixer( MasterTextCollection.WindowsDefault, _cmdLineErrors ),
30+
_cmdLineErrors );
31+
2432
TestConfig = testConfig;
2533
}
2634

@@ -48,7 +56,7 @@ protected void Bind<TTarget, TProp>( Expression<Func<TTarget, TProp>> propSelect
4856

4957
protected void ValidateAllocations()
5058
{
51-
var result = _allocator!.AllocateCommandLine(TestConfig!.CommandLine!, Options!);
59+
var result = Allocator!.AllocateCommandLine(TestConfig!.CommandLine!, Options!);
5260

5361
result.UnknownKeys.Count.Should().Be( TestConfig.UnknownKeys );
5462
result.UnkeyedParameters.Count.Should().Be( TestConfig.UnkeyedParameters );
@@ -62,19 +70,19 @@ protected void ValidateAllocations()
6270
protected void ValidateConfiguration<TParsed>()
6371
where TParsed : class, new()
6472
{
65-
var configBuilder = new ConfigurationBuilder();
66-
configBuilder.AddJ4JCommandLine( TestConfig!.CommandLine, out var options, out var logger);
67-
var config = configBuilder.Build();
73+
var configBuilder = new ConfigurationBuilder()
74+
.AddJ4JCommandLine( TestConfig!.CommandLine, MasterTextCollection.WindowsDefault, Allocator!,
75+
_cmdLineErrors, out var options );
6876

6977
TParsed? parsed = null;
7078

71-
if( TestConfig.OptionConfigurations.Any( x => x.ParsingWillFail ) )
79+
if( TestConfig!.OptionConfigurations.Any( x => x.ParsingWillFail ) )
7280
{
73-
var exception = Assert.Throws<InvalidOperationException>( () => config.Get<TParsed>() );
81+
var exception = Assert.Throws<InvalidOperationException>( () => _configRoot.Get<TParsed>() );
7482
return;
7583
}
7684

77-
parsed = config.Get<TParsed>();
85+
parsed = _configRoot.Get<TParsed>();
7886

7987
if( TestConfig.OptionConfigurations.TrueForAll( x => !x.ValuesSatisfied )
8088
&& TestConfig.OptionConfigurations.All( x => x.Style != OptionStyle.Switch ) )

Binder.Tests/support/CompositionRoot.cs

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -38,34 +38,8 @@ private void ConfigureDependencyInjection( HostBuilderContext context, Container
3838
return retVal;
3939
} )
4040
.As<IJ4JLoggerConfiguration>();
41-
42-
builder.RegisterType<OptionCollection>()
43-
.AsSelf();
44-
45-
builder.RegisterType<MasterTextCollection>()
46-
.OnActivating( x =>
47-
{
48-
x.Instance.Initialize( StringComparison.OrdinalIgnoreCase );
49-
x.Instance.AddRange( TextUsageType.Prefix, "-", "--" );
50-
x.Instance.AddRange( TextUsageType.Quote, "\"", "'" );
51-
x.Instance.AddRange( TextUsageType.ValueEncloser, "=" );
52-
} )
53-
.AsSelf();
54-
55-
builder.RegisterType<Allocator>()
56-
.AsImplementedInterfaces();
57-
58-
builder.RegisterType<ElementTerminator>()
59-
.AsImplementedInterfaces();
60-
61-
builder.RegisterType<KeyPrefixer>()
62-
.AsImplementedInterfaces();
6341
}
6442

6543
public IJ4JLogger Logger => Host!.Services.GetRequiredService<IJ4JLogger>();
66-
67-
public OptionCollection GetOptions() => Host!.Services.GetRequiredService<OptionCollection>();
68-
69-
public IAllocator GetAllocator() => Host!.Services.GetRequiredService<IAllocator>();
7044
}
7145
}

Binder/CommandLineLogger.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,30 @@
11
using System;
2+
using System.Collections;
23
using System.Collections.Generic;
34
using System.Linq;
45
using System.Text;
56
using System.Threading.Tasks;
67

78
namespace J4JSoftware.CommandLine
89
{
9-
public class CommandLineLogger
10+
public class CommandLineLogger : IEnumerable<string>
1011
{
1112
private readonly List<string> _messages = new List<string>();
1213

14+
public bool HasMessages => _messages.Count > 0;
15+
1316
public void Log( string mesg ) => _messages.Add( mesg );
17+
public IEnumerator<string> GetEnumerator()
18+
{
19+
foreach( var mesg in _messages )
20+
{
21+
yield return mesg;
22+
}
23+
}
24+
25+
IEnumerator IEnumerable.GetEnumerator()
26+
{
27+
return GetEnumerator();
28+
}
1429
}
1530
}

Binder/J4JCommandLineExtensions.cs

Lines changed: 107 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,119 @@ public static class J4JCommandLineExtensions
88
public static IConfigurationBuilder AddJ4JCommandLine(
99
this IConfigurationBuilder builder,
1010
string cmdLine,
11-
OptionCollection options,
11+
MasterTextCollection masterText,
12+
IAllocator allocator,
13+
CommandLineLogger logger,
14+
out OptionCollection options )
15+
{
16+
options = new OptionCollection( masterText, logger );
17+
18+
builder.Add( new J4JCommandLineSource( options, cmdLine, allocator ) );
19+
20+
return builder;
21+
}
22+
23+
public static IConfigurationBuilder AddJ4JCommandLineWindows(
24+
this IConfigurationBuilder builder,
25+
string cmdLine,
26+
out OptionCollection options,
1227
out CommandLineLogger logger,
1328
IAllocator? allocator = null )
1429
{
15-
var masterText = new MasterTextCollection();
1630
logger = new CommandLineLogger();
1731

18-
allocator ??= new Allocator( new ElementTerminator( masterText, logger ),
19-
new KeyPrefixer( masterText, logger ), logger );
20-
21-
builder.Add( new J4JCommandLineSource( options, cmdLine, allocator ) );
32+
allocator ??= new Allocator(
33+
new ElementTerminator( MasterTextCollection.WindowsDefault, logger ),
34+
new KeyPrefixer( MasterTextCollection.WindowsDefault, logger ),
35+
logger );
2236

37+
builder.AddJ4JCommandLine(
38+
cmdLine,
39+
MasterTextCollection.WindowsDefault,
40+
allocator,
41+
logger,
42+
out var innerOptions);
43+
44+
options = innerOptions;
45+
46+
return builder;
47+
}
48+
49+
public static IConfigurationBuilder AddJ4JCommandLineWindows(
50+
this IConfigurationBuilder builder,
51+
string cmdLine,
52+
out IAllocator allocator,
53+
out OptionCollection options,
54+
out CommandLineLogger logger)
55+
{
56+
logger = new CommandLineLogger();
57+
58+
allocator = new Allocator(
59+
new ElementTerminator(MasterTextCollection.WindowsDefault, logger),
60+
new KeyPrefixer(MasterTextCollection.WindowsDefault, logger),
61+
logger);
62+
63+
builder.AddJ4JCommandLine(
64+
cmdLine,
65+
MasterTextCollection.WindowsDefault,
66+
allocator,
67+
logger,
68+
out var innerOptions);
69+
70+
options = innerOptions;
71+
72+
return builder;
73+
}
74+
75+
public static IConfigurationBuilder AddJ4JCommandLineLinux(
76+
this IConfigurationBuilder builder,
77+
string cmdLine,
78+
out OptionCollection options,
79+
out CommandLineLogger logger,
80+
IAllocator? allocator = null)
81+
{
82+
logger = new CommandLineLogger();
83+
84+
allocator ??= new Allocator(
85+
new ElementTerminator(MasterTextCollection.LinuxDefault, logger),
86+
new KeyPrefixer(MasterTextCollection.LinuxDefault, logger),
87+
logger);
88+
89+
builder.AddJ4JCommandLine(
90+
cmdLine,
91+
MasterTextCollection.LinuxDefault,
92+
allocator,
93+
logger,
94+
out var innerOptions);
95+
96+
options = innerOptions;
97+
98+
return builder;
99+
}
100+
101+
public static IConfigurationBuilder AddJ4JCommandLineLinux(
102+
this IConfigurationBuilder builder,
103+
string cmdLine,
104+
out IAllocator allocator,
105+
out OptionCollection options,
106+
out CommandLineLogger logger)
107+
{
108+
logger = new CommandLineLogger();
109+
110+
allocator = new Allocator(
111+
new ElementTerminator(MasterTextCollection.LinuxDefault, logger),
112+
new KeyPrefixer(MasterTextCollection.LinuxDefault, logger),
113+
logger);
114+
115+
builder.AddJ4JCommandLine(
116+
cmdLine,
117+
MasterTextCollection.LinuxDefault,
118+
allocator,
119+
logger,
120+
out var innerOptions);
121+
122+
options = innerOptions;
123+
23124
return builder;
24125
}
25126
}

Binder/allocating/MasterTextCollection.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,26 @@ namespace J4JSoftware.CommandLine
1111
// option key) used in the framework and ensures they are all used uniquely.
1212
public class MasterTextCollection
1313
{
14+
static MasterTextCollection()
15+
{
16+
WindowsDefault = new MasterTextCollection();
17+
WindowsDefault.AddRange( TextUsageType.Prefix, "/", "-", "--" );
18+
WindowsDefault.Add( TextUsageType.Quote, "\"" );
19+
WindowsDefault.Add( TextUsageType.ValueEncloser, "=" );
20+
21+
WindowsDefault.Initialize(StringComparison.OrdinalIgnoreCase);
22+
23+
LinuxDefault = new MasterTextCollection();
24+
LinuxDefault.AddRange( TextUsageType.Prefix, "-", "--" );
25+
LinuxDefault.AddRange( TextUsageType.Quote, "\"", "'" );
26+
LinuxDefault.Add( TextUsageType.ValueEncloser, "=" );
27+
28+
LinuxDefault.Initialize( StringComparison.Ordinal );
29+
}
30+
31+
public static MasterTextCollection WindowsDefault { get; }
32+
public static MasterTextCollection LinuxDefault { get; }
33+
1434
private readonly List<TextUsage> _items = new List<TextUsage>();
1535

1636
public bool IsValid => TextComparer != null && TextComparison != null;

0 commit comments

Comments
 (0)