Cocona
Cocona copied to clipboard
Global option/flag?
I want to control the loglevel based on a command line argument, for example if I provide -v
the loglevel would be set to Information.
I didn't find anything in Cocona that would let me do that, besides adding that option to every single command or parsing the command line arguments manually.
What I would like to do is along the lines of:
app.AddFlag("v", () => builder.Logging.SetMinimumLevel(LogLevel.Information))
.WithDescription("verbose");
or
app.AddGlobal(([Option("v", "verbose")]bool verbose) => if (verbose) builder.Logging.SetMinimumLevel(LogLevel.Information));
Both would be run like a filter before any command, but the difference is that they add options, and would show up in the help like so:
Usage: SubCommandApp [command] [-v]
Usage: SubCommandApp [--help] [--version] [-v]
...
Options:
-h, --help Show help message
-v (Global) verbose
--version Show version
I believe that you are looking for Parameter Sets.
The point with parameter sets is that I have to repeat the logic in every command, which is something I wanted to avoid.
The demand for a global flag such as -v
or --verbose
is understandable.
However, what I am wondering is how to pass the value of the global flag to the application. This is not a command, nor is it an option of a command. It is more like a filter/middleware before the command is executed.
Yes, essentially a middleware that can specify options.
How about passing the Parameter Set to the command filters? For example as a property of CoconaCommandExecutingContext
.
That would solve the specific problem, but would feel less elegant than defining a middleware because I'd still have unused parameters in every command definition, where it's not immediately clear why they are even there.
In the library System.CommandLine designed by Microsoft has a definition of RootCommand that works as simple as a normal command. It accepts options, arguments, sub-command, custom help or whatever a typical command can do. The most special thing seems to be that it has the first priority of an application.
Is it possible, if Cocona implements the RootCommand concept what runs before all other commands?
Code example:
var builder = CoconaApp.CreateBuilder(args, rootCommand, options);
var app = builder.Build();
// Or add root command manually
app.AddRootCommand<RootCommand>();
app.Run();