Skip to content

Quick Start

Matthias Beerens edited this page Aug 9, 2019 · 5 revisions

Quick Start

First of all you need to add the nuget package.

PM> Install-Package MatthiWare.CommandLineParser

Now you can setup the command line parser.

static int Main(string[] args)
{
   // create the parser
   var parser = new CommandLineParser<ServerOptions>();
   
   // configure the options using the Fluent API (can also be done using attributes)
   parser.Configure(options => options.Port)
      .Name("p", "port")
      .Description("The port of the server")
      .Required();

   // parse
   var result = parser.Parse(args);

   // check for parsing errors
   // By default errors and correct usage will be printed to the console
   // No need to print the errors ourselves 
   if (result.HasErrors)
   {
      Console.ReadKey();

      return -1;
   }

   Console.WriteLine($"Parsed port is {result.Result.Port}!");
}

Run command line

dotnet myapp --port 2551
Clone this wiki locally