Back to index

nordugrid-arc-nox  1.1.0~rc6
Public Member Functions | Private Attributes
Arc::OptionParser Class Reference

#include <OptionParser.h>

Inheritance diagram for Arc::OptionParser:
Inheritance graph
[legend]
Collaboration diagram for Arc::OptionParser:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 OptionParser (const std::string &arguments="", const std::string &summary="", const std::string &description="")
 ~OptionParser ()
void AddOption (const char shortOpt, const std::string &longOpt, const std::string &optDesc, bool &val)
void AddOption (const char shortOpt, const std::string &longOpt, const std::string &optDesc, const std::string &argDesc, int &val)
void AddOption (const char shortOpt, const std::string &longOpt, const std::string &optDesc, const std::string &argDesc, std::string &val)
void AddOption (const char shortOpt, const std::string &longOpt, const std::string &optDesc, const std::string &argDesc, std::list< std::string > &val)
std::list< std::string > Parse (int argc, char **argv)

Private Attributes

std::string arguments
std::string summary
std::string description
std::list< OptionBase * > options

Detailed Description

Definition at line 13 of file OptionParser.h.


Constructor & Destructor Documentation

Arc::OptionParser::OptionParser ( const std::string &  arguments = "",
const std::string &  summary = "",
const std::string &  description = "" 
)

Definition at line 182 of file OptionParser.cpp.

                              {
    for (std::list<OptionBase*>::iterator it = options.begin();
         it != options.end(); it++)
      delete *it;
  }

Member Function Documentation

void Arc::OptionParser::AddOption ( const char  shortOpt,
const std::string &  longOpt,
const std::string &  optDesc,
bool &  val 
)

Definition at line 188 of file OptionParser.cpp.

                                            {
    options.push_back(new BoolOption(shortOpt, longOpt,
                                     optDesc, "", value));
  }

Here is the caller graph for this function:

void Arc::OptionParser::AddOption ( const char  shortOpt,
const std::string &  longOpt,
const std::string &  optDesc,
const std::string &  argDesc,
int &  val 
)

Definition at line 196 of file OptionParser.cpp.

                                           {
    options.push_back(new IntOption(shortOpt, longOpt,
                                    optDesc, argDesc, value));
  }
void Arc::OptionParser::AddOption ( const char  shortOpt,
const std::string &  longOpt,
const std::string &  optDesc,
const std::string &  argDesc,
std::string &  val 
)

Definition at line 205 of file OptionParser.cpp.

                                                 {
    options.push_back(new StringOption(shortOpt, longOpt,
                                       optDesc, argDesc, value));
  }
void Arc::OptionParser::AddOption ( const char  shortOpt,
const std::string &  longOpt,
const std::string &  optDesc,
const std::string &  argDesc,
std::list< std::string > &  val 
)

Definition at line 214 of file OptionParser.cpp.

                                                          {
    options.push_back(new StringListOption(shortOpt, longOpt,
                                           optDesc, argDesc, value));
  }
std::list< std::string > Arc::OptionParser::Parse ( int  argc,
char **  argv 
)

Definition at line 281 of file OptionParser.cpp.

                                                              {

    struct option *longoptions = new struct option[options.size() + 3];
    int i = 0;
    std::string optstring;
    for (std::list<OptionBase*>::iterator it = options.begin();
         it != options.end(); it++) {
      setopt(longoptions[i++], (*it)->longOpt.c_str(),
             (*it)->argDesc.empty() ? no_argument : required_argument,
             NULL, (*it)->shortOpt);
      optstring += (*it)->shortOpt;
      if (!(*it)->argDesc.empty())
        optstring += ':';
    }
    setopt(longoptions[i++], "help", no_argument, NULL, 'h');
    optstring += 'h';
    setopt(longoptions[i++], "help", no_argument, NULL, '?');
    optstring += '?';
    setopt(longoptions[i++], NULL, no_argument, NULL, '\0');

    char *argv0save = argv[0];
    argv[0] = strrchr(argv[0], '/');
    if (argv[0])
      argv[0]++;
    else
      argv[0] = argv0save;

    int opt = 0;
    while (opt != -1) {
#ifdef HAVE_GETOPT_LONG_ONLY
      opt = getopt_long_only(argc, argv, optstring.c_str(), longoptions, NULL);
#else
      opt = getopt_long(argc, argv, optstring.c_str(), longoptions, NULL);
#endif

      if (opt == -1)
        continue;
      if ((opt == '?') || (opt == ':') || (opt == 'h')) {
        if (optopt) {
          delete longoptions;
          exit(1);
        }
        std::cout << IString("Usage:") << std::endl;
        std::cout << "  " << argv[0];
        if (!options.empty())
          std::cout << " [" << IString("OPTION...") << "]";
        if (!arguments.empty())
          std::cout << " " << IString(arguments);
        std::cout << std::endl << std::endl;
        if (!summary.empty())
          std::cout << IString(summary) << std::endl << std::endl;
        std::cout << IString("Help Options:") << std::endl;
        std::cout << "  -h, -?, --help    " << IString("Show help options")
                  << std::endl << std::endl;
        std::cout << IString("Application Options:") << std::endl;
        for (std::list<OptionBase*>::iterator it = options.begin();
             it != options.end(); it++) {
          std::cout << "  -" << (*it)->shortOpt << ", --" << (*it)->longOpt;
          if (!(*it)->argDesc.empty())
            std::cout << "=" << IString((*it)->argDesc);
          std::cout << "    " << IString((*it)->optDesc) << std::endl;
        }
        std::cout << std::endl;
        if (!description.empty())
          std::cout << IString(description) << std::endl;
        delete longoptions;
        exit(0);
      }
      for (std::list<OptionBase*>::iterator it = options.begin();
           it != options.end(); it++)
        if (opt == (*it)->shortOpt) {
          if (!(*it)->Set(optarg ? optarg : "")) {
            delete longoptions;
            exit(1);
          }
          break;
        }
    }

    delete[] longoptions;
    argv[0] = argv0save;

    std::list<std::string> params;
    while (argc > optind)
      params.push_back(argv[optind++]);
    return params;
  }

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

std::string Arc::OptionParser::arguments [private]

Definition at line 48 of file OptionParser.h.

std::string Arc::OptionParser::description [private]

Definition at line 50 of file OptionParser.h.

std::list<OptionBase*> Arc::OptionParser::options [private]

Definition at line 51 of file OptionParser.h.

std::string Arc::OptionParser::summary [private]

Definition at line 49 of file OptionParser.h.


The documentation for this class was generated from the following files: