Skip to content

Commit 47e468a

Browse files
authored
Fix help option formatting to use comma-space separator instead of pipe (#63284)
1 parent 391631e commit 47e468a

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
lines changed

src/Shared/CommandLineUtils/CommandLine/CommandLineApplication.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -493,11 +493,11 @@ public string GetHelpText(string commandName = null)
493493

494494
optionsBuilder.AppendLine();
495495
optionsBuilder.AppendLine("Options:");
496-
var maxOptLen = options.Max(o => o.Template.Length);
496+
var maxOptLen = options.Max(o => o.GetDisplayText().Length);
497497
var outputFormat = string.Format(CultureInfo.InvariantCulture, " {{0, -{0}}}{{1}}", maxOptLen + 2);
498498
foreach (var opt in options)
499499
{
500-
optionsBuilder.AppendFormat(CultureInfo.InvariantCulture, outputFormat, opt.Template, opt.Description);
500+
optionsBuilder.AppendFormat(CultureInfo.InvariantCulture, outputFormat, opt.GetDisplayText(), opt.Description);
501501
optionsBuilder.AppendLine();
502502
}
503503
}

src/Shared/CommandLineUtils/CommandLine/CommandOption.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,35 @@ public string Value()
100100
return HasValue() ? Values[0] : null;
101101
}
102102

103+
public string GetDisplayText()
104+
{
105+
var parts = new List<string>();
106+
107+
if (!string.IsNullOrEmpty(SymbolName))
108+
{
109+
parts.Add($"-{SymbolName}");
110+
}
111+
112+
if (!string.IsNullOrEmpty(ShortName))
113+
{
114+
parts.Add($"-{ShortName}");
115+
}
116+
117+
if (!string.IsNullOrEmpty(LongName))
118+
{
119+
parts.Add($"--{LongName}");
120+
}
121+
122+
var result = string.Join(", ", parts);
123+
124+
if (!string.IsNullOrEmpty(ValueName))
125+
{
126+
result += $" <{ValueName}>";
127+
}
128+
129+
return result;
130+
}
131+
103132
private static bool IsEnglishLetter(char c)
104133
{
105134
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');

src/Shared/test/Shared.Tests/CommandLineApplicationTests.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -813,7 +813,7 @@ public void OptionsCanBeInherited()
813813

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

816-
Assert.Contains("-a|--option-a", subcmd.GetHelpText());
816+
Assert.Contains("-a, --option-a", subcmd.GetHelpText());
817817
}
818818

819819
[Fact]
@@ -1220,4 +1220,18 @@ public void ThrowExceptionWhenUnmatchedOptionAndTreatUnmatchedOptionsAsArguments
12201220

12211221
Assert.Equal($"Unrecognized option '{firstOption}'", exception.Message);
12221222
}
1223+
1224+
[Fact]
1225+
public void GetHelpTextFormatsAllOptionTypes()
1226+
{
1227+
var app = new CommandLineApplication();
1228+
1229+
// Add an option with symbol, short, long, and value name components
1230+
app.Option("-?|-h|--help <VALUE>", "Show help information", CommandOptionType.SingleValue);
1231+
1232+
var helpText = app.GetHelpText();
1233+
1234+
// Verify the option is formatted with comma-space separators
1235+
Assert.Contains("-?, -h, --help <VALUE>", helpText);
1236+
}
12231237
}

0 commit comments

Comments
 (0)