diff --git a/src/Shared/CommandLineUtils/CommandLine/CommandLineApplication.cs b/src/Shared/CommandLineUtils/CommandLine/CommandLineApplication.cs index b4d85ac00008..145ad3335bf5 100644 --- a/src/Shared/CommandLineUtils/CommandLine/CommandLineApplication.cs +++ b/src/Shared/CommandLineUtils/CommandLine/CommandLineApplication.cs @@ -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(); } } diff --git a/src/Shared/CommandLineUtils/CommandLine/CommandOption.cs b/src/Shared/CommandLineUtils/CommandLine/CommandOption.cs index 588dd6af12f8..93b3c66854d1 100644 --- a/src/Shared/CommandLineUtils/CommandLine/CommandOption.cs +++ b/src/Shared/CommandLineUtils/CommandLine/CommandOption.cs @@ -100,6 +100,35 @@ public string Value() return HasValue() ? Values[0] : null; } + public string GetDisplayText() + { + var parts = new List(); + + 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'); diff --git a/src/Shared/test/Shared.Tests/CommandLineApplicationTests.cs b/src/Shared/test/Shared.Tests/CommandLineApplicationTests.cs index e9a2e6fc6a4e..0b4773e7cac6 100644 --- a/src/Shared/test/Shared.Tests/CommandLineApplicationTests.cs +++ b/src/Shared/test/Shared.Tests/CommandLineApplicationTests.cs @@ -813,7 +813,7 @@ public void OptionsCanBeInherited() Assert.Throws(() => app.Execute("subcmd", "-b", "B")); - Assert.Contains("-a|--option-a", subcmd.GetHelpText()); + Assert.Contains("-a, --option-a", subcmd.GetHelpText()); } [Fact] @@ -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 ", "Show help information", CommandOptionType.SingleValue); + + var helpText = app.GetHelpText(); + + // Verify the option is formatted with comma-space separators + Assert.Contains("-?, -h, --help ", helpText); + } }