Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions src/CommandLine/Core/Tokenizer.cs
Original file line numberDiff line numberDiff line change
Expand Up@@ -6,6 +6,7 @@
using CommandLine.Infrastructure;
using CSharpx;
using RailwaySharp.ErrorHandling;
using System.Text.RegularExpressions;

namespace CommandLine.Core
{
Expand DownExpand Up@@ -188,9 +189,19 @@ private static IEnumerable<Token> TokenizeLongName(
onError(new BadFormatTokenError(value));
yield break;
}
var parts = text.Split('=');
yield return Token.Name(parts[0]);
yield return Token.Value(parts[1], true);

var tokenMatch = Regex.Match(text, "^([^=]+)=([^ ].*)$");

if (tokenMatch.Success)
{
yield return Token.Name(tokenMatch.Groups[1].Value);
yield return Token.Value(tokenMatch.Groups[2].Value, true);
}
else
{
onError(new BadFormatTokenError(value));
yield break;
}
}
}
}
Expand Down
35 changes: 35 additions & 0 deletions tests/CommandLine.Tests/Unit/Core/TokenizerTests.cs
Original file line numberDiff line numberDiff line change
Expand Up@@ -92,6 +92,41 @@ public void Normalize_should_remove_all_value_with_explicit_assignment_of_existi

// Teardown
}

[Fact]
public void Should_properly_parse_option_with_equals_in_value()
{
/**
* This is how the arg. would look in `static void Main(string[] args)`
* if passed from the command-line and the option-value wrapped in quotes.
* Ex.) ./app --connectionString="Server=localhost;Data Source..."
*/
var args = new[]{"--connectionString=Server=localhost;Data Source=(LocalDB)\v12.0;Initial Catalog=temp;"};

var result = Tokenizer.Tokenize(args, name => NameLookupResult.OtherOptionFound, token => token);

var tokens = result.SucceededWith();

Assert.NotNull(tokens);
Assert.Equal(2, tokens.Count());
Assert.Equal("connectionString", tokens.First().Text);
Assert.Equal("Server=localhost;Data Source=(LocalDB)\v12.0;Initial Catalog=temp;", tokens.Last().Text);
}

[Fact]
public void Should_return_error_if_option_format_with_equals_is_not_correct()
{
var args = new[]{"--option1 = fail", "--option2= fail"};

var result = Tokenizer.Tokenize(args, name => NameLookupResult.OtherOptionFound, token => token);

var tokens = result.SuccessfulMessages();

Assert.NotNull(tokens);
Assert.Equal(2, tokens.Count());
Assert.Equal(ErrorType.BadFormatTokenError, tokens.First().Tag);
Assert.Equal(ErrorType.BadFormatTokenError, tokens.Last().Tag);
}
}

}