Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Fix parsing values which starts with dash, but has whitespace inside.
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
commit 9be1ee811bfc496166b41d8226d6417b407307e4
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