When building console applications that take parameters, you can use the arguments passed to Main(string[] args)
.
In the past I've simply indexed/looped that array and done a few regular expressions to extract the values. However, when the commands get more complicated, the parsing can get pretty ugly.
So I'm interested in:
- Libraries that you use
- Patterns that you use
Assume the commands always adhere to common standards such as answered here.
Genghis Command Line Parser may be a little out of date, but it is very feature complete and works pretty well for me.
You may like my one Rug.Cmd
Easy to use and expandable command line argument parser. Handles: Bool, Plus / Minus, String, String List, CSV, Enumeration.
Built in '/?' help mode.
Built in '/??' and '/?D' document generator modes.
Edit: This is my project and as such this answer should not be seen as an endorsement from a third party. That said I do use it for every command line based program I write, it is open source and it is my hope that others may benefit from it.
I wrote a C# command line argument parser a while back. Its at: http://www.codeplex.com/CommandLineArguments
The WPF TestApi library comes with one of the nicest command line parsers for C# development. I highly recommend looking into it, from Ivo Manolov's blog on the API:
There are numerous solutions to this problem. For completeness and to provide the alternative if someone desires I'm adding this answer for two useful classes in my google code library.
The first is ArgumentList which is responsible only for parsing command line parameters. It collects name-value pairs defined by switches '/x:y' or '-x=y' and also collects a list of 'unnamed' entries. It's basic usage is discussed here, view the class here.
The second part of this is the CommandInterpreter which creates a fully-functional command-line application out of your .Net class. As an example:
With the above example code you can run the following:
-- or --
It's as simple as that or as complex as you need it to be. You can review the source code, view the help, or download the binary.
My personal favorite is http://www.codeproject.com/KB/recipes/plossum_commandline.aspx by Peter Palotas: