spectreconsole / spectre.console

A .NET library that makes it easier to create beautiful console applications.

Home Page:https://spectreconsole.net

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Task< SomeReturnInfoClass > state = _app.RunAsyncWithState(args)

carsten-riedel opened this issue · comments

Is your feature request related to a problem? Please describe.
Well the RunAsync command does not properly describe what happened inside the CommandApp it just gives a exitcode.
-1 on Command not found
0 on Help displayed
0 on Version Displayed
0 on Command successful (somehow standard)
all othere states.....
(Duplicates here)

Describe the solution you'd like
I want to determinate in some way if help was displayed or command was successful.
Task< SomeReturnInfoClass > state = _app.RunAsyncWithState(args); // just a simple state like -> exitcode, helpdisplayed, customcommandexectuted,VersionDisplayed, commandnotfound whatever.
Maybe returning -2 if just help is displayed and -3 on Version displayed is suitable. (depends on what's the real meaning of -1)

Describe alternatives you've considered
My command basicly returns 1 as successful execution.

        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            await WaitForApplicationStartAsync(appLifetime.ApplicationStarted);
            var args = Environment.GetCommandLineArgs()[1..];

            //Exitcode 0 will be returned if just help is displayed, Exitcode -1 will be returned if command is not found.
            CommandAppExitCode = await _app.RunAsync(args);

            if (CommandAppExitCode == 0 || CommandAppExitCode == -1)
            {
                appLifetime.StopApplication();
                Environment.ExitCode = CommandAppExitCode;
            }

            if (CommandAppExitCode == 1)
            {
                Environment.ExitCode = 0;
            }
            try
            {
                while (!stoppingToken.IsCancellationRequested)
                {
                        await Task.Delay(500, stoppingToken);
                }
            }
            catch (Exception ex)
            {
            }
        }

Additional context
As you can see in my example i am running the command as a background service, service should continue under some circumstances. Main problem is here if the help is displayed it should stop execution.


Please upvote 👍 this issue if you are interested in it.