Skip to content

Commit fc76f3d

Browse files
authored
Help API adjustments (#1493)
* remove IHelpBuilder * make HelpBuilder.Layout an instance member * fix warnings * make Write virtual
1 parent 804c6e3 commit fc76f3d

20 files changed

+95
-133
lines changed

src/System.CommandLine.Generator.Tests/GeneratedCommandHandlerTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,14 +136,14 @@ public async Task Can_generate_handler_with_well_know_parameters_types()
136136
InvocationContext? boundInvocationContext = null;
137137
IConsole? boundConsole = null;
138138
ParseResult? boundParseResult = null;
139-
IHelpBuilder? boundHelpBuilder = null;
139+
HelpBuilder? boundHelpBuilder = null;
140140
BindingContext? boundBindingContext = null;
141141

142142
void Execute(
143143
InvocationContext invocationContext,
144144
IConsole console,
145145
ParseResult parseResult,
146-
IHelpBuilder helpBuilder,
146+
HelpBuilder helpBuilder,
147147
BindingContext bindingContext)
148148
{
149149
boundInvocationContext = invocationContext;
@@ -155,7 +155,7 @@ void Execute(
155155

156156
var command = new Command("command");
157157

158-
command.SetHandler<Action<InvocationContext, IConsole, ParseResult, IHelpBuilder, BindingContext>>(Execute);
158+
command.SetHandler<Action<InvocationContext, IConsole, ParseResult, HelpBuilder, BindingContext>>(Execute);
159159

160160
await command.InvokeAsync("command", _console);
161161

src/System.CommandLine.Generator/WellKnownTypes.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public WellKnownTypes(Compilation compilation, IEqualityComparer<ISymbol?> compa
2121
Console = GetType("System.CommandLine.IConsole");
2222
ParseResult = GetType("System.CommandLine.Parsing.ParseResult");
2323
InvocationContext = GetType("System.CommandLine.Invocation.InvocationContext");
24-
HelpBuilder = GetType("System.CommandLine.Help.IHelpBuilder");
24+
HelpBuilder = GetType("System.CommandLine.Help.HelpBuilder");
2525
BindingContext = GetType("System.CommandLine.Binding.BindingContext");
2626

2727
INamedTypeSymbol GetType(string typeName)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright (c) .NET Foundation and contributors. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System.CommandLine.Help;
5+
using System.IO;
6+
7+
namespace System.CommandLine.Tests.Help
8+
{
9+
public static class HelpBuilderExtensions
10+
{
11+
public static void Write(
12+
this HelpBuilder builder,
13+
ICommand command,
14+
TextWriter writer) =>
15+
builder.Write(new HelpContext(builder, command, writer));
16+
}
17+
}

src/System.CommandLine.Tests/Help/HelpBuilderTests.Customization.cs

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -205,46 +205,14 @@ public void Command_arguments_can_customize_second_column_text()
205205
_console.ToString().Should().Contain(expected);
206206
}
207207

208-
[Fact]
209-
public void Help_text_can_be_added_after_default_text_by_inheriting_HelpBuilder()
210-
{
211-
var parser = new CommandLineBuilder()
212-
.UseDefaults()
213-
.UseHelpBuilder(context => new CustomHelpBuilderThatAddsTextAfterDefaultText("The text to add"))
214-
.Build();
215-
216-
var console = new TestConsole();
217-
218-
parser.Invoke("-h", console);
219-
220-
console.Out.ToString().Should().EndWith("The text to add");
221-
}
222-
223208
[Fact]
224209
public void Customize_throws_when_symbol_is_null()
225210
{
226211
Action action = () => new HelpBuilder(LocalizationResources.Instance).Customize(null!, "");
227212
action.Should().Throw<ArgumentNullException>();
228213
}
229214
}
230-
231-
private class CustomHelpBuilderThatAddsTextAfterDefaultText : HelpBuilder
232-
{
233-
private readonly string _theTextToAdd;
234-
235-
public CustomHelpBuilderThatAddsTextAfterDefaultText(string theTextToAdd)
236-
: base(LocalizationResources.Instance)
237-
{
238-
_theTextToAdd = theTextToAdd;
239-
}
240-
241-
public override void Write(ICommand command, TextWriter writer, ParseResult parseResult)
242-
{
243-
base.Write(command, writer, parseResult);
244-
writer.Write(_theTextToAdd);
245-
}
246-
}
247-
215+
248216
private class CustomLocalizationResources : LocalizationResources
249217
{
250218
public string OverrideHelpDescriptionTitle { get; set; }

src/System.CommandLine.Tests/Help/HelpBuilderTests.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
using System.Collections.Generic;
66
using System.CommandLine.Builder;
77
using System.CommandLine.Help;
8-
using System.CommandLine.IO;
9-
using System.CommandLine.Parsing;
108
using System.IO;
119
using System.Linq;
1210
using Xunit;

src/System.CommandLine.Tests/Invocation/InvocationPipelineTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ public async Task When_no_help_builder_is_specified_it_uses_default_implementati
279279
bool handlerWasCalled = false;
280280

281281
var command = new Command("help-command");
282-
command.Handler = CommandHandler.Create((IHelpBuilder helpBuilder) =>
282+
command.Handler = CommandHandler.Create((HelpBuilder helpBuilder) =>
283283
{
284284
handlerWasCalled = true;
285285
helpBuilder.Should().NotBeNull();
@@ -304,14 +304,14 @@ public async Task When_help_builder_factory_is_specified_it_is_used_to_create_th
304304

305305
HelpBuilder createdHelpBuilder = null;
306306

307-
Func<BindingContext, IHelpBuilder> helpBuilderFactory = context =>
307+
Func<BindingContext, HelpBuilder> helpBuilderFactory = context =>
308308
{
309309
factoryWasCalled = true;
310310
return createdHelpBuilder = new HelpBuilder(context.ParseResult.Parser.Configuration.LocalizationResources);
311311
};
312312

313313
var command = new Command("help-command");
314-
command.Handler = CommandHandler.Create((IHelpBuilder helpBuilder) =>
314+
command.Handler = CommandHandler.Create((HelpBuilder helpBuilder) =>
315315
{
316316
handlerWasCalled = true;
317317
createdHelpBuilder.Should().NotBeNull();

src/System.CommandLine/Binding/BindingContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public BindingContext(
4040

4141
internal IConsoleFactory? ConsoleFactory { get; set; }
4242

43-
internal IHelpBuilder HelpBuilder => (IHelpBuilder)ServiceProvider.GetService(typeof(IHelpBuilder))!;
43+
internal HelpBuilder HelpBuilder => (HelpBuilder)ServiceProvider.GetService(typeof(HelpBuilder))!;
4444

4545
/// <summary>
4646
/// The console to which output should be written during the current invocation.

src/System.CommandLine/Builder/CommandLineBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public CommandLineBuilder(Command? rootCommand = null)
4646
/// </summary>
4747
public ResponseFileHandling ResponseFileHandling { get; set; }
4848

49-
internal Func<BindingContext, IHelpBuilder>? HelpBuilderFactory { get; set; }
49+
internal Func<BindingContext, HelpBuilder>? HelpBuilderFactory { get; set; }
5050

5151
internal HelpOption? HelpOption { get; set; }
5252

src/System.CommandLine/Builder/CommandLineBuilderExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -467,13 +467,13 @@ internal static CommandLineBuilder UseHelp(
467467
}
468468

469469
/// <summary>
470-
/// Specifies an <see cref="IHelpBuilder"/> to be used to format help output when help is requested.
470+
/// Specifies an <see cref="HelpBuilder"/> to be used to format help output when help is requested.
471471
/// </summary>
472472
/// <param name="builder">A command line builder.</param>
473-
/// <param name="getHelpBuilder">A delegate that returns an instance of <see cref="IHelpBuilder"/></param>
473+
/// <param name="getHelpBuilder">A delegate that returns an instance of <see cref="HelpBuilder"/></param>
474474
/// <returns>The same instance of <see cref="CommandLineBuilder"/>.</returns>
475475
public static TBuilder UseHelpBuilder<TBuilder>(this TBuilder builder,
476-
Func<BindingContext, IHelpBuilder> getHelpBuilder)
476+
Func<BindingContext, HelpBuilder> getHelpBuilder)
477477
where TBuilder : CommandLineBuilder
478478
{
479479
if (builder is null)

src/System.CommandLine/CommandLineConfiguration.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace System.CommandLine
1717
public class CommandLineConfiguration
1818
{
1919
private readonly SymbolSet _symbols = new();
20-
private Func<BindingContext, IHelpBuilder>? _helpBuilderFactory;
20+
private Func<BindingContext, HelpBuilder>? _helpBuilderFactory;
2121

2222
/// <summary>
2323
/// Initializes a new instance of the CommandLineConfiguration class.
@@ -38,7 +38,7 @@ public CommandLineConfiguration(
3838
LocalizationResources? resources = null,
3939
ResponseFileHandling responseFileHandling = ResponseFileHandling.ParseArgsAsLineSeparated,
4040
IReadOnlyList<InvocationMiddleware>? middlewarePipeline = null,
41-
Func<BindingContext, IHelpBuilder>? helpBuilderFactory = null)
41+
Func<BindingContext, HelpBuilder>? helpBuilderFactory = null)
4242
{
4343
RootCommand = command ?? throw new ArgumentNullException(nameof(command));
4444

@@ -56,7 +56,7 @@ public CommandLineConfiguration(
5656
_helpBuilderFactory = helpBuilderFactory;
5757
}
5858

59-
private static IHelpBuilder DefaultHelpBuilderFactory(BindingContext context)
59+
private static HelpBuilder DefaultHelpBuilderFactory(BindingContext context)
6060
{
6161
int maxWidth = int.MaxValue;
6262
if (context.Console is SystemConsole systemConsole)
@@ -115,7 +115,7 @@ private void AddGlobalOptionsToChildren(Command parentCommand)
115115
/// </summary>
116116
public LocalizationResources LocalizationResources { get; }
117117

118-
internal Func<BindingContext, IHelpBuilder> HelpBuilderFactory => _helpBuilderFactory ??= DefaultHelpBuilderFactory;
118+
internal Func<BindingContext, HelpBuilder> HelpBuilderFactory => _helpBuilderFactory ??= DefaultHelpBuilderFactory;
119119

120120
internal IReadOnlyList<InvocationMiddleware> Middleware { get; }
121121

0 commit comments

Comments
 (0)