Skip to content

Commit 1262f97

Browse files
Change help structure in dotnet test (#47288)
1 parent 89cead9 commit 1262f97

File tree

2 files changed

+55
-46
lines changed

2 files changed

+55
-46
lines changed

src/Cli/dotnet/commands/dotnet-test/Terminal/TerminalTestReporter.cs

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

44
using System.Collections.Concurrent;
5+
using System.CommandLine.Help;
56
using System.Globalization;
67
using System.Text.RegularExpressions;
8+
using Microsoft.DotNet.Cli;
79
using Microsoft.Testing.Platform.Helpers;
810
using LocalizableStrings = Microsoft.DotNet.Tools.Test.LocalizableStrings;
911

@@ -1037,7 +1039,51 @@ public void TestInProgress(
10371039
_terminalWithProgress.UpdateWorker(asm.SlotIndex);
10381040
}
10391041

1040-
public void WriteHeading(string? heading, string? description)
1042+
public void WritePlatformAndExtensionOptions(HelpContext context,
1043+
IEnumerable<CommandLineOption> builtInOptions,
1044+
IEnumerable<CommandLineOption> nonBuiltInOptions,
1045+
Dictionary<bool, List<(string[], string[])>> moduleToMissingOptions)
1046+
{
1047+
if (_wasCancelled)
1048+
{
1049+
return;
1050+
}
1051+
1052+
if (builtInOptions.Any())
1053+
{
1054+
WriteOtherOptionsSection(context, LocalizableStrings.HelpPlatformOptions, builtInOptions);
1055+
context.Output.WriteLine();
1056+
}
1057+
1058+
if (nonBuiltInOptions.Any())
1059+
{
1060+
WriteOtherOptionsSection(context, LocalizableStrings.HelpExtensionOptions, nonBuiltInOptions);
1061+
context.Output.WriteLine();
1062+
}
1063+
WriteModulesToMissingOptionsToConsole(moduleToMissingOptions);
1064+
}
1065+
1066+
private void WriteOtherOptionsSection(HelpContext context, string title, IEnumerable<CommandLineOption> options)
1067+
{
1068+
List<TwoColumnHelpRow> optionRows = [];
1069+
1070+
foreach (var option in options)
1071+
{
1072+
if (option.IsHidden != true)
1073+
{
1074+
optionRows.Add(new TwoColumnHelpRow($"--{option.Name}", option.Description));
1075+
}
1076+
}
1077+
1078+
if (optionRows.Count > 0)
1079+
{
1080+
WriteHeading(title, null);
1081+
context.HelpBuilder.WriteColumns(optionRows, context);
1082+
}
1083+
}
1084+
1085+
1086+
private void WriteHeading(string? heading, string? description)
10411087
{
10421088
if (!string.IsNullOrWhiteSpace(heading))
10431089
{

src/Cli/dotnet/commands/dotnet-test/TestingPlatformCommand.Help.cs

Lines changed: 8 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public IEnumerable<Action<HelpContext>> CustomHelpLayout()
1818
{
1919
yield return (context) =>
2020
{
21+
WriteHelpOptions(context);
2122
Console.WriteLine(LocalizableStrings.HelpWaitingForOptionsAndExtensions);
2223

2324
Run(context.ParseResult);
@@ -27,43 +28,24 @@ public IEnumerable<Action<HelpContext>> CustomHelpLayout()
2728
return;
2829
}
2930

30-
WriteCustomHelp(context);
31-
};
32-
}
31+
Dictionary<bool, List<CommandLineOption>> allOptions = GetAllOptions();
32+
allOptions.TryGetValue(true, out List<CommandLineOption> builtInOptions);
33+
allOptions.TryGetValue(false, out List<CommandLineOption> nonBuiltInOptions);
3334

34-
private void WriteCustomHelp(HelpContext context)
35-
{
36-
var allOptions = GetAllOptions();
37-
var builtInOptions = GetOptionNames(allOptions, isBuiltIn: true);
38-
var nonBuiltInOptions = GetOptionNames(allOptions, isBuiltIn: false);
39-
var moduleToMissingOptions = GetModulesToMissingOptions(_moduleNamesToCommandLineOptions, builtInOptions, nonBuiltInOptions);
35+
Dictionary<bool, List<(string[], string[])>> moduleToMissingOptions = GetModulesToMissingOptions(_moduleNamesToCommandLineOptions, builtInOptions.Select(option => option.Name), nonBuiltInOptions.Select(option => option.Name));
4036

41-
WriteHelpSections(context, allOptions, moduleToMissingOptions);
37+
_output.WritePlatformAndExtensionOptions(context, builtInOptions, nonBuiltInOptions, moduleToMissingOptions);
38+
};
4239
}
4340

44-
private static IEnumerable<string> GetOptionNames(Dictionary<bool, List<CommandLineOption>> allOptions, bool isBuiltIn) => allOptions.TryGetValue(isBuiltIn, out var options) ? options.Select(option => option.Name) : [];
45-
46-
private void WriteHelpSections(HelpContext context, Dictionary<bool, List<CommandLineOption>> allOptions, Dictionary<bool, List<(string[], string[])>> moduleToMissingOptions)
41+
private void WriteHelpOptions(HelpContext context)
4742
{
4843
HelpBuilder.Default.SynopsisSection()(context);
4944
context.Output.WriteLine();
5045
WriteUsageSection(context);
5146
context.Output.WriteLine();
5247
HelpBuilder.Default.OptionsSection()(context);
5348
context.Output.WriteLine();
54-
55-
if (allOptions.TryGetValue(true, out var builtInOptions) && builtInOptions.Count > 0)
56-
{
57-
WriteOtherOptionsSection(context, LocalizableStrings.HelpPlatformOptions, builtInOptions);
58-
context.Output.WriteLine();
59-
}
60-
61-
if (allOptions.TryGetValue(false, out var extensionOptions) && extensionOptions.Count > 0)
62-
{
63-
WriteOtherOptionsSection(context, LocalizableStrings.HelpExtensionOptions, extensionOptions);
64-
context.Output.WriteLine();
65-
}
66-
_output.WriteModulesToMissingOptionsToConsole(moduleToMissingOptions);
6749
}
6850

6951
private static void WriteUsageSection(HelpContext context)
@@ -109,25 +91,6 @@ private static string FormatHelpOption(string option)
10991
return $"[{option.Trim(':').ToLower()}]";
11092
}
11193

112-
private void WriteOtherOptionsSection(HelpContext context, string title, List<CommandLineOption> options)
113-
{
114-
List<TwoColumnHelpRow> optionRows = [];
115-
116-
foreach (var option in options)
117-
{
118-
if ((bool)!option.IsHidden)
119-
{
120-
optionRows.Add(new TwoColumnHelpRow($"--{option.Name}", option.Description));
121-
}
122-
}
123-
124-
if (optionRows.Count > 0)
125-
{
126-
_output.WriteHeading(title, null);
127-
context.HelpBuilder.WriteColumns(optionRows, context);
128-
}
129-
}
130-
13194
private void OnHelpRequested(object sender, HelpEventArgs args)
13295
{
13396
CommandLineOption[] commandLineOptionMessages = args.CommandLineOptions;

0 commit comments

Comments
 (0)