Search code examples
c#command-linesystem.commandline

Using System.CommandLine with custom Main() signature


I'm trying to use System.CommandLine and I've installed the nuget package:

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

According to this Microsoft article, I should be able to just write a Main() method with my signature and it should auto-magically work:

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

However my Main() method signature is rejected and I get CS5001: Program does not contain a static 'Main' method suitable for an entry point.

Am I doing something wrong? According to the article, this is how System.CommandLine should be working.


Solution

  • Make sure to target .NET 5.0, and run these 2 commands in the Package Manager Console:

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

    After that it should work.


    Explanation: In order to use System.CommandLine, you also need to install a NuGet package called DragonFruit.

    It's this package that enables the custom command line parameters.

    See here for details: https://github.com/dotnet/command-line-api/blob/main/docs/DragonFruit-overview.md

    Also note that you also need C# version 9 or later to support Top-Level Statements, but you have confirmed that you're using that - I mention it here for other readers.