You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
System.CommandLine is a nice library that convers a lot of edge cases, but in my opinion creating commands (adding handlers) is quite verbose and adds a lot of boilerplate code to the main function.
What I was thinking is that maybe it could be possible to add some extension methods to make creating commands more simple. For example, in my project I have something like this:
privatestaticasyncTask<int>Main(string[]args){varrootCommand=newRootCommand{newCommandBuilder("do-something","Does something").WithOption("--type",CommandLineUtils.GetType).WithOption<bool>("--run-migrations").Build(DoSomethingAsync),newCommandBuilder("do-another-thing","Does another thing").WithArgument<string>().Build(DoAnotherThingAsync),};returnawaitrootCommand.InvokeAsync(args).ConfigureAwait(false);}privatestaticasyncTaskDoSomethingAsync(SomeTypetype,boolrunMigrations){awaitusingvarservices=BuildServiceProvider();if(runMigrations)services.RunMigrations();awaitservices.GetRequiredService<ISomethingService>().DoSomethingAsync(type);}privatestaticasyncTaskDoAnotherThingAsync(stringarg){awaitusingvarservices=BuildServiceProvider();awaitservices.GetRequiredService<IAnotherService>().DoSomethingElseAsync(arg);}
The CommandBuilder is a class that encapsulated the boiler plate code. For example:
And CommandHandler1Builder<T>, CommandHandler2Builder<T1, T2>, CommandHandler3Builder<T1, T2, T3>, etc. are used to create handlers, arguments and options. For example:
System.CommandLine is a nice library that convers a lot of edge cases, but in my opinion creating commands (adding handlers) is quite verbose and adds a lot of boilerplate code to the main function.
What I was thinking is that maybe it could be possible to add some extension methods to make creating commands more simple. For example, in my project I have something like this:
The
CommandBuilder
is a class that encapsulated the boiler plate code. For example:And
CommandHandler1Builder<T>
,CommandHandler2Builder<T1, T2>
,CommandHandler3Builder<T1, T2, T3>
, etc. are used to create handlers, arguments and options. For example:Would it be something that you can be interested in? If yes, I might work on this feature and prepare a PR.
The text was updated successfully, but these errors were encountered: