Skip to content

Fix help option formatting to use comma-space separator instead of pipe #63284

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 4 commits into from
Aug 22, 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
Original file line number Diff line number Diff line change
Expand Up @@ -493,11 +493,11 @@ public string GetHelpText(string commandName = null)

optionsBuilder.AppendLine();
optionsBuilder.AppendLine("Options:");
var maxOptLen = options.Max(o => o.Template.Length);
var maxOptLen = options.Max(o => o.GetDisplayText().Length);
var outputFormat = string.Format(CultureInfo.InvariantCulture, " {{0, -{0}}}{{1}}", maxOptLen + 2);
foreach (var opt in options)
{
optionsBuilder.AppendFormat(CultureInfo.InvariantCulture, outputFormat, opt.Template, opt.Description);
optionsBuilder.AppendFormat(CultureInfo.InvariantCulture, outputFormat, opt.GetDisplayText(), opt.Description);
optionsBuilder.AppendLine();
}
}
Expand Down
29 changes: 29 additions & 0 deletions src/Shared/CommandLineUtils/CommandLine/CommandOption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,35 @@ public string Value()
return HasValue() ? Values[0] : null;
}

public string GetDisplayText()
{
var parts = new List<string>();

if (!string.IsNullOrEmpty(SymbolName))
{
parts.Add($"-{SymbolName}");
}

if (!string.IsNullOrEmpty(ShortName))
{
parts.Add($"-{ShortName}");
}

if (!string.IsNullOrEmpty(LongName))
{
parts.Add($"--{LongName}");
}

var result = string.Join(", ", parts);

if (!string.IsNullOrEmpty(ValueName))
{
result += $" <{ValueName}>";
}

return result;
}

private static bool IsEnglishLetter(char c)
{
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
Expand Down
16 changes: 15 additions & 1 deletion src/Shared/test/Shared.Tests/CommandLineApplicationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -813,7 +813,7 @@ public void OptionsCanBeInherited()

Assert.Throws<CommandParsingException>(() => app.Execute("subcmd", "-b", "B"));

Assert.Contains("-a|--option-a", subcmd.GetHelpText());
Assert.Contains("-a, --option-a", subcmd.GetHelpText());
}

[Fact]
Expand Down Expand Up @@ -1220,4 +1220,18 @@ public void ThrowExceptionWhenUnmatchedOptionAndTreatUnmatchedOptionsAsArguments

Assert.Equal($"Unrecognized option '{firstOption}'", exception.Message);
}

[Fact]
public void GetHelpTextFormatsAllOptionTypes()
{
var app = new CommandLineApplication();

// Add an option with symbol, short, long, and value name components
app.Option("-?|-h|--help <VALUE>", "Show help information", CommandOptionType.SingleValue);

var helpText = app.GetHelpText();

// Verify the option is formatted with comma-space separators
Assert.Contains("-?, -h, --help <VALUE>", helpText);
}
}
Loading