51

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)

1
  • You wrote a program that requires the user to input positional-or-keyword and required-or-optional arguments. Your program then needs to act on certain combination/permutation of these submitted arguments. Are you asking about the design patterns to structure functions that accomplish different tasks depending on the inputs given? Or are you asking about design patterns to loop through and parse the arguments (e.g., you're asking about the design patterns implemented by parser libraries like C's getopt and Python's argparse)?
    – Minh Tran
    Aug 2, 2018 at 20:48

15 Answers 15

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.

1
  • 2
    upvoted for sticking to the concepts rather than implementation. I feel like this should have been the chosen answer.
    – Plasmarob
    May 22, 2015 at 18:21
14

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

6

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

5

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.

1
  • -f, -d, -r seems to specify the information for a single ConcreteCommand. If the user specifies another combination of those flags -f -d, say, (or just -f or just -r or just -d, then each of those could represent another ConcreteCommand whose functionality (implemented by Execute()) differs from the ConcreteCommand initialized with -f, -d, -r.
    – Minh Tran
    Aug 2, 2018 at 21:01
4

A few comments on this...

First, while there aren't any patterns per se, writing a parser is essentially a mechanical exercise, since given a grammar, a parser can be easily generated. Tools like Bison, and ANTLR come to mind.

That said, parser generators are usually overkill for the command line. So the usual pattern is to write one yourself (as others have demonstrated) a few times until you get sick of dealing with the tedious detail and find a library to do it for you.

I wrote one for C++ that saves a bunch of effort that getopt imparts and makes nice use of templates: TCLAP

1
  • TCLAP is a fantastic CLI parsing library. The pre-parse setup of rules and post-parse extraction of the parsing of argv is very useful, intuitive and helps break the program up in to the correct discrete components (IMHO).
    – Sean
    Jan 21, 2013 at 7:08
2

The boost::program_options library is nice if you're in C++ and have the luxury of using Boost.

1
  • It is a good choice. That library is a little bit complex compared to the old getopt, but allows also to use configuration files (substituting or integrating command line arguments). Feb 7, 2013 at 9:05
2

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

while (current_argument = cli_parser_next()) {
    switch(current_argument) {
        case "f": //Parser strips the dashes
        case "force":
            config->force = true;
            break;
        case "d":
        case "delete":
            config->delete = true;
            break;
        //So on and so forth
        default:
            printUsage();
            exit;
    }
}
2

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:

int main (int argc, char *argv[])
   {  int i;
      char *Arg;
      int ParamX, ParamY;
      char *Text, *Primary;

   // Initialize...
   ParamX = 1;
   ParamY = 0;
   Text = NULL;
   Primary = NULL;

   // For each argument...
   for (i = 0; i < argc; i++)
      {
      // Get the next argument and see what it is
      Arg = argv[i];
      switch (Arg[0])
         {
         case '-':
         case '/':
            // It's an argument; which one?
            switch (Arg[1])
               {
               case '?':
               case 'h':
               case 'H':
                  // A cry for help
                  printf ("Usage:  whatever...\n\n");
                  return (0);
                  break;

               case 't':
               case 'T':
                  // Param T requires a value; is it there?
                  i++;
                  if (i >= argc)
                     {
                     printf ("Error:  missing value after '%s'.\n\n", Arg);
                     return (1);
                     }

                  // Just remember this
                  Text = Arg;

                  break;

               case 'x':
               case 'X':
                  // Param X requires a value; is it there?
                  i++;
                  if (i >= argc)
                     {
                     printf ("Error:  missing value after '%s'.\n\n", Arg);
                     return (1);
                     }

                  // The value is there; get it and convert it to an int (1..10)
                  Arg = argv[i];
                  ParamX = atoi (Arg);
                  if ((ParamX == 0) || (ParamX > 10))
                     {
                     printf ("Error:  invalid value for '%s'; must be between 1 and 10.\n\n", Arg);
                     return (1);
                     }

                  break;

               case 'y':
               case 'Y':
                  // Param Y doesn't expect a value after it
                  ParamY = 1;
                  break;

               default:
                  // Unexpected argument
                  printf ("Error:  unexpected parameter '%s'; type 'command -?' for help.\n\n", Arg);
                  return (1);
                  break;
               }

            break;

         default:
            // It's not a switch that begins with '-' or '/', so it's the primary option
            Primary = Arg;

            break;
         }
      }

   // Done
   return (0);
   }
2

I'm riffing on the ANTLR answer by mes5k. This link to Codeproject is for an article that discusses ANLTR and using the visit pattern to implement the actions you want you app to take. It's well written and worth reviewing.

1

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!

0
1

Getopt is the only way to go.

http://sourceforge.net/projects/csharpoptparse

0
1

How about the interpreter pattern? http://www.dofactory.com/net/interpreter-design-pattern

0

You don't mention a language for this but if you are looking for a really nice Objective-C wrapper around getopt then Dave Dribin's DDCLI framework is really nice.

http://www.dribin.org/dave/blog/archives/2008/04/29/ddcli

0

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

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.