CommandLineUtils icon indicating copy to clipboard operation
CommandLineUtils copied to clipboard

Sub-command async OnExecute

Open rychlym opened this issue 8 years ago • 3 comments

Hello,

is it a bug, that program is taken the control before async sub-command OnExecute is done?

a demo example:

static int Main(string[] args)
        {
            var app = new CommandLineApplication();
        app.HelpOption("-h|--help");
        var optionSubject = app.Option("-s|--subject <SUBJECT>", "The subject", CommandOptionType.SingleValue);

        app.Command("subCmd", cmd =>
        {
            cmd.FullName = "Sub-command";
            cmd.Description = "Demo sub-command";
            cmd.ExtendedHelpText = "An extended help text...";
            cmd.HelpOption();
            var subCmdArg = cmd.Argument("SubArg", "The argument");

            cmd.OnExecute(async () =>
            {
                Console.WriteLine("sub-command starting ...");
                await Task.Delay(4000);
                Console.WriteLine("... sub-command: done - arg: " + subCmdArg.Value);
            });
        });

        app.OnExecute(async () =>
        {
            var subject = optionSubject.HasValue()
                ? optionSubject.Value()
                : "world";

            Console.Write($"Hello");

            // This pause here is just for indication that some awaitable operation could happens here.
            await Task.Delay(5000);
            Console.WriteLine($" {subject}!");
            return 0;
        });

        var result = app.Execute(args);
       Console.WriteLine("Press any key to end...");
        Console.ReadKey();
       return result;
     }

rychlym avatar Feb 19 '18 06:02 rychlym

You are probably hitting #46. The compiler picks the wrong overload of OnExecute when you don't return int. I added a new overload that fixes this in https://github.com/natemcmaster/CommandLineUtils/commit/8a2f470b7dec9dc9d4b39278e82b23841f0bb4dd.

natemcmaster avatar Feb 20 '18 04:02 natemcmaster

You are right, thanks a lot for the explanation.

rychlym avatar Feb 20 '18 05:02 rychlym

Ok. This should be fixed in 2.2.0-alpha which I pushed not too long ago to nuget.org. It should be available soon.

natemcmaster avatar Feb 20 '18 05:02 natemcmaster