将 System.CommandLine 与自定义 Main() 签名结合使用

sas*_*alm 5 c# command-line system.commandline

我正在尝试使用System.CommandLine并安装了 nuget 软件包:

Install-Package System.CommandLine -Version 2.0.0-beta1.21308.1
Run Code Online (Sandbox Code Playgroud)

根据这篇 Microsoft 文章,我应该能够使用我的签名编写一个 Main() 方法,并且它应该会自动神奇地工作:

static void Main(FileInfo input, FileInfo output)
{
    Console.WriteLine($"Hello World! {input} {output}");
}
Run Code Online (Sandbox Code Playgroud)

但是我的 Main() 方法签名被拒绝,我得到了CS5001: Program does not contain a static 'Main' method suitable for an entry point.

难道我做错了什么?根据这篇文章,这System.CommandLine应该是如何工作的。

Mat*_*son 6

确保以 .NET 5.0 为目标,并在包管理器控制台中运行以下 2 个命令:

Install-Package System.CommandLine -Version 2.0.0-beta1.21308.1
Install-Package System.CommandLine.DragonFruit -Version 0.3.0-alpha.21216.1 
Run Code Online (Sandbox Code Playgroud)

之后它应该可以工作。


说明:为了使用System.CommandLine,您还需要安装一个名为 的 NuGet 包DragonFruit

正是这个包启用了自定义命令行参数。

有关详细信息,请参阅此处:https://github.com/dotnet/command-line-api/blob/main/docs/DragonFruit-overview.md

另请注意,您还需要 C# 版本 9 或更高版本来支持Top-Level Statements,但您已确认您正在使用它 - 我在这里为其他读者提到它。