CommandLineUtils
CommandLineUtils copied to clipboard
Sub-command async OnExecute
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;
}
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.
You are right, thanks a lot for the explanation.
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.