-
Notifications
You must be signed in to change notification settings - Fork 395
/
Copy pathHelpBuilder.Default.cs
255 lines (225 loc) · 9.85 KB
/
HelpBuilder.Default.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System.Collections;
using System.Collections.Generic;
using System.CommandLine.Completions;
using System.CommandLine.Parsing;
using System.Linq;
namespace System.CommandLine.Help;
public partial class HelpBuilder
{
/// <summary>
/// Provides default formatting for help output.
/// </summary>
public static class Default
{
/// <summary>
/// Gets an argument's default value to be displayed in help.
/// </summary>
/// <param name="argument">The argument to get the default value for.</param>
public static string GetArgumentDefaultValue(Argument argument)
{
if (argument.HasDefaultValue)
{
if (argument.GetDefaultValue() is { } defaultValue)
{
if (defaultValue is IEnumerable enumerable and not string)
{
return string.Join("|", enumerable.OfType<object>().ToArray());
}
else
{
return defaultValue.ToString() ?? "";
}
}
}
return string.Empty;
}
/// <summary>
/// Gets the description for an argument (typically used in the second column text in the arguments section).
/// </summary>
public static string GetArgumentDescription(Argument argument) => argument.Description ?? string.Empty;
/// <summary>
/// Gets the usage title for an argument (for example: <c><value></c>, typically used in the first column text in the arguments usage section, or within the synopsis.
/// </summary>
public static string GetArgumentUsageLabel(Argument argument)
{
if (argument.ValueType == typeof(bool) ||
argument.ValueType == typeof(bool?))
{
if (argument.FirstParent?.Symbol is Command)
{
return $"<{argument.Name}>";
}
else
{
return "";
}
}
string firstColumn;
var completions = (argument is { } a
? a.GetCompletions()
: Array.Empty<CompletionItem>())
.Select(item => item.Label)
.ToArray();
var arg = argument;
var helpName = arg?.HelpName ?? string.Empty;
if (!string.IsNullOrEmpty(helpName))
{
firstColumn = helpName!;
}
else if (completions.Length > 0)
{
firstColumn = string.Join("|", completions);
}
else
{
firstColumn = argument.Name;
}
if (!string.IsNullOrWhiteSpace(firstColumn))
{
return $"<{firstColumn}>";
}
return firstColumn;
}
/// <summary>
/// Gets the description for the specified symbol (typically the used as the second column in help text).
/// </summary>
/// <param name="symbol">The symbol to get the description for.</param>
public static string GetIdentifierSymbolDescription(IdentifierSymbol symbol) => symbol.Description ?? string.Empty;
/// <summary>
/// Gets the usage label for the specified symbol (typically used as the first column text in help output).
/// </summary>
/// <param name="symbol">The symbol to get a help item for.</param>
/// <param name="context">The help context, used for localization purposes.</param>
/// <returns>Text to display.</returns>
public static string GetIdentifierSymbolUsageLabel(IdentifierSymbol symbol, HelpContext context)
{
var aliases = symbol.Aliases
.Select(r => r.SplitPrefix())
.OrderBy(r => r.Prefix, StringComparer.OrdinalIgnoreCase)
.ThenBy(r => r.Alias, StringComparer.OrdinalIgnoreCase)
.GroupBy(t => t.Alias)
.Select(t => t.First())
.Select(t => $"{t.Prefix}{t.Alias}");
var firstColumnText = string.Join(", ", aliases);
foreach (var argument in symbol.Arguments())
{
if (!argument.IsHidden)
{
var argumentFirstColumnText = GetArgumentUsageLabel(argument);
if (!string.IsNullOrWhiteSpace(argumentFirstColumnText))
{
firstColumnText += $" {argumentFirstColumnText}";
}
}
}
if (symbol is Option { IsRequired: true })
{
firstColumnText += $" {context.HelpBuilder.LocalizationResources.HelpOptionsRequiredLabel()}";
}
return firstColumnText;
}
/// <summary>
/// Gets the default sections to be written for command line help.
/// </summary>
public static IEnumerable<Action<HelpContext>> GetLayout()
{
yield return SynopsisSection();
yield return CommandUsageSection();
yield return CommandArgumentsSection();
yield return OptionsSection();
yield return SubcommandsSection();
yield return AdditionalArgumentsSection();
}
/// <summary>
/// Writes a help section describing a command's synopsis.
/// </summary>
public static Action<HelpContext> SynopsisSection() =>
ctx =>
{
ctx.HelpBuilder.WriteHeading(ctx.HelpBuilder.LocalizationResources.HelpDescriptionTitle(), ctx.Command.Description, ctx.Output);
};
/// <summary>
/// Writes a help section describing a command's usage.
/// </summary>
public static Action<HelpContext> CommandUsageSection() =>
ctx =>
{
ctx.HelpBuilder.WriteHeading(ctx.HelpBuilder.LocalizationResources.HelpUsageTitle(), ctx.HelpBuilder.GetUsage(ctx.Command), ctx.Output);
};
/// <summary>
/// Writes a help section describing a command's arguments.
/// </summary>
public static Action<HelpContext> CommandArgumentsSection() =>
ctx =>
{
TwoColumnHelpRow[] commandArguments = ctx.HelpBuilder.GetCommandArgumentRows(ctx.Command, ctx).ToArray();
if (commandArguments.Length <= 0)
{
ctx.WasSectionSkipped = true;
return;
}
ctx.HelpBuilder.WriteHeading(ctx.HelpBuilder.LocalizationResources.HelpArgumentsTitle(), null, ctx.Output);
ctx.HelpBuilder.WriteColumns(commandArguments, ctx);
};
/// <summary>
/// Writes a help section describing a command's subcommands.
/// </summary>
public static Action<HelpContext> SubcommandsSection() =>
ctx => ctx.HelpBuilder.WriteSubcommands(ctx);
/// <summary>
/// Writes a help section describing a command's options.
/// </summary>
public static Action<HelpContext> OptionsSection() =>
ctx =>
{
// by making this logic more complex, we were able to get some nice perf wins elsewhere
List<TwoColumnHelpRow> options = new();
HashSet<Option> uniqueOptions = new();
foreach (Option option in ctx.Command.Options)
{
if (!option.IsHidden && uniqueOptions.Add(option))
{
options.Add(ctx.HelpBuilder.GetTwoColumnRow(option, ctx));
}
}
Command? current = ctx.Command;
while (current is not null)
{
Command? parentCommand = null;
ParentNode? parent = current.FirstParent;
while (parent is not null)
{
if ((parentCommand = parent.Symbol as Command) is not null)
{
foreach (var option in parentCommand.Options)
{
// global help aliases may be duplicated, we just ignore them
if (option.IsGlobal && !option.IsHidden && uniqueOptions.Add(option))
{
options.Add(ctx.HelpBuilder.GetTwoColumnRow(option, ctx));
}
}
break;
}
parent = parent.Next;
}
current = parentCommand;
}
if (options.Count <= 0)
{
ctx.WasSectionSkipped = true;
return;
}
ctx.HelpBuilder.WriteHeading(ctx.HelpBuilder.LocalizationResources.HelpOptionsTitle(), null, ctx.Output);
ctx.HelpBuilder.WriteColumns(options, ctx);
ctx.Output.WriteLine();
};
/// <summary>
/// Writes a help section describing a command's additional arguments, typically shown only when <see cref="Command.TreatUnmatchedTokensAsErrors"/> is set to <see langword="true"/>.
/// </summary>
public static Action<HelpContext> AdditionalArgumentsSection() =>
ctx => ctx.HelpBuilder.WriteAdditionalArguments(ctx);
}
}