Skip to content

Commit e414105

Browse files
authored
Remove Middleware (#2100)
* ParseResult.UnmatchedTokens should return an array, not IReadOnlyList<string> as it allows for less * introduce new HostingAction to avoid using middleware in Hosting. It allows us for creating the host and everything else after parsing has finished * UseCommandHandler must specify the handler for instance of a Command, not per type as there can be multiple commands of the same type * don't use GetAwaiter().GetResult() to block, make the tests async * remove Middleware
1 parent 9ebcd90 commit e414105

25 files changed

+167
-751
lines changed

src/System.CommandLine.ApiCompatibility.Tests/ApiCompatibilityApprovalTests.System_CommandLine_Hosting_api_is_not_changed.approved.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ System.CommandLine.Hosting
66
public static Microsoft.Extensions.Hosting.IHost GetHost(this System.CommandLine.Invocation.InvocationContext invocationContext)
77
public static System.CommandLine.Invocation.InvocationContext GetInvocationContext(this Microsoft.Extensions.Hosting.IHostBuilder hostBuilder)
88
public static System.CommandLine.Invocation.InvocationContext GetInvocationContext(this Microsoft.Extensions.Hosting.HostBuilderContext context)
9-
public static Microsoft.Extensions.Hosting.IHostBuilder UseCommandHandler<TCommand, THandler>(this Microsoft.Extensions.Hosting.IHostBuilder builder)
10-
public static Microsoft.Extensions.Hosting.IHostBuilder UseCommandHandler(this Microsoft.Extensions.Hosting.IHostBuilder builder, System.Type commandType, System.Type handlerType)
9+
public static System.CommandLine.Command UseCommandHandler<THandler>(this System.CommandLine.Command command)
1110
public static System.CommandLine.CommandLineBuilder UseHost(this System.CommandLine.CommandLineBuilder builder, System.Action<Microsoft.Extensions.Hosting.IHostBuilder> configureHost = null)
1211
public static System.CommandLine.CommandLineBuilder UseHost(this System.CommandLine.CommandLineBuilder builder, System.Func<System.String[],Microsoft.Extensions.Hosting.IHostBuilder> hostBuilderFactory, System.Action<Microsoft.Extensions.Hosting.IHostBuilder> configureHost = null)
1312
public static Microsoft.Extensions.Hosting.IHostBuilder UseInvocationLifetime(this Microsoft.Extensions.Hosting.IHostBuilder host, System.CommandLine.Invocation.InvocationContext invocation, System.Action<InvocationLifetimeOptions> configureOptions = null)

src/System.CommandLine.ApiCompatibility.Tests/ApiCompatibilityApprovalTests.System_CommandLine_NamingConventionBinder_api_is_not_changed.approved.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ System.CommandLine.NamingConventionBinder
1919
public static ModelBinder GetOrCreateModelBinder(this System.CommandLine.Binding.BindingContext bindingContext, System.CommandLine.Binding.IValueDescriptor valueDescriptor)
2020
public abstract class BindingHandler : System.CommandLine.CliAction
2121
public System.CommandLine.Binding.BindingContext GetBindingContext(System.CommandLine.Invocation.InvocationContext invocationContext)
22-
public System.CommandLine.Binding.BindingContext SetBindingContext(System.CommandLine.Binding.BindingContext bindingContext)
2322
public static class CommandHandler
2423
public static BindingHandler Create(System.Delegate delegate)
2524
public static BindingHandler Create<T>(Action<T> action)

src/System.CommandLine.ApiCompatibility.Tests/ApiCompatibilityApprovalTests.System_CommandLine_api_is_not_changed.approved.txt

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,6 @@ System.CommandLine
6464
.ctor(Command rootCommand)
6565
public Command Command { get; }
6666
public System.Collections.Generic.List<Directive> Directives { get; }
67-
public CommandLineBuilder AddMiddleware(System.CommandLine.Invocation.InvocationMiddleware middleware, System.CommandLine.Invocation.MiddlewareOrder order = Default)
68-
public CommandLineBuilder AddMiddleware(System.Action<System.CommandLine.Invocation.InvocationContext> onInvoke, System.CommandLine.Invocation.MiddlewareOrder order = Default)
6967
public CommandLineConfiguration Build()
7068
public CommandLineBuilder CancelOnProcessTermination(System.Nullable<System.TimeSpan> timeout = null)
7169
public CommandLineBuilder EnablePosixBundling(System.Boolean value = True)
@@ -85,7 +83,7 @@ System.CommandLine
8583
public CommandLineBuilder UseVersionOption(System.String name, System.String[] aliases)
8684
public class CommandLineConfiguration
8785
public static CommandLineBuilder CreateBuilder(Command rootCommand)
88-
.ctor(Command command, System.Boolean enablePosixBundling = True, System.Boolean enableTokenReplacement = True, System.Collections.Generic.IReadOnlyList<System.CommandLine.Invocation.InvocationMiddleware> middlewarePipeline = null, System.Func<System.CommandLine.Invocation.InvocationContext,System.CommandLine.Help.HelpBuilder> helpBuilderFactory = null, System.CommandLine.Parsing.TryReplaceToken tokenReplacer = null)
86+
.ctor(Command command, System.Boolean enablePosixBundling = True, System.Boolean enableTokenReplacement = True, System.Func<System.CommandLine.Invocation.InvocationContext,System.CommandLine.Help.HelpBuilder> helpBuilderFactory = null, System.CommandLine.Parsing.TryReplaceToken tokenReplacer = null)
8987
public System.Collections.Generic.IReadOnlyList<Directive> Directives { get; }
9088
public System.Boolean EnablePosixBundling { get; }
9189
public System.Boolean EnableTokenReplacement { get; }
@@ -142,7 +140,7 @@ System.CommandLine
142140
public System.Collections.Generic.IReadOnlyList<System.CommandLine.Parsing.ParseError> Errors { get; }
143141
public System.CommandLine.Parsing.CommandResult RootCommandResult { get; }
144142
public System.Collections.Generic.IReadOnlyList<System.CommandLine.Parsing.Token> Tokens { get; }
145-
public System.Collections.Generic.IReadOnlyList<System.String> UnmatchedTokens { get; }
143+
public System.String[] UnmatchedTokens { get; }
146144
public System.CommandLine.Parsing.ArgumentResult FindResultFor(Argument argument)
147145
public System.CommandLine.Parsing.CommandResult FindResultFor(Command command)
148146
public System.CommandLine.Parsing.OptionResult FindResultFor(Option option)
@@ -243,16 +241,6 @@ System.CommandLine.Invocation
243241
public System.CommandLine.ParseResult ParseResult { get; set; }
244242
public T GetValue<T>(Option<T> option)
245243
public T GetValue<T>(Argument<T> argument)
246-
public delegate InvocationMiddleware : System.MulticastDelegate, System.ICloneable, System.Runtime.Serialization.ISerializable
247-
.ctor(System.Object object, System.IntPtr method)
248-
public System.IAsyncResult BeginInvoke(InvocationContext context, System.Threading.CancellationToken cancellationToken, System.Func<InvocationContext,System.Threading.CancellationToken,System.Threading.Tasks.Task> next, System.AsyncCallback callback, System.Object object)
249-
public System.Threading.Tasks.Task EndInvoke(System.IAsyncResult result)
250-
public System.Threading.Tasks.Task Invoke(InvocationContext context, System.Threading.CancellationToken cancellationToken, System.Func<InvocationContext,System.Threading.CancellationToken,System.Threading.Tasks.Task> next)
251-
public enum MiddlewareOrder : System.Enum, System.IComparable, System.IConvertible, System.IFormattable
252-
Default=0
253-
ErrorReporting=1000
254-
ExceptionHandler=-2000
255-
Configuration=-1000
256244
System.CommandLine.IO
257245
public interface IStandardError
258246
public IStandardStreamWriter Error { get; }

src/System.CommandLine.DragonFruit/CommandLine.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,7 @@ private static CommandLineConfiguration BuildConfiguration(MethodInfo method,
110110
var builder = new CommandLineBuilder(new RootCommand())
111111
.ConfigureRootCommandFromMethod(method, target)
112112
.ConfigureHelpFromXmlComments(method, xmlDocsFilePath)
113-
.UseDefaults()
114-
.UseAnsiTerminalWhenAvailable();
113+
.UseDefaults();
115114

116115
return builder.Build();
117116
}

src/System.CommandLine.Hosting.Tests/HostingHandlerTest.cs

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,19 @@ namespace System.CommandLine.Hosting.Tests
1313
{
1414
public static class HostingHandlerTest
1515
{
16-
1716
[Fact]
1817
public static async Task Constructor_Injection_Injects_Service()
1918
{
2019
var service = new MyService();
2120

2221
var config = new CommandLineBuilder(
23-
new MyCommand()
22+
new MyCommand().UseCommandHandler<MyCommand.MyHandler>()
2423
)
2524
.UseHost((builder) => {
2625
builder.ConfigureServices(services =>
2726
{
2827
services.AddTransient(x => service);
29-
})
30-
.UseCommandHandler<MyCommand, MyCommand.MyHandler>();
28+
});
3129
})
3230
.Build();
3331

@@ -39,14 +37,13 @@ public static async Task Constructor_Injection_Injects_Service()
3937
[Fact]
4038
public static async Task Parameter_is_available_in_property()
4139
{
42-
var config = new CommandLineBuilder(new MyCommand())
40+
var config = new CommandLineBuilder(new MyCommand().UseCommandHandler<MyCommand.MyHandler>())
4341
.UseHost(host =>
4442
{
4543
host.ConfigureServices(services =>
4644
{
4745
services.AddTransient<MyService>();
48-
})
49-
.UseCommandHandler<MyCommand, MyCommand.MyHandler>();
46+
});
5047
})
5148
.Build();
5249

@@ -60,8 +57,8 @@ public static async Task Can_have_different_handlers_based_on_command()
6057
{
6158
var root = new RootCommand();
6259

63-
root.Subcommands.Add(new MyCommand());
64-
root.Subcommands.Add(new MyOtherCommand());
60+
root.Subcommands.Add(new MyCommand().UseCommandHandler<MyCommand.MyHandler>());
61+
root.Subcommands.Add(new MyOtherCommand().UseCommandHandler<MyOtherCommand.MyHandler>());
6562
var config = new CommandLineBuilder(root)
6663
.UseHost(host =>
6764
{
@@ -71,9 +68,7 @@ public static async Task Can_have_different_handlers_based_on_command()
7168
{
7269
Action = () => 100
7370
});
74-
})
75-
.UseCommandHandler<MyCommand, MyCommand.MyHandler>()
76-
.UseCommandHandler<MyOtherCommand, MyOtherCommand.MyHandler>();
71+
});
7772
})
7873
.Build();
7974

@@ -91,15 +86,14 @@ public static async Task Can_bind_to_arguments_via_injection()
9186
{
9287
var service = new MyService();
9388
var cmd = new RootCommand();
94-
cmd.Subcommands.Add(new MyOtherCommand());
89+
cmd.Subcommands.Add(new MyOtherCommand().UseCommandHandler<MyOtherCommand.MyHandler>());
9590
var config = new CommandLineBuilder(cmd)
9691
.UseHost(host =>
9792
{
9893
host.ConfigureServices(services =>
9994
{
10095
services.AddSingleton<MyService>(service);
101-
})
102-
.UseCommandHandler<MyOtherCommand, MyOtherCommand.MyHandler>();
96+
});
10397
})
10498
.Build();
10599

@@ -114,16 +108,14 @@ public static async Task Invokes_DerivedClass()
114108
var service = new MyService();
115109

116110
var cmd = new RootCommand();
117-
cmd.Subcommands.Add(new MyCommand());
118-
cmd.Subcommands.Add(new MyOtherCommand());
111+
cmd.Subcommands.Add(new MyCommand().UseCommandHandler<MyCommand.MyDerivedHandler>());
112+
cmd.Subcommands.Add(new MyOtherCommand().UseCommandHandler<MyOtherCommand.MyDerivedHandler>());
119113
var config = new CommandLineBuilder(cmd)
120114
.UseHost((builder) => {
121115
builder.ConfigureServices(services =>
122116
{
123117
services.AddTransient(x => service);
124-
})
125-
.UseCommandHandler<MyCommand, MyCommand.MyDerivedHandler>()
126-
.UseCommandHandler<MyOtherCommand, MyOtherCommand.MyDerivedHandler>();
118+
});
127119
})
128120
.Build();
129121

0 commit comments

Comments
 (0)