CommandLineUtils icon indicating copy to clipboard operation
CommandLineUtils copied to clipboard

Question: RunCommandLineApplicationAsync only supports attribute command line configuration?

Open clavecoder opened this issue 7 years ago • 12 comments

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 which news up a CommandLineApplication that uses attribute driven command line configuration

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?

clavecoder avatar Sep 02 '19 19:09 clavecoder

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();

natemcmaster avatar Sep 04 '19 00:09 natemcmaster

My preference is for the second variant as it lines up well with the common configuration style.

AngusMcIntyre avatar Oct 21 '19 15:10 AngusMcIntyre

I have the opposite problem, how do I use RunCommandLineApplicationAsync with my attributes? There is no sample or docs for this..

markmnl avatar Oct 31 '19 02:10 markmnl

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)
        {

markmnl avatar Oct 31 '19 02:10 markmnl

https://natemcmaster.github.io/CommandLineUtils/docs/advanced/generic-host.html

natemcmaster avatar Oct 31 '19 03:10 natemcmaster

@natemcmaster I'm ready to work on this feature

vpkopylov avatar Jan 06 '20 16:01 vpkopylov

@natemcmaster I have a few questions:

Assuming that the new API looks like:

new HostBuilder()
  .ConfigureCommandLineApplication(app =>
  {
      app.Option("-a|--abc", "blah blah");
  })
  .RunAsync();
  1. What ConfigureCommandLineApplication is supposed to return? An IHost object ? If so and RunAsync is meant to be HostingAbstractionsHostExtensions.RunAsync how the caller should get the result code of the app? Or RunAsync is meant to be a custom extension method of IHostBuilder that encapsulate getting the app from the container and running it?
  2. In RunCommandLineApplicationAsync method StoreExceptionHandler class is used to catch an unhandled exception and then to throw it again. Why is it required?

vpkopylov avatar Jan 12 '20 21:01 vpkopylov

Good questions @vpkopylov!

What ConfigureCommandLineApplication is supposed to return?

Let's return IHost.

If so and RunAsync is 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.

natemcmaster avatar Feb 06 '20 06:02 natemcmaster

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.

vpkopylov avatar Mar 03 '20 05:03 vpkopylov

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.

vpkopylov avatar Mar 03 '20 20:03 vpkopylov

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}");
            });
        });

natemcmaster avatar Mar 09 '20 01:03 natemcmaster

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.

vpkopylov avatar Mar 09 '20 21:03 vpkopylov