Skip to content

fix #2589 #2654

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 8, 2025
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
78 changes: 76 additions & 2 deletions src/System.CommandLine.Tests/HelpOptionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.CommandLine.Invocation;
using System.CommandLine.Tests.Utility;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
Expand Down Expand Up @@ -239,9 +240,9 @@ public void The_users_can_set_max_width()

RootCommand rootCommand = new(description);
rootCommand.Options.Clear();
rootCommand.Options.Add(new HelpOption()
rootCommand.Options.Add(new HelpOption
{
Action = new HelpAction()
Action = new HelpAction
{
MaxWidth = 2 /* each line starts with two spaces */ + description.Length / 2
}
Expand Down Expand Up @@ -286,6 +287,79 @@ public void DefaultValueFactory_does_not_throw_when_help_is_invoked()
invocationConfiguration.Error.ToString().Should().Be("");
}

[Fact] // https://github.com/dotnet/command-line-api/issues/2589
public void Help_and_version_options_are_displayed_after_other_options_on_root_command()
{
var command = new RootCommand
{
Subcommands =
{
new Command("subcommand")
{
Description = "The subcommand"
}
},
Options =
{
new Option<int>("-i")
{
Description = "The option"
}
}
};

var output = new StringWriter();


command.Parse("-h").Invoke(new() { Output = output });

output.ToString()
.Split(['\n', '\r'], StringSplitOptions.RemoveEmptyEntries)
.Select(line => line.Trim())
.Should()
.ContainInOrder([
"Options:",
"-i The option",
"-?, -h, --help Show help and usage information",
"--version Show version information",
"Commands:"
]);
}

[Fact] // https://github.com/dotnet/command-line-api/issues/2589
public void Help_and_version_options_are_displayed_after_other_options_on_subcommand()
{
var command = new RootCommand
{
Subcommands =
{
new Command("subcommand")
{
Description = "The subcommand", Options =
{
new Option<int>("-i")
{
Description = "The option"
}
}
}
}
};

var output = new StringWriter();

command.Parse("subcommand -h").Invoke(new() { Output = output });

output.ToString()
.Split(['\n', '\r'], StringSplitOptions.RemoveEmptyEntries)
.Select(line => line.Trim())
.Should()
.ContainInOrder([
"Options:",
"-i The option",
"-?, -h, --help Show help and usage information",
]);
}

private sealed class CustomizedHelpAction : SynchronousCommandLineAction
{
Expand Down
5 changes: 3 additions & 2 deletions src/System.CommandLine/Help/HelpBuilder.Default.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,15 +215,16 @@ public static Func<HelpContext, bool> OptionsSection() =>
{
List<TwoColumnHelpRow> optionRows = new();
bool addedHelpOption = false;
foreach (Option option in ctx.Command.Options)
foreach (Option option in ctx.Command.Options.OrderBy(o => o is HelpOption or VersionOption))
{
if (!option.Hidden)
{
optionRows.Add(ctx.HelpBuilder.GetTwoColumnRow(option, ctx));
if (option is HelpOption)
{
addedHelpOption = true;
}

optionRows.Add(ctx.HelpBuilder.GetTwoColumnRow(option, ctx));
}
}

Expand Down