What is the design pattern for processing command line arguments

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.


You didn't mention the language, but for Java we've loved Apache Commons CLI. For C/C++, getopt.


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