以下简单的命令Program.cs
需要一个定义的根命令的参数:
using System.CommandLine;
var inputArgument = new Argument<string>(
name: "--input",
description: "input any value and print it out");
var rootCommand = new RootCommand();
rootCommand.AddArgument(inputArgument);
rootCommand.SetHandler((inputArgumentValue) =>
{
Console.WriteLine($"{inputArgumentValue}");
}, inputArgument);
rootCommand.Invoke(args);
我希望使用以下参数调用它:在 shell 中--input "Hello World"
打印出Hello World 。但是我收到以下错误:
Unrecognized command or argument 'Hello World'.
using System.CommandLine;
var inputArgument = new Option<string>(
name: "--input",
description: "input any value and print it out");
var rootCommand = new RootCommand();
rootCommand.AddOption(inputArgument);
rootCommand.SetHandler((inputArgumentValue) =>
{
Console.WriteLine($"{inputArgumentValue}");
}, inputArgument);
rootCommand.Invoke(args);
我对课堂有什么误解Argument
?为什么我不能传递一个带有值的参数?
由于其其他属性,我想使用参数类而不是选项。我正在使用 .NET 6.0 和 System.CommandLine 版本 2.0.0-beta4.22272.1
查看System.CommandLine 文档的命令行语法概述。
它将选项定义为:
参数如下:
所以基本上参数是传递给命令或选项的无名位置参数,即对于您的第一个片段有效调用将是:
您当然可以添加 2 个参数:
然后您的
appName --input "Hello World"
调用将导致处理程序获得 2 个值--input
forinputArgumentValue
和"Hello World"
forinputArgumentValue2
。但我认为使用
Option<string>
(第二个片段)应该是更正确的方法(它也将允许传递用=
:分隔的值appName --input="Hello World"
)。