Skip to content

Commit 2a0ca02

Browse files
committed
Fixed problem with containing quote characters not being stripped from parameter values.
1 parent 2b278c1 commit 2a0ca02

File tree

5 files changed

+63
-8
lines changed

5 files changed

+63
-8
lines changed

Binder.Tests/MiscAllocation.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using FluentAssertions;
2+
using J4JSoftware.CommandLine;
3+
using Xunit;
4+
5+
namespace J4JSoftware.Binder.Tests
6+
{
7+
public class MiscAllocation
8+
{
9+
[ Theory ]
10+
[ InlineData( "-x abc", "abc" ) ]
11+
[ InlineData( "-x \"abc\"", "abc" ) ]
12+
public void StringHandling( string cmdLine, string? result )
13+
{
14+
var options = new OptionCollection();
15+
16+
var option = options.Bind<MiscTarget, string?>( x => x.AStringValue, "x" );
17+
option.Should().NotBeNull();
18+
options.Log.HasMessages().Should().BeFalse();
19+
20+
var allocResult = options.Allocator.AllocateCommandLine( cmdLine, options );
21+
allocResult.UnknownKeys.Should().BeEmpty();
22+
allocResult.UnkeyedParameters.Should().BeEmpty();
23+
24+
option!.CommandLineValues.Count.Should().Be( 1 );
25+
option.CommandLineValues[ 0 ].Should().Be( result );
26+
}
27+
}
28+
}

Binder.Tests/results/MiscTarget.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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.Binder.Tests
8+
{
9+
public class MiscTarget
10+
{
11+
public string? AStringValue { get; set; }
12+
}
13+
}

Binder/allocating/Allocator.cs

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

34
#pragma warning disable 8618
45

@@ -8,16 +9,19 @@ public class Allocator : IAllocator
89
{
910
private readonly IElementTerminator _terminator;
1011
private readonly IKeyPrefixer _prefixer;
12+
private readonly MasterTextCollection _masterText;
1113
private readonly CommandLineLogger _logger;
1214

1315
public Allocator(
1416
IElementTerminator terminator,
1517
IKeyPrefixer prefixer,
18+
MasterTextCollection masterText,
1619
CommandLineLogger logger
1720
)
1821
{
1922
_terminator = terminator;
2023
_prefixer = prefixer;
24+
_masterText = masterText;
2125

2226
_logger = logger;
2327
}
@@ -71,7 +75,17 @@ public AllocationResult AllocateCommandLine( string cmdLine, OptionCollection op
7175
else
7276
{
7377
// element is parameter value
74-
if( curOption == null
78+
79+
// strip off any containing quotes
80+
var firstQuoteChar = element.Where(c =>
81+
_masterText[TextUsageType.Quote].Any(x => x == c.ToString()))
82+
.Select(c => c)
83+
.FirstOrDefault();
84+
85+
if( firstQuoteChar != char.MinValue )
86+
element = element.Replace( firstQuoteChar.ToString(), "" );
87+
88+
if ( curOption == null
7589
|| curOption.NumValuesAllocated >= curOption.MaxValues )
7690
retVal.UnkeyedParameters.Add( element );
7791
else curOption.AddAllocatedValue( element );

Binder/allocating/ElementTerminator.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ public int GetMaxTerminatorLength( string text, bool isKey )
2929
// spaces are terminators only if the text contains no paired allowable quotes
3030
// or has an even number of allowable quotes (i.e., the quotes are 'closed')
3131
var closedQuotes = true;
32-
33-
foreach( var quoteChar in _masterText[TextUsageType.Quote] )
32+
33+
foreach (var quoteChar in _masterText[TextUsageType.Quote])
3434
{
35-
closedQuotes &= text.Count( c => c == quoteChar[0] ) % 2 == 0;
35+
closedQuotes &= text.Count(c => c == quoteChar[0]) % 2 == 0;
3636
}
3737

38-
if( closedQuotes )
38+
if ( closedQuotes )
3939
{
4040
if( text[ ^1 ] == ' ' )
4141
retVal = 1;

Binder/options/OptionCollection.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public OptionCollection(
4545
MasterText = MasterTextCollection.GetDefault(cmdLineStyle);
4646
ElementTerminator = _elementTerminator ?? new ElementTerminator(MasterText, Log);
4747
KeyPrefixer = _keyPrefixer ?? new KeyPrefixer(MasterText, Log);
48-
Allocator = allocator ?? new Allocator(ElementTerminator, KeyPrefixer, Log);
48+
Allocator = allocator ?? new Allocator(ElementTerminator, KeyPrefixer, MasterText, Log);
4949
}
5050

5151
public OptionCollection(
@@ -60,7 +60,7 @@ public OptionCollection(
6060
MasterText = mt;
6161
ElementTerminator = _elementTerminator ?? new ElementTerminator(MasterText, Log);
6262
KeyPrefixer = _keyPrefixer ?? new KeyPrefixer(MasterText, Log);
63-
Allocator = allocator ?? new Allocator(ElementTerminator, KeyPrefixer, Log);
63+
Allocator = allocator ?? new Allocator(ElementTerminator, KeyPrefixer, MasterText, Log);
6464
}
6565

6666
public CommandLineStyle CommandLineStyle { get; }

0 commit comments

Comments
 (0)