Slice Tools
  • Home
  • SourceForge Page


  • libFoundation
  • Home
  • Class Hierarchy
  • Class List
  • Class Members
  • Examples


  • SourceForge.net Logo
     

    example-options.cc

    Demonstrates commmand line options handling.

    Notice how the results of the command line option handling can be found automatically without explicitly asking if an option was specified, or option data can be queried by name. If automatic results are requested, the options handler will convert the raw character array from argv into the output type (int, float, or string). Automatic results also allow for default values to be automatically updated if corresponding options are provided on the command line.
    // $Id: example-options.cc,v 1.1 2005/07/29 02:55:16 mschatz Exp $
    // Example program for options handling
    
    #include "Foundation.hh"
    using namespace std;
    
    // Different functions of the main program to run as determined by the command
    // line options
    
    // Each takes a different type of argument (int, float or string) as an example
    // to the different types of data that the options handler can automatically
    // parse.
    
    void workA(int a)
    {
      cout << "In doWorkA()" << endl;
      cout << "a=" << a << endl;
    }
    
    void workB(float b)
    {
      cout << "In doWorkB()" << endl;
      cout << "b=" << b << endl;
    }
    
    void workC(string c)
    {
      cout << "In doWorkC()" << endl;
      cout << "c=" << c << endl;
    }
    
    
    
    
    // Prints out an option with value or that it just exists
    void printOption(Options * options, string optionName)
    {
      cout << "Option Found: ";
      string value = options->getOption(optionName);
    
      if (options->hasOption(optionName))
      {
        if (value.length())
        {
          cout << optionName << "=" << value << endl;
        }
        else
        {
          cout << optionName << " exists" << endl;
        }
      }
      else
      {
        cout << optionName << " was not specified" << endl;
      }
    }
    
    // Demonstates different operations on options
    // One can loop through all of the different options that the options handler
    // understands, and query for the value of each.
    int testOptions(Options * options)
    {
      int retval = 0;
    
      cout << "--------------------------" << endl;
      cout << "Testing Options"            << endl;
      cout << "--------------------------" << endl;
    
      cout << "Application Filespec is: " 
           << options->getApplicationFilespec() << endl;
    
      cout << "Application Name is: "     
           << options->getApplicationName() << endl;
    
      cout << "Invocation Was: "          
           << options->getInvocation() << endl;
    
      string optionName;
      while ((optionName = options->getNextOption()) != "")
      {
        printOption(options, optionName);
      }
    
      // Loop through the non-option other data specified on the command line
      while ((optionName = options->getNextOtherData()) !="")
      {
        cout << "Other Data: " << optionName << endl;
      }
      
      return retval;
    }
    
    
    // Instantiates Foundation, runs tests based on command line options
    int main(int argc, char ** argv)
    {
      string helptext = "Foundation command line options example";
      string version = "Foundation example-options 1.0";
      string dependencies = "Foundation";
    
      // Set default values that will be overwritten by the options handler
      int doTestOptions = 0;
    
      int doWorkA       = 0;
      int doWorkB       = 0;
    
      int a    = 5432;
      float b  = 5432.11;
      string c = "5432.11";
    
      try
      {
        // Instantiate Foundation
        Foundation *app = new Foundation (version, 
                                          helptext, 
                                          dependencies,
                                          argc, 
                                          argv);
        // Add additional options
        // Notice how in these examples, a pointer is passed to where the 
        // options handler should write the result of the options passed
        // on the command line. If the option is not specified on the command
        // line, the original default value set above will still be present.
        app->getOptions()->addOptionResult("testoptions!", &doTestOptions,
                                           "Test the options handling");
    
        app->getOptions()->addOptionResult("a=i", &a, "Set an int value for a");
        app->getOptions()->addOptionResult("b=f", &b, "Set a float value for b");
        app->getOptions()->addOptionResult("c=s", &c, "Set a string value for c");
    
        app->getOptions()->addOptionResult("doA", &doWorkA, "Run workA()");
        app->getOptions()->addOptionResult("doB", &doWorkB, "Run workB()");
    
        // Parse options 
        // handleStandardOptions has the side effect of parsing all options
        // in addition to just handling the standard options.
        // This way, a second call to parse the options is not needed.
    
        // If non-appropriate data is provided on the command line to the different
        // options, a fatal exception will be thrown
        app->handleStandardOptions();
    
        // Main program starts here
        
        // No special calls are needed to get the results of the command line
        // handling, because a pointer was passed to where the results should
        // be written
        if (doTestOptions)                     testOptions(app->getOptions());
        if (doWorkA)                           workA(a);
        if (doWorkB)                           workB(b);
    
        // Alternatively, one can also query for an option by name
        if (app->getOptions()->hasOption("c")) workC(c);
    
        // Delete Foundation
        delete app;
      }
      catch (ExitProgramNormally e)
      {
        // non fatal error (help, version, or dependencies were requested)
      }
      catch (Exception e)
      {
        // fatal error
        
        cerr << e << endl;
        exit (100);
      }
    
      return 0;
    }