Skip to content

Consider adding SetDefaultValue and SetDefaultValueFactory to IValueDescriptor #1263

Closed

Description

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);
      }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions