Skip to content

Commit 33f88c6

Browse files
committed
prep to branch
1 parent 749ae7c commit 33f88c6

File tree

8 files changed

+108
-63
lines changed

8 files changed

+108
-63
lines changed

Binder/Binder.csproj

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@
2222
<Compile Remove="options\TypeBoundOptions.cs" />
2323
</ItemGroup>
2424

25-
<ItemGroup>
26-
<Folder Include="options\" />
27-
</ItemGroup>
28-
2925
<ItemGroup>
3026
<PackageReference Include="Microsoft.Extensions.Configuration" Version="5.0.0" />
3127
</ItemGroup>

Binder/J4JCommandLineExtensions.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public static IConfigurationBuilder AddJ4JCommandLineWindows(
2727
{
2828
logger = new CommandLineLogger();
2929

30-
var mt = MasterTextCollection.GetWindowsDefault();
30+
var mt = MasterTextCollection.GetDefault( CommandLineStyle.Windows );
3131

3232
allocator ??= new Allocator(
3333
new ElementTerminator( mt, logger ),
@@ -55,7 +55,7 @@ public static IConfigurationBuilder AddJ4JCommandLineWindows(
5555
{
5656
logger = new CommandLineLogger();
5757

58-
var mt = MasterTextCollection.GetWindowsDefault();
58+
var mt = MasterTextCollection.GetDefault(CommandLineStyle.Windows);
5959

6060
allocator = new Allocator(
6161
new ElementTerminator(mt, logger),
@@ -83,7 +83,7 @@ public static IConfigurationBuilder AddJ4JCommandLineLinux(
8383
{
8484
logger = new CommandLineLogger();
8585

86-
var mt = MasterTextCollection.GetLinuxDefault();
86+
var mt = MasterTextCollection.GetDefault( CommandLineStyle.Linux );
8787

8888
allocator ??= new Allocator(
8989
new ElementTerminator(mt, logger),
@@ -111,7 +111,7 @@ public static IConfigurationBuilder AddJ4JCommandLineLinux(
111111
{
112112
logger = new CommandLineLogger();
113113

114-
var mt = MasterTextCollection.GetLinuxDefault();
114+
var mt = MasterTextCollection.GetDefault(CommandLineStyle.Linux);
115115

116116
allocator = new Allocator(
117117
new ElementTerminator(mt, logger),

Binder/allocating/ElementTerminator.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,6 @@ CommandLineLogger logger
2525

2626
public int GetMaxTerminatorLength( string text, bool isKey )
2727
{
28-
if( !_masterText.IsValid )
29-
{
30-
_logger.Log("MasterTextCollection is not initialized" );
31-
throw new TypeInitializationException( "MasterTextCollection is not initialized", null );
32-
}
33-
3428
var retVal = 0;
3529

3630
if( string.IsNullOrEmpty( text ) )
@@ -63,7 +57,7 @@ public int GetMaxTerminatorLength( string text, bool isKey )
6357
? text.Substring( text.Length - termLen, termLen )
6458
: string.Empty;
6559

66-
if( !string.Equals( terminator, tail, _masterText.TextComparison!.Value ) )
60+
if( !string.Equals( terminator, tail, _masterText.TextComparison ) )
6761
continue;
6862

6963
if( termLen > retVal )

Binder/allocating/KeyPrefixer.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,6 @@ CommandLineLogger logger
3030

3131
public int GetMaxPrefixLength( string text )
3232
{
33-
if (!_masterText.IsValid)
34-
{
35-
_logger.Log("MasterTextCollection is not initialized");
36-
throw new TypeInitializationException("MasterTextCollection is not initialized", null);
37-
}
38-
3933
var retVal = 0;
4034

4135
if ( string.IsNullOrEmpty( text ) )
@@ -49,7 +43,7 @@ public int GetMaxPrefixLength( string text )
4943
? text.Substring(0, prefixLen)
5044
: string.Empty;
5145

52-
if (!string.Equals(prefix, start, _masterText.TextComparison!.Value))
46+
if (!string.Equals(prefix, start, _masterText.TextComparison))
5347
continue;
5448

5549
if (prefixLen > retVal)

Binder/allocating/MasterTextCollection.cs

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections;
33
using System.Collections.Generic;
44
using System.Collections.ObjectModel;
5+
using System.ComponentModel;
56
using System.Linq;
67
using System.Net.Sockets;
78

@@ -11,44 +12,44 @@ namespace J4JSoftware.CommandLine
1112
// option key) used in the framework and ensures they are all used uniquely.
1213
public class MasterTextCollection
1314
{
14-
public static MasterTextCollection GetWindowsDefault( StringComparison? comparison = null )
15+
public static MasterTextCollection GetDefault( CommandLineStyle cmdLineStyle, StringComparison? comparison = null )
1516
{
16-
var retVal = new MasterTextCollection();
17+
var retVal = new MasterTextCollection( comparison ?? StringComparison.OrdinalIgnoreCase );
1718

18-
retVal.AddRange( TextUsageType.Prefix, "/", "-", "--" );
19-
retVal.Add( TextUsageType.Quote, "\"" );
20-
retVal.Add( TextUsageType.ValueEncloser, "=" );
19+
switch( cmdLineStyle )
20+
{
21+
case CommandLineStyle.Linux:
22+
retVal.AddRange(TextUsageType.Prefix, "-", "--");
23+
retVal.AddRange(TextUsageType.Quote, "\"", "'");
24+
retVal.Add(TextUsageType.ValueEncloser, "=");
2125

22-
retVal.Initialize( comparison.HasValue ? comparison.Value : StringComparison.OrdinalIgnoreCase );
26+
break;
2327

24-
return retVal;
25-
}
28+
case CommandLineStyle.Universal:
29+
retVal.AddRange(TextUsageType.Prefix, "-", "--", "/");
30+
retVal.AddRange(TextUsageType.Quote, "\"", "'");
31+
retVal.Add(TextUsageType.ValueEncloser, "=");
2632

27-
public static MasterTextCollection GetLinuxDefault( StringComparison? comparison = null )
28-
{
29-
var retVal = new MasterTextCollection();
30-
31-
retVal.AddRange(TextUsageType.Prefix, "-", "--");
32-
retVal.AddRange(TextUsageType.Quote, "\"", "'");
33-
retVal.Add(TextUsageType.ValueEncloser, "=");
33+
break;
34+
35+
case CommandLineStyle.Windows:
36+
retVal.AddRange(TextUsageType.Prefix, "/", "-", "--");
37+
retVal.Add(TextUsageType.Quote, "\"");
38+
retVal.Add(TextUsageType.ValueEncloser, "=");
3439

35-
retVal.Initialize(comparison.HasValue ? comparison.Value : StringComparison.Ordinal);
40+
break;
41+
}
3642

3743
return retVal;
3844
}
39-
45+
4046
private readonly List<TextUsage> _items = new List<TextUsage>();
4147

42-
public bool IsValid => TextComparer != null && TextComparison != null;
43-
44-
public StringComparison? TextComparison { get; private set; }
45-
public IEqualityComparer<string>? TextComparer { get; private set; }
46-
47-
public void Initialize( StringComparison textComp )
48+
public MasterTextCollection( StringComparison comparison )
4849
{
49-
TextComparison = textComp;
50+
TextComparison = comparison;
5051

51-
TextComparer = textComp switch
52+
TextComparer = comparison switch
5253
{
5354
StringComparison.CurrentCultureIgnoreCase => StringComparer.CurrentCultureIgnoreCase,
5455
StringComparison.CurrentCulture => StringComparer.CurrentCulture,
@@ -60,13 +61,16 @@ public void Initialize( StringComparison textComp )
6061
};
6162
}
6263

64+
public StringComparison TextComparison { get; }
65+
public IEqualityComparer<string> TextComparer { get; }
66+
6367
// indicates whether the supplied text exists in the collection
6468
public bool Contains( string text )
65-
=> IsValid && _items.Any( x => string.Equals( x.Text, text, TextComparison!.Value ) );
69+
=> _items.Any( x => string.Equals( x.Text, text, TextComparison ) );
6670

6771
// indicates whether the supplied text exists in the collection for the specified usage
6872
public bool Contains( string text, TextUsageType usage ) =>
69-
IsValid && _items.Any( x => x.Usage == usage && string.Equals( x.Text, text, TextComparison!.Value ) );
73+
_items.Any( x => x.Usage == usage && string.Equals( x.Text, text, TextComparison ) );
7074

7175
// adds an item to the collection
7276
public bool Add( TextUsageType usage, string item )

Binder/options/CommandLineStyle.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace J4JSoftware.CommandLine
2+
{
3+
public enum CommandLineStyle
4+
{
5+
Linux,
6+
Universal,
7+
Windows,
8+
UserDefined
9+
}
10+
}
Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -167,29 +167,17 @@ public bool Bind<TTarget, TProp>(
167167

168168
public bool UsesContextPath( string contextPath )
169169
{
170-
if( !MasterText.IsValid )
171-
{
172-
Logger.Log( "MasterTextCollection is not initialized" );
173-
return false;
174-
}
175-
176170
return Options.Any( x =>
177-
x.ContextPath?.Equals( contextPath, MasterText.TextComparison!.Value ) ?? false );
171+
x.ContextPath?.Equals( contextPath, MasterText.TextComparison ) ?? false );
178172
}
179173

180174
public IOption? this[ string key ]
181175
{
182176
get
183177
{
184-
if (!MasterText.IsValid)
185-
{
186-
Logger.Log("MasterTextCollection is not initialized");
187-
return null;
188-
}
189-
190178
return Options.FirstOrDefault( opt =>
191179
opt.IsInitialized
192-
&& opt.Keys.Any( k => string.Equals( k, key, MasterText.TextComparison!.Value ) )
180+
&& opt.Keys.Any( k => string.Equals( k, key, MasterText.TextComparison ) )
193181
);
194182
}
195183
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace J4JSoftware.CommandLine
8+
{
9+
public abstract class OptionCollectionFactory
10+
{
11+
protected OptionCollectionFactory(
12+
CommandLineStyle cmdLineStyle = CommandLineStyle.Windows,
13+
IAllocator? allocator = null,
14+
IElementTerminator? _elementTerminator = null,
15+
IKeyPrefixer? _keyPrefixer = null
16+
)
17+
{
18+
CommandLineStyle = cmdLineStyle;
19+
Log = new CommandLineLogger();
20+
MasterText = MasterTextCollection.GetDefault( cmdLineStyle );
21+
ElementTerminator = _elementTerminator ?? new ElementTerminator( MasterText, Log );
22+
KeyPrefixer = _keyPrefixer ?? new KeyPrefixer( MasterText, Log );
23+
Allocator = allocator ?? new Allocator( ElementTerminator, KeyPrefixer, Log );
24+
}
25+
26+
protected OptionCollectionFactory(
27+
MasterTextCollection mt,
28+
IAllocator? allocator = null,
29+
IElementTerminator? _elementTerminator = null,
30+
IKeyPrefixer? _keyPrefixer = null
31+
)
32+
{
33+
CommandLineStyle = CommandLineStyle.UserDefined;
34+
Log = new CommandLineLogger();
35+
MasterText = mt;
36+
ElementTerminator = _elementTerminator ?? new ElementTerminator(MasterText, Log);
37+
KeyPrefixer = _keyPrefixer ?? new KeyPrefixer(MasterText, Log);
38+
Allocator = allocator ?? new Allocator(ElementTerminator, KeyPrefixer, Log);
39+
}
40+
41+
public CommandLineStyle CommandLineStyle { get; }
42+
public CommandLineLogger Log { get; }
43+
public MasterTextCollection MasterText { get; }
44+
public IElementTerminator ElementTerminator { get; }
45+
public IKeyPrefixer KeyPrefixer { get; }
46+
public IAllocator Allocator { get; }
47+
48+
public OptionCollection GetOptionCollection()
49+
{
50+
var retVal = new OptionCollection( MasterText, Log );
51+
52+
InitializeOptions( retVal );
53+
54+
return retVal;
55+
}
56+
57+
protected abstract void InitializeOptions( OptionCollection options );
58+
}
59+
}

0 commit comments

Comments
 (0)