4

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.

sashoalm
  • 75,001
  • 122
  • 434
  • 781
  • What version of the C# language are you using? The feature you're trying to use is called ["Top level statements"](https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-9#top-level-statements) and was introduced with c# 9. – Matthew Watson Dec 01 '21 at 09:13
  • I am using .NET 5.0 and according to https://stackoverflow.com/questions/19532942/which-version-of-c-sharp-am-i-using/19533393, that determines the C# version which should be 9.0 – sashoalm Dec 01 '21 at 09:16
  • Did you follow [the instructions for using `DragonFruit`](https://github.com/dotnet/command-line-api/blob/main/docs/DragonFruit-overview.md)? – Matthew Watson Dec 01 '21 at 09:25
  • 1
    Thank you, I needed to run `Install-Package System.CommandLine.DragonFruit -Version 0.3.0-alpha.21216.1` and now it works. Could you add that as an answer so I can accept it? – sashoalm Dec 01 '21 at 09:29

1 Answers1

5

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.

sashoalm
  • 75,001
  • 122
  • 434
  • 781
Matthew Watson
  • 104,400
  • 10
  • 158
  • 276