If you are writing a program that is executable from the command line, you often want to offer the user several options or flags, along with possibly more than one argument. I have stumbled my way through this many times, but is there some sort of design pattern for looping through args and calling the appropriate handler functions?
Consider:
myprogram -f filename -d directory -r regex
How do you organize the handler functions after you retrieve the arguments using whatever built-ins for your language? (language-specific answers welcomed, if that helps you articulate an answer)
I prefer options like "-t text" and "-i 44"; I don't like "-fname" or "--very-long-argument=some_value".
And "-?", "-h", and "/h" all produce a help screen.
Here's how my code looks:
Getopt is the only way to go.
http://sourceforge.net/projects/csharpoptparse
Assuming you have a "config" object that you aim to setup with the flags and a suitable command line parser that takes care of parsing the command line and supply a constant stream of the options, here goes a block of pseudocode
I would recommend using a command line processor library. Some Russian guy created a decent one, but there are tons of them out there. Will save you some time so you can concentrate on the purpose of your app rather than parsing command line switches!
I use the Getopts::std and Getopts::long in perl and also the Getopt function in C. This standardises the parsing and format of parameters. Other languages have different mechanisms for handling these.
Hope this helps
How about the interpreter pattern? http://www.dofactory.com/net/interpreter-design-pattern