Custom main entry point parameters from the command line in C#

Viewed 502

I saw somewhere online someone showing what appeared to be valid C# code. It allowed for custom parameters in the main method to be passed as command-line arguments. It looked something like this:

static void Main(FileInfo input, int maxSize = 9, bool someflag = false)
{
    // code
}

This could be used in the command line like this:

$ myApp hello_world.txt --maxSize 10 --someflag

Is this actually possible, or is someone pulling my leg?

1 Answers

Yes you can with DragonFruit, a (currently) alpha feature of the dotnet CLI.

Interpreting the string[] arguments into behaviors has been left as a task for the developer. Did the user ask for help? Did they pass invalid input? Can the input be converted to the types that you need if they're not string? These problems are not solved for you.

What if you could declare a strongly-typed Main method? This was the question that led to the creation of the experimental app model called "DragonFruit", which allows you to create an entry point with multiple parameters of various types and using default values [...]

Here's a tutorial: https://github.com/dotnet/command-line-api/blob/main/docs/Your-first-app-with-System-CommandLine-DragonFruit.md

Related