Skip to content

Commit 66d9708

Browse files
authored
Ensure longer option is parsed when aliases are prefixed (#1177)
* Ensure longer option is parsed when aliases are prefixed * PR feedback
1 parent 3835463 commit 66d9708

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

src/System.CommandLine.Tests/OptionTests.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,15 @@ public void When_Name_is_set_to_its_current_value_then_it_is_not_removed_from_al
371371
option.Aliases.Should().Contain("name");
372372
}
373373

374+
[Fact]
375+
public void When_aliases_overlap_the_longer_alias_is_chosen()
376+
{
377+
var option = new Option<string>(new[] { "-o", "-option" });
378+
379+
var parseResult = option.Parse("-option value");
380+
parseResult.ValueForOption(option).Should().Be("value");
381+
}
382+
374383
protected override Symbol CreateSymbol(string name) => new Option(name);
375384
}
376385
}

src/System.CommandLine/Parsing/StringExtensions.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,12 @@ bool CanBeUnbundled(string arg, out IReadOnlyList<string>? replacement)
204204
return false;
205205
}
206206

207+
// don't unbundle if this is a known option
208+
if (knownTokens.ContainsKey(arg))
209+
{
210+
return false;
211+
}
212+
207213
var (prefix, alias) = arg.SplitPrefix();
208214

209215
return prefix == "-" &&

0 commit comments

Comments
 (0)