Skip to content

Commit 0ab81a9

Browse files
committed
fix #1533
1 parent a93ebcf commit 0ab81a9

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

src/System.CommandLine.Tests/ParserTests.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1685,6 +1685,27 @@ public void A_subcommand_wont_overflow_when_checking_maximum_argument_capcity()
16851685
act.Should().NotThrow();
16861686
}
16871687

1688+
[Fact] // https://github.com/dotnet/command-line-api/issues/1533
1689+
public void Empty_strings_in_parsed_args_array_are_ignored()
1690+
{
1691+
var option = new Option<int>("-x");
1692+
var subcommand = new Command("sub")
1693+
{
1694+
option
1695+
};
1696+
var command = new RootCommand
1697+
{
1698+
subcommand
1699+
};
1700+
1701+
var result = command.Parse("", "sub", "", "-x", "123");
1702+
1703+
result.Errors.Should().BeEmpty();
1704+
1705+
result.CommandResult.Command.Should().Be(subcommand);
1706+
result.FindResultFor(option).Should().NotBeNull();
1707+
}
1708+
16881709
[TypeConverter(typeof(CustomTypeConverter))]
16891710
public class ClassWithCustomTypeConverter
16901711
{

src/System.CommandLine/Parsing/StringExtensions.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ internal static string RemovePrefix(this string alias)
3636

3737
internal static int GetPrefixLength(this string alias)
3838
{
39-
// empty aliases are illegal, so there is no need to check for length
4039
if (alias[0] == '-')
4140
return alias.Length > 1 && alias[1] == '-' ? 2 : 1;
4241
if (alias[0] == '/')
@@ -76,7 +75,7 @@ internal static TokenizeResult Tokenize(
7675
for (var i = 0; i < argList.Count; i++)
7776
{
7877
var arg = argList[i];
79-
78+
8079
if (foundDoubleDash)
8180
{
8281
if (configuration.EnableLegacyDoubleDashBehavior)
@@ -200,7 +199,7 @@ internal static TokenizeResult Tokenize(
200199
tokenList.Add(Argument(arg));
201200
}
202201
}
203-
else
202+
else if (arg.Length > 0)
204203
{
205204
tokenList.Add(Argument(arg));
206205
}

0 commit comments

Comments
 (0)