how to get all options and values?
BackT0TheFuture opened this issue · 4 comments
Hi,
great job!
I came accross one prblem, I want to log all options and values when running,
But I didn't find it. Is there some api to do this?
thanks!
@goodtogood I'm hitting a very similar problem and below might help you but I'm hoping someone else speaks up with a better solution that works for both of us.
In my case I want to declare two options at the top level and then access their values in several subcommands of subcommands.
I see I can access the options with app.Options
but it's not straightforward and for things like a boolean option for verbose
I lose the details that it's a boolean and the values for that CommandOption is an empty array of strings with the HasValue property is set to true.
namespace Cli.Components.Commands.Configuration
{
[Command(Name = "validate")]
internal class ValidateCmd : CommandBase
{
ConfigurationCommandGroup Parent { get; }
public ValidateCmd(ILogger<ValidateCmd> logger)
{
_logger = logger;
}
protected override async Task<int> OnExecute(CommandLineApplication app)
{
// Looping through each option
foreach (var opt in app.GetOptions())
{
if (opt.HasValue())
{
Console.WriteLine(opt.OptionType);
Console.WriteLine(opt.LongName + "=" + opt.Values);
}
else
{
Console.WriteLine(opt.LongName + "= No Value");
}
}
// Another way to access the values
var options = app.GetOptions().ToDictionary(x => x.LongName, x => x.Values);
Console.WriteLine("config path = " + options["config-path"][0]);
Console.WriteLine("verbose = " + options["verbose"]);
_logger.LogInformation("Executing config validate command.");
return 0;
}
}
}
It works well, thank you so much.