Skip to content

Commit 8367e32

Browse files
lvermeulennatemcmaster
authored andcommitted
Add DefaultHelpTextGenerator.SortCommandsByName (#148)
1 parent c070b58 commit 8367e32

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

src/CommandLineUtils/HelpText/DefaultHelpTextGenerator.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ public class DefaultHelpTextGenerator : IHelpTextGenerator
2323
/// </summary>
2424
protected DefaultHelpTextGenerator() { }
2525

26+
/// <summary>
27+
/// Determines if commands are ordered by name in generated help text
28+
/// </summary>
29+
public bool SortCommandsByName { get; set; } = true;
30+
2631
/// <inheritdoc />
2732
public virtual void Generate(CommandLineApplication application, TextWriter output)
2833
{
@@ -215,7 +220,10 @@ protected virtual void GenerateCommands(
215220

216221
var newLineWithMessagePadding = Environment.NewLine + new string(' ', firstColumnWidth + 2);
217222

218-
foreach (var cmd in visibleCommands.OrderBy(c => c.Name))
223+
var orderedCommands = SortCommandsByName
224+
? visibleCommands.OrderBy(c => c.Name).ToList()
225+
: visibleCommands;
226+
foreach (var cmd in orderedCommands)
219227
{
220228
var message = string.Format(outputFormat, cmd.Name, cmd.Description);
221229
message = message.Replace(Environment.NewLine, newLineWithMessagePadding);

test/CommandLineUtils.Tests/DefaultHelpTextGeneratorTests.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Nate McMaster.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
using System;
45
using System.IO;
56
using System.Text;
67
using McMaster.Extensions.CommandLineUtils.HelpText;
@@ -37,5 +38,40 @@ public void ItListOptions()
3738
Assert.Contains("--option <OPTION>", helpText);
3839
Assert.DoesNotContain("-|--option <OPTION>", helpText);
3940
}
41+
42+
[Fact]
43+
public void OrderCommandsByName()
44+
{
45+
var app = new CommandLineApplication<EmptyShortName>();
46+
app.Conventions.UseDefaultConventions();
47+
app.Command("b", null);
48+
app.Command("a", null);
49+
var sb = new StringBuilder();
50+
DefaultHelpTextGenerator.Singleton.Generate(app, new StringWriter(sb));
51+
var helpText = sb.ToString();
52+
_output.WriteLine(helpText);
53+
54+
int indexOfA = helpText.IndexOf(" a", StringComparison.InvariantCulture);
55+
int indexOfB = helpText.IndexOf(" b", StringComparison.InvariantCulture);
56+
Assert.True(indexOfA < indexOfB);
57+
}
58+
59+
[Fact]
60+
public void DontOrderCommandsByName()
61+
{
62+
DefaultHelpTextGenerator.Singleton.SortCommandsByName = false;
63+
var app = new CommandLineApplication<EmptyShortName>();
64+
app.Conventions.UseDefaultConventions();
65+
app.Command("b", null);
66+
app.Command("a", null);
67+
var sb = new StringBuilder();
68+
DefaultHelpTextGenerator.Singleton.Generate(app, new StringWriter(sb));
69+
var helpText = sb.ToString();
70+
_output.WriteLine(helpText);
71+
72+
int indexOfA = helpText.IndexOf(" a", StringComparison.InvariantCulture);
73+
int indexOfB = helpText.IndexOf(" b", StringComparison.InvariantCulture);
74+
Assert.True(indexOfA > indexOfB);
75+
}
4076
}
4177
}

0 commit comments

Comments
 (0)