System.CommandLine 异常处理

System.CommandLine Exception Handling

是否有关于使用 System.CommandLine 进行异常处理的最佳实践的文档?

我通过 CommandHandler.Create 和 return 来自我的控制台应用程序的 InvokeAsync 结果构建了一个处理程序。

如何向我的应用程序用户报告异常情况?如果我在我的处理程序中 try/catch/log 并设置 Environment.ExitCode,它当然会被忽略,因为我正在 returning InvokeAsync 的结果并且我的处理程序没有 return 任何东西但是一个任务。

returning 非零退出代码的推荐模式是什么?

CommandHandler.Create 方法有一些重载,您可以在其中 return 一个 int 用作 ExitCode

public static class CommandHandler
{
    ...
    public static ICommandHandler Create(Func<int> action) =>
        HandlerDescriptor.FromDelegate(action).GetCommandHandler();

    public static ICommandHandler Create<T>(
        Func<T, int> action) =>
        HandlerDescriptor.FromDelegate(action).GetCommandHandler();
    ...
}

https://github.com/dotnet/command-line-api/blob/master/src/System.CommandLine/Invocation/CommandHandler.cs