Skip to content

Commit c6aeffb

Browse files
committed
fix #860
1 parent fed075e commit c6aeffb

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

src/System.CommandLine.Tests/Help/HelpBuilderTests.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,6 +1148,46 @@ public void Options_aliases_differing_only_by_prefix_are_deduplicated_favoring_d
11481148
help.Should().NotContain("/long");
11491149
}
11501150

1151+
[Fact]
1152+
public void Options_help_preserves_the_order_options_are_added_the_the_parent_command()
1153+
{
1154+
var command = new RootCommand
1155+
{
1156+
new Option(new[] { "--first", "-f" }),
1157+
new Option(new[] { "--second", "-s" }),
1158+
new Option(new[] { "--third" }),
1159+
new Option(new[] { "--last", "-l" })
1160+
};
1161+
1162+
_helpBuilder.Write(command);
1163+
var help = _console
1164+
.Out
1165+
.ToString()
1166+
.Split(new[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries)
1167+
.Select(l => l.Trim());
1168+
1169+
help.Should().ContainInOrder(
1170+
"-f, --first",
1171+
"-s, --second",
1172+
"--third",
1173+
"-l, --last");
1174+
}
1175+
1176+
[Fact]
1177+
public void Option_aliases_are_shown_before_long_names_regardless_of_alphabetical_order()
1178+
{
1179+
var command = new RootCommand
1180+
{
1181+
new Option(new[] { "-z", "-a", "--zzz", "--aaa" })
1182+
};
1183+
1184+
_helpBuilder.Write(command);
1185+
1186+
_console
1187+
.Out
1188+
.ToString().Should().Contain("-a, -z, --aaa, --zzz");
1189+
}
1190+
11511191
#endregion Options
11521192

11531193
#region Subcommands

src/System.CommandLine/Help/HelpBuilder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -335,8 +335,8 @@ private IEnumerable<HelpItem> GetOptionHelpItems(ISymbol symbol)
335335
var rawAliases = symbol
336336
.RawAliases
337337
.Select(r => r.SplitPrefix())
338-
.OrderBy(r => r.alias)
339-
.ThenBy(r => r.prefix)
338+
.OrderBy(r => r.prefix)
339+
.ThenBy(r => r.alias)
340340
.GroupBy(t => t.alias)
341341
.Select(t => t.First())
342342
.Select(t => $"{t.prefix}{t.alias}");

0 commit comments

Comments
 (0)