diff --git a/src/CommandLineUtils/HelpText/DefaultHelpTextGenerator.cs b/src/CommandLineUtils/HelpText/DefaultHelpTextGenerator.cs index 5f758483..cd489622 100644 --- a/src/CommandLineUtils/HelpText/DefaultHelpTextGenerator.cs +++ b/src/CommandLineUtils/HelpText/DefaultHelpTextGenerator.cs @@ -23,6 +23,11 @@ public class DefaultHelpTextGenerator : IHelpTextGenerator /// protected DefaultHelpTextGenerator() { } + /// + /// Determines if commands are ordered by name in generated help text + /// + public bool SortCommandsByName { get; set; } = true; + /// public virtual void Generate(CommandLineApplication application, TextWriter output) { @@ -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); diff --git a/test/CommandLineUtils.Tests/DefaultHelpTextGeneratorTests.cs b/test/CommandLineUtils.Tests/DefaultHelpTextGeneratorTests.cs index 174f5b66..b908fb7a 100644 --- a/test/CommandLineUtils.Tests/DefaultHelpTextGeneratorTests.cs +++ b/test/CommandLineUtils.Tests/DefaultHelpTextGeneratorTests.cs @@ -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; @@ -37,5 +38,40 @@ public void ItListOptions() Assert.Contains("--option