Giter VIP home page Giter VIP logo

Comments (5)

BlazeFace avatar BlazeFace commented on June 11, 2024 1

@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);

from spectre.console.

patriksvensson avatar patriksvensson commented on June 11, 2024

@FrankRay78 Do you know if any of the work we've done lately might have affected this?

from spectre.console.

FrankRay78 avatar FrankRay78 commented on June 11, 2024

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.

from spectre.console.

BlazeFace avatar BlazeFace commented on June 11, 2024

@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));

from spectre.console.

FrankRay78 avatar FrankRay78 commented on June 11, 2024

I'd welcome a PR for this @BlazeFace, and I would prioritise its review with a view to merging.

from spectre.console.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.