command-line-api icon indicating copy to clipboard operation
command-line-api copied to clipboard

(Small) API Suggestion: Make SetHandler return parent instance

Open abbottdev opened this issue 3 years ago • 0 comments

At the moment, with the new API style the recommended approach is something like:

var subCommand = new Command("subcommand")
{
     new Option<string>("-x")
};

var rootCommand = new RootCommand
{
    subCommand
};

subCommand.SetHandler(string x => {
   //Do stuff
});

But this can be quite terse, at the moment the SetHandler methods return void - if it could instead return the instance of the command the API comes alot more fluent and shorter:

var rootCommand = new RootCommand
{
   new Command("subcommand")
   {
        new Option<string>("-x")
   }
   .SetHandler((string x) => {
        //Do stuff
   });
};

Or potentially extend the Add methods on Command to include the Func<>/Command handler

var rootCommand = new RootCommand
{
   new Command("subcommand")
   {
       new Option<string>("-x"),
       (string x) => {
              //Do stuff
       }
   };
};

abbottdev avatar Apr 07 '22 13:04 abbottdev