Skip to content

Commit d0f17c0

Browse files
committed
Revamped errors to act as a logging system and provide more information. Eliminated MappingResults in favor of logging errors and returning booleans. Renamed CommandLineParser to Allocator to reflect what it's doing.
1 parent b489b79 commit d0f17c0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+526
-515
lines changed

AutofacCommandLine/AutofacCommandLineExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public static ContainerBuilder AddJ4JCommandLine( this ContainerBuilder builder
1414
builder.RegisterType<ElementTerminator>()
1515
.AsImplementedInterfaces();
1616

17-
builder.RegisterType<CommandLineParser>()
17+
builder.RegisterType<Allocator>()
1818
.AsImplementedInterfaces();
1919

2020
builder.RegisterType<BindingTargetBuilder>()

FancyHelpError/FancyConsole.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,9 @@ public void AddLine( ConsoleSection section, string? text = null )
6363
_grid.Children.Add( cell );
6464
}
6565

66-
public void AddError( List<string> errors, List<string>? keys = null )
66+
public void AddError( CommandLineLogger.ConsolidatedLog consolidatedLog )
6767
{
68-
keys ??= new List<string>();
69-
var keyText = string.Join( ", ", keys );
68+
var keyText = string.Join( ", ", consolidatedLog.Keys );
7069

7170
_grid.Children.Add( new Cell( keyText )
7271
{
@@ -76,7 +75,7 @@ public void AddError( List<string> errors, List<string>? keys = null )
7675
Margin = Margin
7776
} );
7877

79-
_grid.Children.Add( new Cell( string.Join( "\n", errors ) )
78+
_grid.Children.Add( new Cell( string.Join( "\n", consolidatedLog.Texts ) )
8079
{
8180
Color = ErrorColor,
8281
Stroke = GridThickness,

J4JCommandLine.Tests/ArrayPropertyTest.cs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ namespace J4JCommandLine.Tests
88
public class ArrayPropertyTest
99
{
1010
[Theory]
11-
[InlineData("-z 32 33", true, MappingResult.MissingRequired, new int[] { }, new int[] {33 })]
12-
[InlineData("-x 32 33", true, MappingResult.Success, new int[] { 32 }, new int[] { 33 })]
13-
[InlineData("-z 32 33", false, MappingResult.Success, new int[] { }, new int[] {33 })]
14-
[InlineData("-x 32 -x 33", true, MappingResult.Success, new int[] { 32, 33 }, new int[] { })]
11+
[InlineData("-z 32 33", true, false, new int[] { }, new int[] {33 })]
12+
[InlineData("-x 32 33", true, true, new int[] { 32 }, new int[] { 33 })]
13+
[InlineData("-z 32 33", false, true, new int[] { }, new int[] {33 })]
14+
[InlineData("-x 32 -x 33", true, true, new int[] { 32, 33 }, new int[] { })]
1515
public void root_properties(
1616
string cmdLine,
1717
bool required,
18-
MappingResult result,
18+
bool result,
1919
int[] optValue,
2020
int[] unkeyedValues)
2121
{
@@ -28,14 +28,14 @@ public void root_properties(
2828
}
2929

3030
[Theory]
31-
[InlineData("-z 32 33", true, MappingResult.MissingRequired, new int[] { }, new int[] { 33})]
32-
[InlineData("-x 32 33", true, MappingResult.Success, new int[] { 32 }, new int[] { 33 })]
33-
[InlineData("-z 32 33", false, MappingResult.Success, new int[] { }, new int[] {33 })]
34-
[InlineData("-x 32 -x 33", true, MappingResult.Success, new int[] { 32, 33 }, new int[] { })]
31+
[InlineData("-z 32 33", true, false, new int[] { }, new int[] { 33})]
32+
[InlineData("-x 32 33", true, true, new int[] { 32 }, new int[] { 33 })]
33+
[InlineData("-z 32 33", false, true, new int[] { }, new int[] {33 })]
34+
[InlineData("-x 32 -x 33", true, true, new int[] { 32, 33 }, new int[] { })]
3535
public void parameterless_properties(
3636
string cmdLine,
3737
bool required,
38-
MappingResult result,
38+
bool result,
3939
int[] optValue,
4040
int[] unkeyedValues)
4141
{
@@ -48,14 +48,14 @@ public void parameterless_properties(
4848
}
4949

5050
[Theory]
51-
[InlineData("-z 32 33", true, MappingResult.MissingRequired, new int[] { }, new int[] { 33})]
52-
[InlineData("-x 32 33", true, MappingResult.Success, new int[] { 32 }, new int[] { 33 })]
53-
[InlineData("-z 32 33", false, MappingResult.Success, new int[] { }, new int[] {33 })]
54-
[InlineData("-x 32 -x 33", true, MappingResult.Success, new int[] { 32, 33 }, new int[] { })]
51+
[InlineData("-z 32 33", true, false, new int[] { }, new int[] { 33})]
52+
[InlineData("-x 32 33", true, true, new int[] { 32 }, new int[] { 33 })]
53+
[InlineData("-z 32 33", false, true, new int[] { }, new int[] {33 })]
54+
[InlineData("-x 32 -x 33", true, true, new int[] { 32, 33 }, new int[] { })]
5555
public void parametered_properties(
5656
string cmdLine,
5757
bool required,
58-
MappingResult result,
58+
bool result,
5959
int[] optValue,
6060
int[] unkeyedValues)
6161
{
@@ -77,7 +77,7 @@ private void ProcessTest<T>(
7777
BindingTarget<T> target,
7878
Option option,
7979
Option unkeyed,
80-
MappingResult desiredParseResult,
80+
bool desiredParseResult,
8181
Func<TestProperties> results,
8282
int[] optValue,
8383
int[] unkeyedValues)

J4JCommandLine.Tests/CommandLineParsing.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ namespace J4JCommandLine.Tests
88
{
99
public class CommandLineParsing
1010
{
11-
private readonly ICommandLineParser _cmdLineParser;
12-
private readonly CommandLineErrors _errors;
11+
private readonly IAllocator _cmdLineParser;
12+
private readonly CommandLineLogger _logger;
1313

1414
public CommandLineParsing()
1515
{
16-
_cmdLineParser = ServiceProvider.Instance.GetRequiredService<ICommandLineParser>();
17-
_errors = new CommandLineErrors( StringComparison.OrdinalIgnoreCase );
16+
_cmdLineParser = ServiceProvider.Instance.GetRequiredService<IAllocator>();
17+
_logger = new CommandLineLogger( StringComparison.OrdinalIgnoreCase );
1818

1919
var masterText = new MasterTextCollection(StringComparison.OrdinalIgnoreCase);
2020

@@ -25,9 +25,9 @@ public CommandLineParsing()
2525

2626
if( !_cmdLineParser.Initialize(
2727
StringComparison.OrdinalIgnoreCase,
28-
_errors,
28+
_logger,
2929
masterText ) )
30-
throw new ApplicationException( $"{nameof(CommandLineParser)} initialization failed" );
30+
throw new ApplicationException( $"{nameof(Allocator)} initialization failed" );
3131
}
3232

3333
[ Theory ]
@@ -38,7 +38,7 @@ [ Theory ]
3838
[ InlineData( "-x:\"abc\"", new string[] { "\"abc\"" }, new string[] { } ) ]
3939
public void parse_one_item( string cmdLine, string[] optValue, string[] unkeyedValue )
4040
{
41-
var parsed = _cmdLineParser.Parse( cmdLine );
41+
var parsed = _cmdLineParser.AllocateCommandLine( cmdLine );
4242

4343
parsed.Count.Should().Be( 1 );
4444
parsed.Contains( "x" ).Should().BeTrue();
@@ -61,7 +61,7 @@ public void parse_two_items( string cmdLine, string[] keys, string[] results1, s
6161
{
6262
var results = new string[][] { results1, results2 };
6363

64-
var parsed = _cmdLineParser.Parse( cmdLine );
64+
var parsed = _cmdLineParser.AllocateCommandLine( cmdLine );
6565

6666
parsed.Count.Should().Be( keys.Length );
6767

J4JCommandLine.Tests/EnumTests.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ namespace J4JCommandLine.Tests
88
public class EnumTests
99
{
1010
[ Theory ]
11-
[ InlineData( "z", "B", true, MappingResult.MissingRequired, PlainEnum.A ) ]
12-
[ InlineData( "x", "B", true, MappingResult.Success, PlainEnum.B ) ]
13-
[ InlineData( "z", "B", false, MappingResult.Success, PlainEnum.A ) ]
11+
[ InlineData( "z", "B", true, false, PlainEnum.A ) ]
12+
[ InlineData( "x", "B", true, true, PlainEnum.B ) ]
13+
[ InlineData( "z", "B", false, true, PlainEnum.A ) ]
1414
public void Plain_enum(
1515
string key,
1616
string arg,
1717
bool required,
18-
MappingResult result,
18+
bool result,
1919
PlainEnum desiredValue )
2020
{
2121
var target = ServiceProvider.GetBindingTarget<EnumProperties>( true );
@@ -38,14 +38,14 @@ public void Plain_enum(
3838
}
3939

4040
[Theory]
41-
[InlineData("z", "A,B", true, MappingResult.MissingRequired, FlagsEnum.A)]
42-
[InlineData("x", "A,B", true, MappingResult.Success, FlagsEnum.A | FlagsEnum.B)]
43-
[InlineData("z", "A,B", false, MappingResult.Success, FlagsEnum.A)]
41+
[InlineData("z", "A,B", true, false, FlagsEnum.A)]
42+
[InlineData("x", "A,B", true, true, FlagsEnum.A | FlagsEnum.B)]
43+
[InlineData("z", "A,B", false, true, FlagsEnum.A)]
4444
public void Flags_enum(
4545
string key,
4646
string arg,
4747
bool required,
48-
MappingResult result,
48+
bool result,
4949
FlagsEnum desiredValue)
5050
{
5151
var target = ServiceProvider.GetBindingTarget<EnumProperties>(true);
@@ -68,14 +68,14 @@ public void Flags_enum(
6868
}
6969

7070
[Theory]
71-
[InlineData("z", "A", true, MappingResult.Unbound, UnconvertibleEnum.A)]
72-
[InlineData("x", "B", true, MappingResult.Unbound, UnconvertibleEnum.A)]
73-
[InlineData("z", "A", false, MappingResult.Unbound, UnconvertibleEnum.A)]
71+
[InlineData("z", "A", true, false, UnconvertibleEnum.A)]
72+
[InlineData("x", "B", true, false, UnconvertibleEnum.A)]
73+
[InlineData("z", "A", false, false, UnconvertibleEnum.A)]
7474
public void Unconvertible_enum(
7575
string key,
7676
string arg,
7777
bool required,
78-
MappingResult result,
78+
bool result,
7979
UnconvertibleEnum desiredValue)
8080
{
8181
var target = ServiceProvider.GetBindingTarget<EnumProperties>(true);

J4JCommandLine.Tests/HelpErrorTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ protected class RootProperties
1818
}
1919

2020
[ Theory ]
21-
[ InlineData( "h", MappingResult.HelpRequested ) ]
21+
[ InlineData( "h", true ) ]
2222
public void Trigger_help(
2323
string key,
24-
MappingResult result )
24+
bool result )
2525
{
2626
var target = ServiceProvider.GetBindingTarget<RootProperties>(true);
2727
target.Should().NotBeNull();

J4JCommandLine.Tests/ListPropertyTest.cs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ namespace J4JCommandLine.Tests
88
public class ListPropertyTest
99
{
1010
[Theory]
11-
[InlineData("-z 32 33", true, MappingResult.MissingRequired, new int[] { }, new int[] { 33 })]
12-
[InlineData("-x 32 33", true, MappingResult.Success, new int[] { 32 }, new int[] { 33 })]
13-
[InlineData("-z 32 33", false, MappingResult.Success, new int[] { }, new int[] { 33 })]
14-
[InlineData("-x 32 -x 33", true, MappingResult.Success, new int[] { 32, 33 }, new int[] { })]
11+
[InlineData("-z 32 33", true, false, new int[] { }, new int[] { 33 })]
12+
[InlineData("-x 32 33", true, true, new int[] { 32 }, new int[] { 33 })]
13+
[InlineData("-z 32 33", false, true, new int[] { }, new int[] { 33 })]
14+
[InlineData("-x 32 -x 33", true, true, new int[] { 32, 33 }, new int[] { })]
1515
public void root_properties(
1616
string cmdLine,
1717
bool required,
18-
MappingResult result,
18+
bool result,
1919
int[] optValue,
2020
int[] unkeyedValues)
2121
{
@@ -28,14 +28,14 @@ public void root_properties(
2828
}
2929

3030
[Theory]
31-
[InlineData("-z 32 33", true, MappingResult.MissingRequired, new int[] { }, new int[] { 33 })]
32-
[InlineData("-x 32 33", true, MappingResult.Success, new int[] { 32 }, new int[] { 33 })]
33-
[InlineData("-z 32 33", false, MappingResult.Success, new int[] { }, new int[] { 33 })]
34-
[InlineData("-x 32 -x 33", true, MappingResult.Success, new int[] { 32, 33 }, new int[] { })]
31+
[InlineData("-z 32 33", true, false, new int[] { }, new int[] { 33 })]
32+
[InlineData("-x 32 33", true, true, new int[] { 32 }, new int[] { 33 })]
33+
[InlineData("-z 32 33", false, true, new int[] { }, new int[] { 33 })]
34+
[InlineData("-x 32 -x 33", true, true, new int[] { 32, 33 }, new int[] { })]
3535
public void parameterless_properties(
3636
string cmdLine,
3737
bool required,
38-
MappingResult result,
38+
bool result,
3939
int[] optValue,
4040
int[] unkeyedValues)
4141
{
@@ -48,14 +48,14 @@ public void parameterless_properties(
4848
}
4949

5050
[Theory]
51-
[InlineData("-z 32 33", true, MappingResult.MissingRequired, new int[] { }, new int[] { 33 })]
52-
[InlineData("-x 32 33", true, MappingResult.Success, new int[] { 32 }, new int[] { 33 })]
53-
[InlineData("-z 32 33", false, MappingResult.Success, new int[] { }, new int[] { 33 })]
54-
[InlineData("-x 32 -x 33", true, MappingResult.Success, new int[] { 32, 33 }, new int[] { })]
51+
[InlineData("-z 32 33", true, false, new int[] { }, new int[] { 33 })]
52+
[InlineData("-x 32 33", true, true, new int[] { 32 }, new int[] { 33 })]
53+
[InlineData("-z 32 33", false, true, new int[] { }, new int[] { 33 })]
54+
[InlineData("-x 32 -x 33", true, true, new int[] { 32, 33 }, new int[] { })]
5555
public void parametered_properties(
5656
string cmdLine,
5757
bool required,
58-
MappingResult result,
58+
bool result,
5959
int[] optValue,
6060
int[] unkeyedValues)
6161
{
@@ -77,7 +77,7 @@ private void ProcessTest<T>(
7777
BindingTarget<T> target,
7878
Option option,
7979
Option unkeyed,
80-
MappingResult desiredParseResult,
80+
bool desiredParseResult,
8181
Func<TestProperties> results,
8282
int[] optValue,
8383
int[] unkeyedValues)

J4JCommandLine.Tests/ParsingTests.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ namespace J4JCommandLine.Tests
1111
{
1212
public class ParsingTests
1313
{
14-
private readonly ICommandLineParser _cmdLineParser;
15-
private readonly CommandLineErrors _errors;
14+
private readonly IAllocator _cmdLineParser;
15+
private readonly CommandLineLogger _logger;
1616

1717
public ParsingTests()
1818
{
19-
_cmdLineParser = ServiceProvider.Instance.GetRequiredService<ICommandLineParser>();
20-
_errors = new CommandLineErrors(StringComparison.OrdinalIgnoreCase);
19+
_cmdLineParser = ServiceProvider.Instance.GetRequiredService<IAllocator>();
20+
_logger = new CommandLineLogger(StringComparison.OrdinalIgnoreCase);
2121

2222
var masterText = new MasterTextCollection(StringComparison.OrdinalIgnoreCase);
2323

@@ -28,9 +28,9 @@ public ParsingTests()
2828

2929
if (!_cmdLineParser.Initialize(
3030
StringComparison.OrdinalIgnoreCase,
31-
_errors,
31+
_logger,
3232
masterText))
33-
throw new ApplicationException($"{nameof(CommandLineParser)} initialization failed");
33+
throw new ApplicationException($"{nameof(Allocator)} initialization failed");
3434
}
3535

3636
[ Theory ]
@@ -44,7 +44,7 @@ public void Single_parameter( string key, string[] input, string[] output )
4444
{
4545
var numResults = string.IsNullOrEmpty( key ) ? 0 : 1;
4646

47-
var results = _cmdLineParser.Parse( input );
47+
var results = _cmdLineParser.AllocateCommandLine( input );
4848

4949
results.Count.Should().Be( numResults );
5050

@@ -63,7 +63,7 @@ public void Single_parameter( string key, string[] input, string[] output )
6363
[InlineData("abc -x -y 7 8 -z -12", 3, new string[] { "7", "-12" }, new string[] { "abc", "8" })]
6464
public void Command_line_string(string cmdLine, int numKeys, string[] keyedOutput, string[] unkeyedOutput )
6565
{
66-
var results = _cmdLineParser.Parse(cmdLine);
66+
var results = _cmdLineParser.AllocateCommandLine(cmdLine);
6767

6868
results.Count.Should().Be(numKeys);
6969

J4JCommandLine.Tests/SimplePropertyTest.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ namespace J4JCommandLine.Tests
88
public class SimplePropertyTest
99
{
1010
[Theory]
11-
[InlineData("-z 32", true, MappingResult.MissingRequired, -1, new int[] { })]
12-
[InlineData("-x 32", true, MappingResult.Success, 32, new int[] { })]
13-
[InlineData("-z 32", false, MappingResult.Success, -1, new int[] { })]
11+
[InlineData("-z 32", true, false, -1, new int[] { })]
12+
[InlineData("-x 32", true, true, 32, new int[] { })]
13+
[InlineData("-z 32", false, true, -1, new int[] { })]
1414
public void root_properties(
1515
string cmdLine,
1616
bool required,
17-
MappingResult result,
17+
bool result,
1818
int optValue,
1919
int[] unkeyedValues)
2020
{
@@ -27,13 +27,13 @@ public void root_properties(
2727
}
2828

2929
[Theory]
30-
[InlineData("-z 32", true, MappingResult.MissingRequired, -1, new int[] { })]
31-
[InlineData("-x 32", true, MappingResult.Success, 32, new int[] { })]
32-
[InlineData("-z 32", false, MappingResult.Success, -1, new int[] { })]
30+
[InlineData("-z 32", true, false, -1, new int[] { })]
31+
[InlineData("-x 32", true, true, 32, new int[] { })]
32+
[InlineData("-z 32", false, true, -1, new int[] { })]
3333
public void parameterless_properties(
3434
string cmdLine,
3535
bool required,
36-
MappingResult result,
36+
bool result,
3737
int optValue,
3838
int[] unkeyedValues)
3939
{
@@ -46,13 +46,13 @@ public void parameterless_properties(
4646
}
4747

4848
[Theory]
49-
[InlineData("-z 32", true, MappingResult.MissingRequired, -1, new int[] { })]
50-
[InlineData("-x 32", true, MappingResult.Success, 32, new int[] { })]
51-
[InlineData("-z 32", false, MappingResult.Success, -1, new int[] { })]
49+
[InlineData("-z 32", true, false, -1, new int[] { })]
50+
[InlineData("-x 32", true, true, 32, new int[] { })]
51+
[InlineData("-z 32", false, true, -1, new int[] { })]
5252
public void parametered_properties(
5353
string cmdLine,
5454
bool required,
55-
MappingResult result,
55+
bool result,
5656
int optValue,
5757
int[] unkeyedValues)
5858
{
@@ -74,7 +74,7 @@ private void ProcessTest<T>(
7474
BindingTarget<T> target,
7575
Option option,
7676
Option unkeyed,
77-
MappingResult desiredParseResult,
77+
bool desiredParseResult,
7878
Func<TestProperties> results,
7979
int optValue,
8080
int[] unkeyedValues )

0 commit comments

Comments
 (0)