【发布时间】:2022-09-26 06:00:34
【问题描述】:
我正在尝试学习如何使用 System.CommandLine。我创建了一个简单的代码,它需要接受一个 url 和一个可选的文件路径参数。
static async Task<int> Main(string[] args)
{
var cmd = new RootCommand(\"Save URL content to file\");
cmd.AddArgument(new Argument<string>(\"URL\", \"The network resource url you want to retrieve\"));
cmd.AddOption(new Option<FileInfo>(new[] { \"--output-document\", \"-O\" }, \"write document to FILE\"));
cmd.Handler = CommandHandler.Create<string, FileInfo, IConsole >(HandleDownload);
return await cmd.InvokeAsync(args);
}
static void HandleDownload(string url, FileInfo file, IConsole console)
{
console.WriteLine(url);
console.WriteLine(file.FullName);
}
当我使用以下参数运行时
\"http://www.google.com\" --output-document c:\\test.html
内部 HandleDownload url 变量被正确分配,但 \'file\' 变量显示为空。我究竟做错了什么?
-
是大写的问题吗? \"URL\" 参数名称与
url方法名称
标签: c# .net-core system.commandline