【问题标题】:My System.CommandLine app won't build! It can't find a CommandHandler. Do I need to write it?我的 System.CommandLine 应用程序无法构建!它找不到 CommandHandler。我需要写吗?
【发布时间】:2021-12-17 23:10:19
【问题描述】:

我正在使用 VS 2022、.Net 6.0,并尝试使用 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;
}

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

肯定在某些方面我只是个大白痴。但我没看到。

欢迎任何见解。

【问题讨论】:

  • 我相信,查看 Github 上的示例,您只是在代码文件顶部缺少 using System.CommandLine.Invocation; ...

标签: c# system.commandline


【解决方案1】:

这个问题是由更新CommandLine 2.0 Beta 2 包引起的。将引用 System.CommandLine.NamingConventionBinder 添加到引用以解决问题。关注 command-line-api 的 GitHub 账号上的公告:

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

在您的代码中,将对 System.CommandLine.Invocation 命名空间的引用更改为 使用 System.CommandLine.NamingConventionBinder,其中 CommandHandler.Create 现在找到方法了。 (不再有 CommandHandler 类型 System.CommandLine,所以在你更新之后你会得到编译错误,直到你 参考 System.CommandLine.NamingConventionBinder。)

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


参考文献

【讨论】:

  • 是的!正如您所指出的,问题在于 CommandHandler 已移至 System.CommandLine.NamingConventionBinder 。更令人困惑的是,现在有一个单独的用于 System.CommandLine.NamingConventionBinder 的 nuGet 包,它必须单独安装。谢谢你,我在做生意。这就是我们使用 beta 工具得到的结果......但是这个工具非常有用
【解决方案2】:

认为您缺少using 行:

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

我不能发誓就是这样,但看起来CommandHandler 是在未被using 引用的命名空间中定义的(在您当前的代码中),所以System.CommandLine.Invocation 可能是关键!

【讨论】:

  • 谢谢,我也试过了,但愚蠢地失败了,它把它放到了示例代码中。事实证明,家具已经重新排列,我想这是在预发布代码中的预期结果。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多