Skip to content

Fix #1209 #1706

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions src/System.CommandLine.Hosting.Tests/HostingHandlerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,50 @@ public static async Task Can_bind_to_arguments_via_injection()
service.StringValue.Should().Be("TEST");
}

[Fact]
public static async Task Invokes_DerivedClass()
{
var service = new MyService();

var cmd = new RootCommand();
cmd.AddCommand(new MyCommand());
cmd.AddCommand(new MyOtherCommand());
var parser = new CommandLineBuilder(cmd)
.UseHost((builder) => {
builder.ConfigureServices(services =>
{
services.AddTransient(x => service);
})
.UseCommandHandler<MyCommand, MyCommand.MyDerivedHandler>()
.UseCommandHandler<MyOtherCommand, MyOtherCommand.MyDerivedHandler>();
})
.Build();

await parser.InvokeAsync(new string[] { "mycommand", "--int-option", "54" });
service.Value.Should().Be(54);

await parser.InvokeAsync(new string[] { "myothercommand", "TEST" });
service.StringValue.Should().Be("TEST");
}

public abstract class MyBaseHandler : ICommandHandler
{
public int IntOption { get; set; } // bound from option
public IConsole Console { get; set; } // bound from DI

public int Invoke(InvocationContext context)
{
return Act();
}

public Task<int> InvokeAsync(InvocationContext context)
{
return Task.FromResult(Act());
}

protected abstract int Act();
}

public class MyCommand : Command
{
public MyCommand() : base(name: "mycommand")
Expand Down Expand Up @@ -144,6 +188,22 @@ public Task<int> InvokeAsync(InvocationContext context)
return Task.FromResult(IntOption);
}
}

public class MyDerivedHandler : MyBaseHandler
{
private readonly MyService service;

public MyDerivedHandler(MyService service)
{
this.service = service;
}

protected override int Act()
{
service.Value = IntOption;
return IntOption;
}
}
}

public class MyOtherCommand : Command
Expand Down Expand Up @@ -177,6 +237,25 @@ public Task<int> InvokeAsync(InvocationContext context)
return Task.FromResult(service.Action?.Invoke() ?? 0);
}
}

public class MyDerivedHandler : MyBaseHandler
{
private readonly MyService service;

public MyDerivedHandler(MyService service)
{
this.service = service;
}

public string One { get; set; }

protected override int Act()
{
service.Value = IntOption;
service.StringValue = One;
return service.Action?.Invoke() ?? 0;
}
}
}

public class MyService
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public override ICommandHandler GetCommandHandler()
}
}

public override ModelDescriptor Parent => ModelDescriptor.FromType(_handlerMethodInfo.DeclaringType);
public override ModelDescriptor Parent => ModelDescriptor.FromType(_handlerMethodInfo.ReflectedType);

private protected override IEnumerable<ParameterDescriptor> InitializeParameterDescriptors() =>
_handlerMethodInfo.GetParameters()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public async Task<int> InvokeAsync(InvocationContext context)
if (_handlerDelegate is null)
{
var invocationTarget = _invocationTarget ??
bindingContext.GetService(_handlerMethodInfo!.DeclaringType);
bindingContext.GetService(_handlerMethodInfo!.ReflectedType);
if(invocationTarget is { })
{
_invocationTargetBinder?.UpdateInstance(invocationTarget, bindingContext);
Expand Down