Skip to content

Commit 55d6172

Browse files
committed
fix #1551
1 parent 4f7a7e7 commit 55d6172

File tree

3 files changed

+14
-22
lines changed

3 files changed

+14
-22
lines changed

src/System.CommandLine.Tests/ArgumentTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

44
using System.Collections.Generic;
5-
using System.CommandLine.Invocation;
65
using System.CommandLine.Parsing;
76
using System.IO;
87
using FluentAssertions;

src/System.CommandLine.Tests/ParserTests.cs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1642,25 +1642,21 @@ public void A_subcommand_wont_overflow_when_checking_maximum_argument_capcity()
16421642
act.Should().NotThrow();
16431643
}
16441644

1645-
[Fact] // https://github.com/dotnet/command-line-api/issues/1533
1646-
public void Empty_strings_in_parsed_args_array_are_ignored()
1645+
[Theory] // https://github.com/dotnet/command-line-api/issues/1551, https://github.com/dotnet/command-line-api/issues/1533
1646+
[InlineData("--exec-prefix", "")]
1647+
[InlineData("--exec-prefix:", "")]
1648+
[InlineData("--exec-prefix=", "")]
1649+
public void Parsed_value_of_empty_string_arg_is_an_empty_string(string arg1, string arg2)
16471650
{
1648-
var option = new Option<int>("-x");
1649-
var subcommand = new Command("sub")
1651+
var option = new Option<string>("--exec-prefix", getDefaultValue: () => "/usr/local");
1652+
var rootCommand = new RootCommand
16501653
{
16511654
option
16521655
};
1653-
var command = new RootCommand
1654-
{
1655-
subcommand
1656-
};
1657-
1658-
var result = command.Parse("", "sub", "", "-x", "123");
16591656

1660-
result.Errors.Should().BeEmpty();
1657+
var result = rootCommand.Parse(new[] { arg1, arg2 });
16611658

1662-
result.CommandResult.Command.Should().BeSameAs(subcommand);
1663-
result.FindResultFor(option).Should().NotBeNull();
1659+
result.GetValueForOption(option).Should().BeEmpty();
16641660
}
16651661
}
16661662
}

src/System.CommandLine/Parsing/StringExtensions.cs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -171,16 +171,13 @@ internal static TokenizeResult Tokenize(
171171
tokenList.Add(Argument(rest));
172172
}
173173
}
174-
else if (configuration.EnablePosixBundling &&
175-
CanBeUnbundled(arg) &&
176-
TryUnbundle(arg.AsSpan(1), i))
177-
{
178-
}
179-
else if (arg.Length > 0)
174+
else if (!configuration.EnablePosixBundling ||
175+
!CanBeUnbundled(arg) ||
176+
!TryUnbundle(arg.AsSpan(1), i))
180177
{
181178
tokenList.Add(Argument(arg));
182179
}
183-
180+
184181
Token Argument(string value) => new(value, TokenType.Argument, default, i);
185182

186183
Token CommandArgument(string value, Command command) => new(value, TokenType.Argument, command, i);
@@ -203,7 +200,7 @@ internal static TokenizeResult Tokenize(
203200
bool CanBeUnbundled(string arg)
204201
=> arg.Length > 2
205202
&& arg[0] == '-'
206-
&& char.IsLetter(arg[1]) // don't check for "--" prefixed args
203+
&& arg[1] != '-'// don't check for "--" prefixed args
207204
&& arg[2] != ':' && arg[2] != '=' // handled by TrySplitIntoSubtokens
208205
&& !PreviousTokenIsAnOptionExpectingAnArgument(out _);
209206

0 commit comments

Comments
 (0)