In my console application, whichever handler I specify list (parameter 3 in this case) will be executed when no console arguments are provided. The handler is called with the boolean parameter set to false. But it makes more sense to me for it to not be called at all.
How do I prevent this from happening and instead show me the help text?
using System;
using System.CommandLine;
namespace GlideRush.LeaderboardsConsole // Note: actual namespace depends on the project name.
{
internal class Program
{
static async Task<int> Main(string[] args)
{
var test1 = new Option<bool>(
name: "test1",
description: "test param 1");
var test2 = new Option<bool>(
name: "test2",
description: "test param 2");
var test3 = new Option<bool>(
name: "test3",
description: "test param 3");
var rootCommand = new RootCommand("Testing console params");
rootCommand.AddOption(test1);
rootCommand.AddOption(test2);
rootCommand.AddOption(test3);
rootCommand.SetHandler(boolparam => Console.WriteLine($"Param 1 {boolparam}"), test1);
rootCommand.SetHandler(boolparam => Console.WriteLine($"Param 2 {boolparam}"), test2);
rootCommand.SetHandler(boolparam => Console.WriteLine($"Param 3 {boolparam}"), test3);
return await rootCommand.InvokeAsync(args);
}
}
}
When I provide no parameters, it outputs:
$ dotnet run
Param 3 False
I would like it to output the default:
Description:
Testing console params
Usage:
GlideRushConsole [options]
Options:
test1 test param 1
test2 test param 2
test3 test param 3
--version Show version information
-?, -h, --help Show help and usage information
It does not feel right to have the order of handlers determine the default behaviour, nor do I want to check for the bool to be false on every parameter handler. And in any case that still doesn't give me the default help menu.
CodePudding user response:
A single command can only have one handler as Panagiotis Kanavos already stated.
If you want different handlers based on a parameter you probably are looking for subcommands or verbs.
for example (freehand code)
var smoothOrNot = new Option<bool>(
name: "smooth",
description: "smooth gliding or nah?");
var fastOrNot = new Option<bool>(
name: "fast",
description: "fast gliding or prefer slow");
var glideRushCommand = new RootCommand();
var glideVerb = new Command("glide", "glide subcommand");
var rushVerb = new Command("rush", "rush subcommand");
glideVerb.Add(glideOptionOne);
glideVerb.Add(glideOptionTwo);
glideRushCommand.Add(glideVerb);
CodePudding user response:
A command can have only a single handler at a time. Calling SetHandler
three times replaced the previous handlers so only the last one is used.
To handle 3 parameters, use an action with three parameters:
rootCommand.SetHandler((b1,b2,b2)=>{
Console.WriteLine($"Param 1 {b1}");
Console.WriteLine($"Param 2 {b2}");
Console.WriteLine($"Param 3 {b3}");
}, test1, test2, test3);
Better yet, use a separate method :
rootCommand.SetHandler(DoSomething, test1, test2, test3);
...
void DoSomething(bool? b1,bool? b2, bool? b3)
{
Console.WriteLine($"Param 1 {b1}");
Console.WriteLine($"Param 2 {b2}");
Console.WriteLine($"Param 3 {b3}");
};