【发布时间】: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