-
Notifications
You must be signed in to change notification settings - Fork 416
Open
Description
Examples
// In addition to these:
T? ParseResult.GetValue<T>(Option<T> option);
new Option<T>(string name, params string[] aliases);
// etc...
// Make it possible to call them like this:
object? ParseResult.GetValue(Option option, Type valueType);
new Option(Type valueType, string name, params string[] aliases);This is somewhat similar to what we have in DI:
// We can add service like this:
services.AddSignleton<T>();
// Or this:
services.AddSingleton(Type serviceType);Justification
I am making my own library that adds proper hosting API, making using System.CommandLine similar to any regular ASP.NET project.
For example:
Program.cs
app.MapCommand("foo", ([Option] FileInfo file) =>
{
foreach (string line in File.ReadLines(file.FullName))
Console.WriteLine(line);
});ShellApplication.cs
public void MapCommand(string name, Delegate command)
{
RootCommand rootCommand = [];
ParameterInfo[] parameters = command.Method.GetParameters();
foreach (ParameterInfo parameterInfo in parameters)
{
if (parameterInfo.GetCustomAttribute<OptionAttribute>() is not null)
{
Option option = (typeof(Option<>).MakeGenericType(parameterInfo.ParameterType).GetConstructor([typeof(string), typeof(string[])])!.Invoke([name]) as Option)!;
rootCommand.Options.Add(option);
}
}
// etc...
}At first glance this looks fine – just make a generic type using reflection. Only the problem is that this is not AOT compatible and will cause exceptions. Adding alternatives that accept types as parameters would solve this.
Additional context
I've glossed over the codebase, and it doesn't look like it will be that hard to implement changes. If that's ok with you, I can prepare a PR myself.
Metadata
Metadata
Assignees
Labels
No labels