Closed JacobCZ closed 2 months ago
@FrankRay78 Do you know if any of the work we've done lately might have affected this?
It's be a long time since any core CLI changes have been made, so without stepping through the code/git history, I suspect it's been latent. Also, I've suspected for a while now, that branch behaviour, particularly branch of branch of branch, is not as thoroughly used/test covered, compared to default command, first level commands. I would guess there are other 'edge cases' like this, lying latent.
@FrankRay78 @patriksvensson
Looking at the Empty Configurator in the case you call .AddDelegate in this case
app.Configure(cfg =>
{
cfg.AddDelegate("a", _ =>
{
AnsiConsole.MarkupLine("[red]Complete[/]");
return 0;
});
});
It will default to using configurator.AddDelegate<EmptyCommandSettings>
but when called in this case
app.Configure(cfg =>
{
cfg.AddBranch("a", d =>
{
d.AddDelegate("b", _ =>
{
AnsiConsole.MarkupLine("[red]Complete[/]");
return 0;
});
});
});
It defaulted to using TSettings
even when no settings are supplied.
One possible solution is what I have attached in the diff below where we will check if the settings are abstract and if so fallback to EmptyCommandSettings.
I have added tests for these two use cases and can make a PR if this looks correct!
@@ -324,11 +324,16 @@
/// <param name="func">The delegate to execute as part of command execution.</param>
/// <returns>A command configurator that can be used to configure the command further.</returns>
public static ICommandConfigurator AddDelegate<TSettings>(
- this IConfigurator<TSettings> configurator,
+ this IConfigurator<TSettings>? configurator,
string name,
Func<CommandContext, int> func)
- where TSettings : CommandSettings
+ where TSettings : CommandSettings
{
+ if (typeof(TSettings).IsAbstract)
+ {
+ AddDelegate(configurator as IConfigurator<EmptyCommandSettings>, name, func);
+ }
+
if (configurator == null)
{
throw new ArgumentNullException(nameof(configurator));
@JacobCZ adding this should fix it in the meantime!
...
var app = new CommandApp();
app.Configure(cfg => {
cfg.AddBranch("demo", d => {
d.AddDelegate<EmptyCommandSettings>("delegate", ctx => {
AnsiConsole.MarkupLine("[red]This doesn't work[/]");
return 0;
});
});
});
...
app.Run(args);
I'd welcome a PR for this @BlazeFace, and I would prioritise its review with a view to merging.
Information
macOS 14.3
0.48.0
8.0.100
Describe the bug Using the
AddDelegate
method on a command branch produces aMissingMethodException
.To Reproduce
AddDelegate
This produces a
MissingMethodException
.Expected behavior The delegate function runs and prints "This doesn't work" in red