Command line parameters in .NET Core Console Applications

When implementing .NET Core console applications, you can extract the command line parameters from the void Main(string[] args) method, but it can be a little tedious to do so. To help with the parsing, Microsoft used to maintain the Microsoft.Extensions.CommandLineUtils, but this library have not been in development since 2017, so this is not a good option.

Thankfully, Nate McMaster forked the Microsoft project and created the CommandLineUtils, so developers like you and I don’t have to concern us with parsing of parameters, creating help pages, documentation and so on.

First you need to install the NuGet package:

McMaster.Extensions.CommandLineUtils

This is a base template of how your Program.cs main entry point will look like when using CommandLineUtils:

using McMaster.Extensions.CommandLineUtils;

namespace MyApplication
{
  [Command(Name = "The application name", Description = "The application description")]
  [HelpOption("-?")]
  public class Program
  {
    static void Main(string[] args) => CommandLineApplication.Execute<Program>(args);

    [Argument(0, Description = "The first argument")]
    private string MyArgument { get; }

    [Option("-o|--option", Description = "An example parameter")]
    private bool MyOption { get; }
    
    private void OnExecute()
    {
      // Initialize all the things...
      // Configure logging, application insights, ...
      // Run your application
    }
  }
}

EXPLANATION: THE Main(string[] args) METHOD:

This function is now pointing to the “OnExecute()” function instead. Treat theOnExecute() method as you would treat the Main(string[] args) method and initialize the servicecollection, logging, dependencies as you would normally do it.

EXPLANATION: ATTRIBUTE [Argument(…..)] VS [Option(……)]

CommandLineUtils treat command line parameters differently.

The [Argument] attribute determines parameters without any prefix. They also have a fixed position in the list of parameters.

The the example above, the MyArgument MUST be the first parameter when the application is called. This is determined by the value zero (0) in the attribute:

[Argument(0, Description = "The first argument")]

The value of MyArgument is set only by the first parameter:

./myapplication.exe firstparameter

Here, the value of MyArgument will be “firstparameter“.

The [Option] attribute does not have a fixed position, but a prefix instead. The boolean value of MyOption will be FALSE if -o or -option is NOT set, but true if -o or -option is set:

./myapplication.exe -o

Here, the value of MyOption is TRUE.

Options can have string parameters as well:

[Option("-o2|--option2", Description = "Another Option")]
private string MyOption2 { get; } = "Hello World";

The value of MyOption2 is “Hello world” unless you set the value in a parameter:

./myapplication.exe -o2 AnotherValue

Here, the value of MyOption2 will be “AnotherValue“.

The library have tons of more options, from automatic help pages, prompts for yes/no/passwords, optional/required parameters and so on. Dig into the library and see for yourself. Happy coding.

MORE TO READ:

About briancaos

Developer at Pentia A/S since 2003. Have developed Web Applications using Sitecore Since Sitecore 4.1.
This entry was posted in .NET Core, c# and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.