spectre.console icon indicating copy to clipboard operation
spectre.console copied to clipboard

Branch + AddAsyncDelegate causes MissingMethodException

Open dotTrench opened this issue 11 months ago • 0 comments

Information

  • Version: 0.49.1

Describe the bug AddAsyncDelegate without custom settings throws CommandRuntimeException on invocation. Spectre.Console.Cli.CommandRuntimeException: Could not resolve type 'Spectre.Console.Cli.CommandSettings'. ---> System.MissingMethodException: Cannot dynamically create an instance of type 'Spectre.Console.Cli.CommandSettings'. Reason: Cannot create an abstract class.

To Reproduce

using Spectre.Console.Cli;

var app = new CommandApp();

app.Configure(cfg =>
{
    cfg.PropagateExceptions();
    cfg.AddBranch("sample", s =>
    {
        s.AddAsyncDelegate("async-test", async context =>
        {
            await Console.Out.WriteLineAsync("Hello from async delegate");
            return 0;
        });
        s.AddDelegate("sync-test", context =>
        {
            Console.WriteLine("Hello from sync delegate");
            return 0;
        });
    });
});

return await app.RunAsync(args);

Invoking the program using sample async-test throws an exception, while invoking using sample sync-test prints "Hello from sync delegate".

Expected behavior AddAsyncDelegate and AddDelegate should behave in the same way, i.e. just invoke the provided delegate

Additional context

As a workaround it's possible to call AddBranch<EmptyCommandSettings>(...) and get the expected behavior. AddDelegate currently performs a type check using Type.IsAbstract here to fix #1507. while no such check is done when configuring an async delegate.


Please upvote :+1: this issue if you are interested in it.

dotTrench avatar Dec 22 '24 18:12 dotTrench