Skip to content

Commit

Permalink
Fix parsing values which starts with dash, but has whitespace inside.
Browse files Browse the repository at this point in the history
this is required when your option can accept command line arguments to other applications.

See dotnet/BenchmarkDotNet#2320 (comment) for example of parsing issues
  • Loading branch information
kant2002 committed Jun 6, 2023
1 parent 1e3607b commit 9be1ee8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/CommandLine/Core/Tokenizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static Result<IEnumerable<Token>, Error> Tokenize(
Action<Error> onError = errors.Add;

var tokens = (from arg in arguments
from token in !arg.StartsWith("-", StringComparison.Ordinal)
from token in (!arg.StartsWith("-", StringComparison.Ordinal) || arg.Contains(" "))
? new[] { Token.Value(arg) }
: arg.StartsWith("--", StringComparison.Ordinal)
? TokenizeLongName(arg, onError)
Expand Down
21 changes: 21 additions & 0 deletions tests/CommandLine.Tests/Unit/ParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,27 @@ public void Parse_options_with_double_dash()
// Teardown
}

[Fact]
public void Parse_options_with_whitespace_and_double_dash()
{
// Fixture setup
var expectedOptions = new Simple_Options
{
StringValue = "--astring value",
IntSequence = Enumerable.Empty<int>(),
};
var sut = new Parser();

// Exercize system
var result =
sut.ParseArguments<Simple_Options>(
new[] { "--stringvalue", "--astring value" });

// Verify outcome
((Parsed<Simple_Options>)result).Value.Should().BeEquivalentTo(expectedOptions);
// Teardown
}

[Fact]
public void Parse_options_with_repeated_value_in_values_sequence_and_option()
{
Expand Down

0 comments on commit 9be1ee8

Please sign in to comment.