If you have the need to parse command line arguments in C# then might I recommend the excellent Command Line Parser. It can be installed from nuget by simply running
Â Â Install-Package CommandLineParser
Once you have it installed you start by setting up an options file which contains properties for all the options you would like your application to understand. Mine looks like
For each parameter you would like parsed you can decorate it with an Option attribute. Within this I defined the shortÂ options nameÂ (u for export users and h for help) followed by the long option name. I also set the help text, required status and default value for each option.
Within the Main method of my application I called out to the option parser like so
Another nifty feature of the library is the ability to define subcommands as part of your options. This allows building a command line interface similar to git in which the behaviour of the tool changes drastically based on the first undashed option:
git remoteÂ add http://some.url/project.git
This is quite well documented on the github wiki. I havenâ€™t tried it yet as my needs are not that complex.
There is a version 2.0 of the library under development on githubÂ which changes the interface pretty drastically. There doesnâ€™t seem to be a whole lot of development on it so Iâ€™ve opted for the more stable version for my projects.
Overall I would recommend this library over Mono.Options which Iâ€™ve used with a high degree of success in the past.