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.