What is the design pattern for processing command

2020-02-07 19:45发布

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)

16条回答
男人必须洒脱
2楼-- · 2020-02-07 20:16

The standard design usually follows what getopt does, there are getopt libraries for many languages, .NET, python, C, Perl, PHP, etc.

The basic design is to have a command line parser which returns part by part the arguments passed to be checked in a loop.

This article discusses it in some more detail.

查看更多
SAY GOODBYE
3楼-- · 2020-02-07 20:17

Well, its an old post but i would still like to contribute. The question was intended on choice of design patterns however i could see a lot of discussion on which library to use. I have checked out microsoft link as per lindsay which talks about template design pattern to use.

However, i am not convinced with the post. Template pattern's intent is to define a template which will be implemented by various other classes to have uniform behavior. I don't think parsing command line fits into it.

I would rather go with "Command" design pattern. This pattern is best fit for menu driven options.

http://www.blackwasp.co.uk/Command.aspx

so in your case, -f, -d and -r all becomes commands which has common or separate receiver defined. That way more receivers can be defined in future. The next step will be to chain these responsibilities of commands, in case there a processing chain required. For which i would choose.

http://www.blackwasp.co.uk/ChainOfResponsibility.aspx

I guess the combination of these two are best to organize the code for command line processing or any menu driven approach.

查看更多
Melony?
4楼-- · 2020-02-07 20:19

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.

查看更多
Bombasti
5楼-- · 2020-02-07 20:21

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

查看更多
登录 后发表回答