c# - 使用具有自定义 Main() 签名的 System.CommandLine

标签 c# command-line system.commandline

我正在尝试使用 System.CommandLine 并且我已经安装了 nuget 包:

Install-Package System.CommandLine -Version 2.0.0-beta1.21308.1

根据 this Microsoft article ,我应该能够用我的签名编写一个 Main() 方法,它应该自动神奇地工作:

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

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

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

最佳答案

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

Install-Package System.CommandLine -Version 2.0.0-beta1.21308.1
Install-Package System.CommandLine.DragonFruit -Version 0.3.0-alpha.21216.1 

之后它应该可以工作了。


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

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

详情请看这里:https://github.com/dotnet/command-line-api/blob/main/docs/DragonFruit-overview.md

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

关于c# - 使用具有自定义 Main() 签名的 System.CommandLine,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70181552/

相关文章:

C# 泛型方法解析失败,出现不明确的调用错误

c# - 在 Windows Phone 7 应用程序中发布图像文件

c# - System.CommandLine 解析的值与输入不匹配

c# - 如何判断在使用 System.CommandLine 时是否指定了一个选项?

c# - 从流中读取不知道有多少期望 [c#]

c# - ASP.NET Core 货币格式无法正常工作

command-line - 使用代码启动 Photoshop 操作

MySQL - 如何更改所有数据库中的列数据类型?

windows - findstr Windows命令正则表达式

c# - System.CommandLine命令自定义验证器: how to search