Question: RunCommandLineApplicationAsync only supports attribute command line configuration?
Is your feature request related to a problem? Please describe. In order to create a CLI extensible commands and subcommands
Describe the solution you'd like I want to use a version of RunCommandLineApplicationAsync for command line configurations that use the Builder API
Describe alternatives you've considered
Whereas currently RunCommandLineApplicationAsync injects a CommandLineService
Additional context Actually I want to confirm that I need to create an alternative to RunCommandLineApplicationAsync which uses configuration based on the Builder API. Am I correct?
Although if I do it, I may be able to contribute to the repo. Interested?
Yes, I would be open to a contribution to this. What do you think the API should look like for users? Maybe something like this?
var app = new CommandLineApplication();
var hostBuilder = new HostBuilder();
// configure it
hostBuilder.RunCommandLineApplicationAsync(app);
or maybe
new HostBuilder()
.ConfigureCommandLineApplication(app =>
{
app.Option("-a|--abc", "blah blah");
})
.RunAsync();
My preference is for the second variant as it lines up well with the common configuration style.
I have the opposite problem, how do I use RunCommandLineApplicationAsync with my attributes? There is no sample or docs for this..
My bad sample is here (still docs would be nice): https://github.com/natemcmaster/CommandLineUtils/blob/master/docs/samples/attributes/Program.cs
static Task<int> Main(string[] args)
{
return CommandLineApplication.ExecuteAsync<Program>(args);
}
private async Task<int> OnExecuteAsync(CommandLineApplication app, CancellationToken cancellationToken = default)
{
https://natemcmaster.github.io/CommandLineUtils/docs/advanced/generic-host.html
@natemcmaster I'm ready to work on this feature
@natemcmaster I have a few questions:
Assuming that the new API looks like:
new HostBuilder()
.ConfigureCommandLineApplication(app =>
{
app.Option("-a|--abc", "blah blah");
})
.RunAsync();
- What
ConfigureCommandLineApplicationis supposed to return? AnIHostobject ? If so andRunAsyncis meant to beHostingAbstractionsHostExtensions.RunAsynchow the caller should get the result code of the app? OrRunAsyncis meant to be a custom extension method ofIHostBuilderthat encapsulate getting the app from the container and running it? - In
RunCommandLineApplicationAsyncmethodStoreExceptionHandlerclass is used to catch an unhandled exception and then to throw it again. Why is it required?
Good questions @vpkopylov!
What ConfigureCommandLineApplication is supposed to return?
Let's return IHost.
If so and
RunAsyncis meant to be HostingAbstractionsHostExtensions.RunAsync how the caller should get the result code of the app?
Probably yes? What do you think? I think we should stick to the conventions of GenericHost. We could provide a custom extension method if that is helpful. I haven't used generic host myself to know how people are normally using the build-in APIs Microsoft provides if they want to return an exit code. I know RunAsync and RunConsoleAsync exist, but I never looked into it.
In RunCommandLineApplicationAsync method StoreExceptionHandler class is used to catch an unhandled exception and then to throw it again. Why is it required?
This was added by @TheConstructor in https://github.com/natemcmaster/CommandLineUtils/pull/192. See the PR description for why it was added.
I was working on my imlementation of this, but was too late. I'll publish a draft PR with my version in a few hours.
Example of using my implementation:
static Task<int> Main(string[] args)
=> new HostBuilder()
.RunCommandLineApplicationFluentAsync(args, (app, provider) =>
{
var portOption = app.Option<int>("-p|--port <PORT>", "Port", CommandOptionType.SingleValue);
app.OnExecute(() =>
{
var port = portOption.HasValue() ? portOption.ParsedValue : 8080;
var env = provider.GetRequiredService<IHostingEnvironment>();
Console.WriteLine($"Starting on port {port}, env = {env.EnvironmentName}");
});
});
All of the logic is in the RunCommandLineApplicationFluentAsync extension method. I don't see a strong reason to implement a separate method just for configuring since we can't use HostingAbstractionsHostExtensions.RunAsync directly and have to provide a custom wrapper instead for extracting the return code.
Thanks for the PR, @vpkopylov. I see your point about HostingAbstractionsHostExtensions.RunAsync not working. Can we tweak your proposal a little? I think I'd like to see usage like this:
static Task<int> Main(string[] args)
=> new HostBuilder()
.RunCommandLineApplicationAsync(args, app =>
{
var portOption = app.Option<int>("-p|--port <PORT>", "Port", CommandOptionType.SingleValue);
app.OnExecute(() =>
{
var port = portOption.HasValue() ? portOption.ParsedValue : 8080;
IServiceProvider provider = app;
var env = provider.GetRequiredService<IHostingEnvironment>();
Console.WriteLine($"Starting on port {port}, env = {env.EnvironmentName}");
});
});
Can we tweak your proposal a little?
I made some changes and left a few questions, one of them concerning the name of the new method. Ready to improve the PR further.