Command Interpreter Basics

Last Updated: 08/23/2009


A command interpreter does the following:

  1. Input Command - Inputs commands from the outside world;
  2. Command Decoder - Interprets or decodes the command;
  3. Command Dispatcher - Dispatches the command to a "Do Operation";
  4. Do Operations - Operations specifically written to handle the command.  These operations usually produce some kind of output or response back to the outside world.
  5. Command Controller - Repeats Steps 1 - 4 forever, or until a special command tells it to quit processing commands.

Command-Line Programs

A Command Interpreter's Architecture

#include "wrapper.h"
using namespace std;
The #include Section
  • Always include the "wrapper.h" file to get all the standard wrapper classes for the built-in types.
  • #include files that provide software components, e.g., Queue, Stack, List, etc.
  • #include files that provide functions that need to be called.
 
void Display_Menu ( ... )
{
   cout << 
      "Legal Commands Are:" <<
      endl;
   cout << 
      " ... " << 
      endl;
      ...
   cout << 
      "Type 'q' to Quit" <<
      endl;
}
Display Menu Section
  • It can be helpful to display a menu of legal commands for the user.
  • If the number of commands is large, however, this approach may not work very well.
 
void Do_Op1 ( ... ) { ... }
void Do_Op2 ( ... ) { ... }

   ...

void Do_OpN ( ... ) { ... }
The "Do_" Operations Section
  • Specifically written to handle a command. 
  • One "Do_" Operation per command.
  • A "Do_"  Operation might produce output to the user.
  • They might cause a permanent change to a data structure.
  • They might cause a change to the environment, e.g., a file to be written to.
 
void Dispatch_Command ( ... )
{
   switch (command) {
      case command_1:
         Do_Op1 (...);
         break;
      case command_2:
         ...
      default:
         cout << 
            "No such command" <<
            endl;
   } // end switch
}
The Decoder/Dispatcher Section
  • This example decodes the command and dispatches to the correct "Do_"  Operation.
  • If the commands are complex, then a separate decoder operation might be required.
  • A C++ switch statement can be used.
  • if statements can be used.
  • A default case can be supplied to handle incorrect commands entered by the user.
 
void Command_Controller ( ... )
{
   Character command;

   do {
      Display_Menu ( ... );
      cin >> command;
      Dispatch_Command (command);
   while (command != 'q');
}
The Command_Controller Section
  • Displays the menu, if necessary
  • Inputs a command
  • Gives it to the Decoder/Dispather
  • Keeps looping until user requests an exit
 
void main ( ... )
{
   T x1, x2;

   Command_Controller (x1, x2, ... );
}
The main Operation Section
  • Declares objects that need to live the lifetime of the program.
  • In this example, type T would have come from one of the #include files.
  • Passes those objects to the Command_Controller as parameters so rest of program can use them.
  • Calls Command_Controller to begin looping.

The Ordering of the Sections in the File