.NET System.CommandLine:通过 Parser 调用命令不会写入控制台,而 command.Invoke 会写入

问题描述 投票:0回答:1

我有一个控制台应用程序,它使用

System.CommandLine
来解析参数和选项。

为了能够更好地处理异常,我已经从 RootCommand.Invoke(args) 切换到使用这样的 CommandLineBuilder:

    CommandLineBuilder oCLB = new(oRootCmd);
    oCLB.UseExceptionHandler((e, c) => LogExceptionAndRaiseEvent(e));
    Parser oPrs = oCLB.Build();
    int iRC = oPrs.Invoke(p_StartupArgs);

现在我注意到这种调用方式不再写入控制台,无论是在出现解析错误时还是在使用默认帮助开关“-h”调用时。

我怀疑它与 Invoke 中的第二个参数有关,它可以是一个 IConsole(默认情况下为 NULL),但我没有真正的线索,也不知道该向那里传递什么。

更新: 请参阅下面的解决方案,使用一些 CommandLineBuilder 配置选项。

c# .net console-application
1个回答
0
投票

更新,解决方案是在

CommandLineBuilder
:

中添加更多“使用”配置
      CommandLineBuilder oCLB = new(oRootCmd);
      oCLB.UseHelp().
        UseVersionOption().
        UseParseErrorReporting();
      oCLB.UseExceptionHandler((e, c) => BygLog.LogExceptionAndRaiseEvent(e));
      Parser oPrs = oCLB.Build();
      int iRC = oPrs.Invoke(p_StartupArgs, new SystemConsole());

根据https://github.com/dotnet/command-line-api/issues/2188

© www.soinside.com 2019 - 2024. All rights reserved.