C# - 控制台应用程序 - 如何读取CLI选项参数
使用system.commandline v.2.0.0-beta3.22114.1
和system.commandline.commandline.namingconconcenventionbinder v.2.0.0.0.0.0.22114.1
,我正在尝试创建创建一个简单的get
命令,带有- 名称
选项。
需要该选项。它期望一个参数,并且将在console.writeline
语句中使用它。
为了组织,我正在单独的类文件中创建命令,并继承命令
类。
public class GetCommand : Command
{
public GetCommand()
:base("get", "Filters events by name.")
{
var name = new Option("--name", "The name of the event you like to fetch.")
{
IsRequired = true,
};
AddOption(name);
Handler = CommandHandler.Create((string name) => Console.WriteLine(name));
}
}
在运行应用程序时,当我使用get -name ali
命令,选项和参数运行可执行文件时,我一直在获得以下例外: 未识别的命令或参数'ali'。
我在做什么错?
Using the System.CommandLine v.2.0.0-beta3.22114.1
and System.CommandLine.NamingConventionBinder v.2.0.0-beta3.22114.1
, I'm attempting to create a simple get
command with a --name
option.
The option is required. It expects an argument, and it will use it in the Console.WriteLine
statement.
For the sake of organization, I am creating the commands in separate class files and inherit the Command
class.
public class GetCommand : Command
{
public GetCommand()
:base("get", "Filters events by name.")
{
var name = new Option("--name", "The name of the event you like to fetch.")
{
IsRequired = true,
};
AddOption(name);
Handler = CommandHandler.Create((string name) => Console.WriteLine(name));
}
}
While running the app, when I run the executable with the get --name ali
command, option, and argument, I keep getting the following exception:Unrecognized command or argument 'ali'.
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于参数的选项,您应该使用通用
选项< t>
。使用参数的类型作为类型参数:For options that take an argument, you should use the generic
Option<T>
. Use the type of the argument as the type parameter: