commandline icon indicating copy to clipboard operation
commandline copied to clipboard

Increase the number of verbs supported

Open unwobbling opened this issue 1 year ago • 3 comments

Would it be a possible to remove the limit on the number of verbs supported?

Currently the limit is 16.

Ideal would be no limit if that's not possible due to the implementation 32 would be great.

In:

ParserExtensions .ParseArguments<>

ParserResultExtensions .MapResult()

unwobbling avatar Feb 15 '24 20:02 unwobbling

I just searched for this topic at Stackoverflow and unlimited / dynamic number is not possible.

But I would like to have more than 16 parameters, too. 32 would be really nice.

quotschmacher avatar Feb 23 '24 09:02 quotschmacher

This is already possible. I stumpled upon this while trying to use dependency injection with commandlineparser. I dont think that there is a real limit on the number of verbs by doing it this way:

using CommandLine;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

var services = new ServiceCollection()
.AddTransient<IController<FooOptions>, FooController>()
// ...
.AddTransient<IController<FooOptions17>, FooController17>()
.BuildServiceProvider();

Type[] optionTypes = [
    typeof(FooOptions),
// ...
    typeof(FooOptions17),
];

Parser.Default.ParseArguments(args, optionTypes)
.MapResult((IOptions o) => {
    Type t = typeof(IController<>).MakeGenericType(o.GetType());
    var controller = services.GetRequiredService(t);
    return t.GetMethod(nameof(IController<IOptions>.Run))?.Invoke(controller, [o]);
},
errs => 1);

Roiskia avatar Mar 03 '24 06:03 Roiskia