Skip to content

Commit ecc0557

Browse files
committed
fix exception when a token is just a prefix
1 parent c6aeffb commit ecc0557

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

src/System.CommandLine.Tests/ParserTests.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,21 @@ public void When_no_options_are_specified_then_an_error_is_returned()
148148
.Be("You must specify at least one option or command.");
149149
}
150150

151+
[Theory]
152+
[InlineData("-")]
153+
[InlineData("/")]
154+
public void When_a_token_is_just_a_prefix_then_an_error_is_returned(string prefix)
155+
{
156+
var parser = new Parser(new RootCommand());
157+
158+
var result = parser.Parse(prefix);
159+
160+
result.Errors
161+
.Select(e => e.Message)
162+
.Should()
163+
.Contain(ValidationMessages.Instance.UnrecognizedCommandOrArgument(prefix));
164+
}
165+
151166
[Fact]
152167
public void Two_options_cannot_have_conflicting_aliases()
153168
{

src/System.CommandLine/Parsing/StringExtensions.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ internal static TokenizeResult Tokenize(
171171

172172
return new TokenizeResult(tokenList, errorList);
173173

174-
bool CanBeUnbundled(string arg, out IEnumerable<string> replacement)
174+
bool CanBeUnbundled(string arg, out IReadOnlyList<string> replacement)
175175
{
176176
replacement = null;
177177

@@ -208,8 +208,14 @@ void AddRestValue(List<string> list, string rest)
208208
}
209209
}
210210

211-
bool TryUnbundle(string arg, out IEnumerable<string> replacement)
211+
bool TryUnbundle(string arg, out IReadOnlyList<string> replacement)
212212
{
213+
if (arg == string.Empty)
214+
{
215+
replacement = null;
216+
return false;
217+
}
218+
213219
var lastTokenHasArgument = false;
214220
var builder = new List<string>();
215221
for (var i = 0; i < arg.Length; i++)

0 commit comments

Comments
 (0)