Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the design pattern for processing command line arguments

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)

like image 346
Sam McAfee Avatar asked Sep 10 '08 15:09

Sam McAfee


People also ask

Which method we used for command line arguments?

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.

What design principle is the Command pattern using?

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.

What is Command design pattern in Java?

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.

What is concept of command line arguments?

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.


2 Answers

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.

like image 166
Lindsay Morsillo Avatar answered Oct 19 '22 11:10

Lindsay Morsillo


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").

like image 25
Peter Ritchie Avatar answered Oct 19 '22 11:10

Peter Ritchie