bilal-fazlani / commanddotnet

A modern framework for building modern CLI apps
https://commanddotnet.bilal-fazlani.com
MIT License
560 stars 32 forks source link

Can't inherit command methods defined in parent class. #427

Closed sanitycheck closed 2 years ago

sanitycheck commented 2 years ago

Hi there, Thank you for your work on this great library! I am trying to use it in a project and create multiple command menus for different sets of features. However, I would like each menu to inherit certain core functions. For Example, I have the following parent class:

public class CoreCli {
      [Command(Name = "exit", Description = "Exit the console")]
      public void Exit()
      {
          Environment.Exit(0);
      }
    }

And I would like my AutomationCli class to inherit the 'exit' function.

 public class AutomationCli : CoreCli {
   [Command(Name = 'runbooklet', Description = 'runs instruction booklet' )]
   public void RunBooklet(string filename) 
   {
     /*do stuff*/
    }
 }

but when I use the following code to startup the application the commands defined in CoreCli are not available and do not show up in the help menu only the commands defined explicitly in AutomationCli

            var app = new AppRunner<AutomationCli>();
            try
            {
                app.Run(args);
            }
            catch (Exception)
            {
                console.MarkupLine("[red][[!]][/] Invalid command syntax");
            }

Help Expected:

Usage: [command]

Commands:

  exit  Exit the console
  runbooklet  runs instruction booklet

Help Produced:

Usage: [command]

Commands:

  runbooklet  runs instruction booklet
drewburlingame commented 2 years ago

[Hi @sanitycheck. Thanks for posting. This was an intentional choice at the time as we favor composition over inheritance.. However, I'm amenable to having this functionality added. I've created this PR to spike out the implementation. It's pretty simple, but requires tests and updated docs. I'm starting a new gig so it will take me a week or two to wrap this up.

If you have time, the test would be a new class in the same dir as DefaultCommandMethodTests. The doc to update is https://github.com/bilal-fazlani/commanddotnet/blob/master/docs/Commands/commands.md and https://github.com/bilal-fazlani/commanddotnet/blob/master/docs/ReleaseNotes/CommandDotNet.md.

drewburlingame commented 2 years ago

@sanitycheck https://www.nuget.org/packages/CommandDotNet/6.0.5 this has been pushed. See release note for how to enable.