Cysharp / ConsoleAppFramework

Zero Dependency, Zero Overhead, Zero Reflection, Zero Allocation, AOT Safe CLI Framework powered by C# Source Generator.
MIT License
1.54k stars 88 forks source link

[Help wanted] Prevent the console app to got terminated after the Run method call #92

Closed nilobaz closed 1 year ago

nilobaz commented 1 year ago

So, I'm testing the very friendly and initial examples like that:

var app = ConsoleApp.Create(args);
app.AddCommands<Foo>();
app.Run();

But when I build/run the app, my console just got terminated and I didn't have a chance to make any input. I tried to use Console.ReadLine() right after the app.Run() but it is not working and I didn't get the command response either.

The only way I was able to see it working was by calling this in the terminal at solution directory e.g. dotnet run -- sum 2 2

I would like to have something I could debug, is there a proper way or any tips to achieve that, please? I mean, something to keep console app open and just terminated with some command like e.g. --exit.

neuecc commented 1 year ago

If an argument is wrong, invalid, etc., do you get any kind of message? Also, if Logging Level is set to Trace, more internal information is output.

nilobaz commented 1 year ago

Thanks for the update! I added ZLoger to it, and now we're getting more info.

When I use dotnet run -- sum z x it works as expected and we get the right information.

dotnet run -- sum z x
Hosting starting
ConsoleAppEngine.Run Start
Parameter "x" fail on JSON deserialize, please check type or JSON escape or add double-quotation. args: sum z x
Hosting started
Hosting stopping
Hosting stopped

But unfortunately, when I try to debug, it seems that the hosting got stopped right after the app.Run(). I tried to use Console.ReadLine() to send commands but without success, I got no output response. I guess it is because the hosting has stopped. I'm not sure if there's a way to prevent that and just let it stop when calling an exit command for example.

Hosting starting
ConsoleAppEngine.Run Start
Usage: test <Command>

Commands:
  echo      
  help       Display help.
  sum        
  version    Display version.

ConsoleAppEngine.Run Complete Successfully
Hosting started
Hosting stopping
Hosting stopped
test sum 2 3                 // no output msg
test sum z x                // no output msg
foo sum 2 3                // no output msg
foo sum z x               // no output msg
neuecc commented 1 year ago

I'm not sure what you want to do or what the problem is, maybe you want to give parameters in Console.ReadLine?

If so, create new args...

var line = Console.ReadLine();
var newArg = line!.Split(' ');

var app = ConsoleApp.Create(newArg);
app.AddCommands<Foo>();
app.Run();
nilobaz commented 1 year ago

Yeah, that helps. I was calling Console.ReadLine after the app.Run(). I think I misunderstood how it works and I was expecting the console app to not get terminated after app.Run(). My apologies, and thanks for the help!