Introducing the Command Line Parser Library to make parsing command line arguments easier

If you’ve worked extensively with .NET console applications then you’ve probably encountered difficulties with parsing the command line arguments. They are passed into the args argument of the Main function. Then it’s your task to investigate if the caller has passed in all necessary arguments:

  • Have all the mandatory arguments been passed in?
  • What default value do we take for optional arguments?
  • What about the ordering of arguments?

…et cetera, the list could grow a lot longer.

Fortunately there are libraries that can help you parse the arguments. One such library is called Command Line Parser Library available from NuGet here. You can find its project page here with some initial code examples.

You can install the library from within Visual Studio:

Command line parser library in nuget

Upon installation you’ll see a readme.txt file in Visual Studio with some extra code examples.

The most basic usage of the library is that you create a custom class that will hold the argument properties. E.g. if your application requires a name and an age argument then you create a class with those properties and decorate them with attributes from the parser library.

Let’s see how it works in code.

Consider the following class:

public class ProgramArguments
{
	[Option('n')]
	public string Name { get; set; }
	[Option('a')]
	public int Age { get; set; }
}

The Option attribute has a range of overloaded versions and named arguments. The above example means that Name will be populated with the argument preceded by “-n”. The same is “-a” for the Age. It is very common to indicate the role of an argument:

myapplication -n “Andras” -a 36

The ordering shouldn’t matter:

myapplication -a 36 -n “Andras”

The parser library can handle that of course.

You can populate the ProgramArguments object properties as follows:

static void Main(string[] args)
{
	ProgramArguments arguments = new ProgramArguments();
	CommandLine.Parser.Default.ParseArguments(args, arguments);
}

Providing the arguments…

-n “Andras” -a 36

…to the application will in fact correctly populate the Name and Age properties of the ProgramArguments object.

The library can handle a lot of different cases.

View all various C# language feature related posts here.

Advertisements

About Andras Nemes
I'm a .NET/Java developer living and working in Stockholm, Sweden.

One Response to Introducing the Command Line Parser Library to make parsing command line arguments easier

  1. Pingback: Introducing the Command Line Parser Library to make parsing command line arguments easier | Dinesh Ram Kali.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

ultimatemindsettoday

A great WordPress.com site

iReadable { }

.NET Tips & Tricks

Robin Sedlaczek's Blog

Developer on Microsoft Technologies

HarsH ReaLiTy

A Good Blog is Hard to Find

Softwarearchitektur in der Praxis

Wissenswertes zu Webentwicklung, Domain-Driven Design und Microservices

the software architecture

thoughts, ideas, diagrams,enterprise code, design pattern , solution designs

Technology Talks

on Microsoft technologies, Web, Android and others

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

Anything around ASP.NET MVC,WEB API, WCF, Entity Framework & AngularJS

Cyber Matters

Bite-size insight on Cyber Security for the not too technical.

Guru N Guns's

OneSolution To dOTnET.

Johnny Zraiby

Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

%d bloggers like this: