From 9be1ee811bfc496166b41d8226d6417b407307e4 Mon Sep 17 00:00:00 2001 From: Andrii Kurdiumov Date: Tue, 6 Jun 2023 15:50:22 +0600 Subject: [PATCH] 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 https://github.com/dotnet/BenchmarkDotNet/pull/2320#issuecomment-1577300888 for example of parsing issues --- src/CommandLine/Core/Tokenizer.cs | 2 +- tests/CommandLine.Tests/Unit/ParserTests.cs | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/CommandLine/Core/Tokenizer.cs b/src/CommandLine/Core/Tokenizer.cs index fe94fc61..675fbb4d 100644 --- a/src/CommandLine/Core/Tokenizer.cs +++ b/src/CommandLine/Core/Tokenizer.cs @@ -28,7 +28,7 @@ public static Result, Error> Tokenize( Action 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) diff --git a/tests/CommandLine.Tests/Unit/ParserTests.cs b/tests/CommandLine.Tests/Unit/ParserTests.cs index b079ce0f..8b30fd5a 100644 --- a/tests/CommandLine.Tests/Unit/ParserTests.cs +++ b/tests/CommandLine.Tests/Unit/ParserTests.cs @@ -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(), + }; + var sut = new Parser(); + + // Exercize system + var result = + sut.ParseArguments( + new[] { "--stringvalue", "--astring value" }); + + // Verify outcome + ((Parsed)result).Value.Should().BeEquivalentTo(expectedOptions); + // Teardown + } + [Fact] public void Parse_options_with_repeated_value_in_values_sequence_and_option() {