我的 System.CommandLine 应用程序无法构建!它找不到 CommandHandler。我需要写吗?

Haw*_*ale 14 c# system.commandline

我正在使用 VS 2022、.Net 6.0,并尝试使用 .Net 构建我的第一个应用程序System.CommandLine

问题:当我构建它时,出现错误

当前上下文中不存在名称“CommandHandler”

我尝试构建的代码是来自 GitHub 站点的示例应用程序:https ://github.com/dotnet/command-line-api/blob/main/docs/Your-first-app-with-System-CommandLine .md,无需更改(我认为)。

它看起来像这样:

using System;
using System.CommandLine;
using System.IO;

static int Main(string[] args)
{
    // Create a root command with some options
    var rootCommand = new RootCommand
        { 
        new Option<int>(
            "--int-option",
            getDefaultValue: () => 42,
            description: "An option whose argument is parsed as an int"),
        new Option<bool>(
            "--bool-option",
            "An option whose argument is parsed as a bool"),
        new Option<FileInfo>(
            "--file-option",
            "An option whose argument is parsed as a FileInfo")
    };

    rootCommand.Description = "My sample app";

    // Note that the parameters of the handler method are matched according to the names of the options
    rootCommand.Handler = CommandHandler.Create<int, bool, FileInfo>((intOption, boolOption, fileOption) =>
    {
        Console.WriteLine($"The value for --int-option is: {intOption}");
        Console.WriteLine($"The value for --bool-option is: {boolOption}");
        Console.WriteLine($"The value for --file-option is: {fileOption?.FullName ?? "null"}");
    });

    // Parse the incoming args and invoke the handler
    return rootCommand.InvokeAsync(args).Result;
}
Run Code Online (Sandbox Code Playgroud)

我已经安装了最新版本System.Commandline:2.0.0-beta2.21617.1

当然,在某些方面我只是个大白痴。但我没有看到。

任何见解都会受到欢迎。

Ser*_*can 24

此问题是由更新CommandLine 2.0 Beta 2软件包引起的。将参考添加System.CommandLine.NamingConventionBinder到参考文献中以解决问题。请遵循 command-line-api 的 GitHub 帐户上的公告:

\n
\n

在您的项目中,添加对 System.CommandLine.NamingConventionBinder 的引用。

\n

在代码中,将对 System.CommandLine.Initation 命名空间的引用更改为\n使用 System.CommandLine.NamingConventionBinder,其中现在可找到 CommandHandler.Create\n方法。(\nSystem.CommandLine 中 \xe2\x80\x99 不再是 CommandHandler 类型,因此更新后,\xe2\x80\x99 将出现编译错误,直到\n引用 System.CommandLine.NamingConventionBinder。)

\n
\n

如果您想继续旧习惯,请尝试使用旧版本的软件包System.CommandLine

\n
\n
参考
\n\n

  • 是的!正如您所指出的,问题在于 CommandHandler 已移至 System.CommandLine.NamingConventionBinder 。更令人困惑的是,System.CommandLine.NamingConventionBinder 现在有一个单独的 nuGet 包,必须单独安装它。谢谢你,我是做生意的。这就是我们使用测试版工具得到的结果...但是这个工具非常有用 (3认同)
  • 哎呀,我当然一问就回答。它位于单独的 NuGet 包中:https://www.nuget.org/packages/System.CommandLine.NamingConventionBinder (2认同)