Description
openedon Apr 27, 2021
Both Argument
and Option
implement IValueDescriptor
, which allows treating both in a generic fashion for most processing.
It would be useful (given that HasDefaultValue
and GetDefaultValue
are already on the interface) to also add SetDefaultValue
and SetDefaultValueFactory
to IValueDescriptor
so setting default values can also be done uniformly for both.
In the case of the Option
, that would naturally delegate to the inner argument for it, so it should be straightforward to implement.
My use case: I'm adding an extension method on Command
that allows setting default value factories for all arguments and options (for nested commands too) so they can pull the values from a dotnet-config file, and currently I have to depend on internals knowledge to set that default value factory for options. (I get the options' Children.OfType<Argument>().FirstOrDefault()
, which is super hacky).
My ideal code would just look like:
public static T AddDefaults<T>(this T command, Config config) where T : Command
{
foreach (var descriptor in command.OfType<IValueDescriptor>())
{
if (!arg.HasDefaultValue)
arg.SetDefaultValueFactory(// get default from config);
}
}