Skip to content

Commit 2912a6f

Browse files
committed
Allow spaces when tokenizing option-value
Previous behavior split the incoming token long-name value using the equal sign as the separator. This would cause an option's value containing spaces to get truncated. New behavior does regex. match to make sure format is correct and gets the full value.
1 parent 78848d0 commit 2912a6f

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

‎src/CommandLine/Core/Tokenizer.cs‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
usingCommandLine.Infrastructure;
77
usingCSharpx;
88
usingRailwaySharp.ErrorHandling;
9+
usingSystem.Text.RegularExpressions;
910

1011
namespaceCommandLine.Core
1112
{
@@ -188,9 +189,19 @@ private static IEnumerable<Token> TokenizeLongName(
188189
onError(newBadFormatTokenError(value));
189190
yieldbreak;
190191
}
191-
varparts=text.Split('=');
192-
yieldreturnToken.Name(parts[0]);
193-
yieldreturnToken.Value(parts[1],true);
192+
193+
vartokenMatch=Regex.Match(text,"^([^=]+)=([^ ].*)$");
194+
195+
if(tokenMatch.Success)
196+
{
197+
yieldreturnToken.Name(tokenMatch.Groups[1].Value);
198+
yieldreturnToken.Value(tokenMatch.Groups[2].Value,true);
199+
}
200+
else
201+
{
202+
onError(newBadFormatTokenError(value));
203+
yieldbreak;
204+
}
194205
}
195206
}
196207
}

‎tests/CommandLine.Tests/Unit/Core/TokenizerTests.cs‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,41 @@ public void Normalize_should_remove_all_value_with_explicit_assignment_of_existi
9292

9393
// Teardown
9494
}
95+
96+
[Fact]
97+
publicvoidShould_properly_parse_option_with_equals_in_value()
98+
{
99+
/**
100+
* This is how the arg. would look in `static void Main(string[] args)`
101+
* if passed from the command-line and the option-value wrapped in quotes.
102+
* Ex.) ./app --connectionString="Server=localhost;Data Source..."
103+
*/
104+
varargs=new[]{"--connectionString=Server=localhost;Data Source=(LocalDB)\v12.0;Initial Catalog=temp;"};
105+
106+
varresult=Tokenizer.Tokenize(args, name =>NameLookupResult.OtherOptionFound, token =>token);
107+
108+
vartokens=result.SucceededWith();
109+
110+
Assert.NotNull(tokens);
111+
Assert.Equal(2,tokens.Count());
112+
Assert.Equal("connectionString",tokens.First().Text);
113+
Assert.Equal("Server=localhost;Data Source=(LocalDB)\v12.0;Initial Catalog=temp;",tokens.Last().Text);
114+
}
115+
116+
[Fact]
117+
publicvoidShould_return_error_if_option_format_with_equals_is_not_correct()
118+
{
119+
varargs=new[]{"--option1 = fail","--option2= fail"};
120+
121+
varresult=Tokenizer.Tokenize(args, name =>NameLookupResult.OtherOptionFound, token =>token);
122+
123+
vartokens=result.SuccessfulMessages();
124+
125+
Assert.NotNull(tokens);
126+
Assert.Equal(2,tokens.Count());
127+
Assert.Equal(ErrorType.BadFormatTokenError,tokens.First().Tag);
128+
Assert.Equal(ErrorType.BadFormatTokenError,tokens.Last().Tag);
129+
}
95130
}
96131

97132
}

0 commit comments

Comments
(0)