Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/CommandLineUtils/HelpText/DefaultHelpTextGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ public class DefaultHelpTextGenerator : IHelpTextGenerator
/// </summary>
protected DefaultHelpTextGenerator() { }

/// <summary>
/// Determines if commands are ordered by name in generated help text
/// </summary>
public bool SortCommandsByName { get; set; } = true;

/// <inheritdoc />
public virtual void Generate(CommandLineApplication application, TextWriter output)
{
Expand Down Expand Up @@ -215,7 +220,10 @@ protected virtual void GenerateCommands(

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

foreach (var cmd in visibleCommands.OrderBy(c => c.Name))
var orderedCommands = SortCommandsByName
? visibleCommands.OrderBy(c => c.Name).ToList()
: visibleCommands;
foreach (var cmd in orderedCommands)
{
var message = string.Format(outputFormat, cmd.Name, cmd.Description);
message = message.Replace(Environment.NewLine, newLineWithMessagePadding);
Expand Down
36 changes: 36 additions & 0 deletions test/CommandLineUtils.Tests/DefaultHelpTextGeneratorTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Nate McMaster.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.IO;
using System.Text;
using McMaster.Extensions.CommandLineUtils.HelpText;
Expand Down Expand Up @@ -37,5 +38,40 @@ public void ItListOptions()
Assert.Contains("--option <OPTION>", helpText);
Assert.DoesNotContain("-|--option <OPTION>", helpText);
}

[Fact]
public void OrderCommandsByName()
{
var app = new CommandLineApplication<EmptyShortName>();
app.Conventions.UseDefaultConventions();
app.Command("b", null);
app.Command("a", null);
var sb = new StringBuilder();
DefaultHelpTextGenerator.Singleton.Generate(app, new StringWriter(sb));
var helpText = sb.ToString();
_output.WriteLine(helpText);

int indexOfA = helpText.IndexOf(" a", StringComparison.InvariantCulture);
int indexOfB = helpText.IndexOf(" b", StringComparison.InvariantCulture);
Assert.True(indexOfA < indexOfB);
}

[Fact]
public void DontOrderCommandsByName()
{
DefaultHelpTextGenerator.Singleton.SortCommandsByName = false;
var app = new CommandLineApplication<EmptyShortName>();
app.Conventions.UseDefaultConventions();
app.Command("b", null);
app.Command("a", null);
var sb = new StringBuilder();
DefaultHelpTextGenerator.Singleton.Generate(app, new StringWriter(sb));
var helpText = sb.ToString();
_output.WriteLine(helpText);

int indexOfA = helpText.IndexOf(" a", StringComparison.InvariantCulture);
int indexOfB = helpText.IndexOf(" b", StringComparison.InvariantCulture);
Assert.True(indexOfA > indexOfB);
}
}
}