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)
In the command line, the arguments passed from the console can be received in the java program and they can be used as input. The users can pass the arguments during the execution bypassing the command-line arguments inside the main() method. We need to pass the arguments as space-separated values.
In object-oriented programming, the command pattern is a behavioral design pattern in which an object is used to encapsulate all information needed to perform an action or trigger an event at a later time.
A Command pattern is an object behavioral pattern that allows us to achieve complete decoupling between the sender and the receiver. (A sender is an object that invokes an operation, and a receiver is an object that receives the request to execute a certain operation.
Command line arguments are nothing but simply arguments that are specified after the name of the program in the system's command line, and these argument values are passed on to your program during program execution.
I think the following answer is more along the lines of what you are looking for:
You should look at applying the Template Pattern (Template Method in "Design Patterns" [Gamma, el al])
In short it's overall processing looks like this:
If the arguments to the program are valid then
Do necessary pre-processing
For every line in the input
Do necessary input processing
Do necessary post-processing
Otherwise
Show the user a friendly usage message
In short, implement a ConsoleEngineBase class that has methods for:
PreProcess()
ProcessLine()
PostProcess()
Usage()
Main()
Then create a chassis, that instantiates a ConsoleEngine() instance and sends the Main() message to kick it off.
To see a good example of how to apply this to a console or command line program check out the following link: http://msdn.microsoft.com/en-us/magazine/cc164014.aspx
The example is in C#, but the ideas are easily implemented in any other environment.
You would look at the GetOpt() as just the part that fit's into the argument handling (pre-processing).
Hope this helps.
I don't know of any documented "patterns" for processing.
I believe one of the oldest libraries/APIs for handling arguments is getopt. Googling "getopt" shows lots of man pages and links to implementations.
Generally, I have a preferences or settings service in my application that the argument processor knows how to communicate with. Arguments are then translated into something in this service that the application than then query. This could be as simple as a dictionary of settings (like a string setting named "filename").
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With