Parsing Command Line Arguments in C#
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
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 shortoptions 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 remoteadd 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 githubwhich 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.