Main Page   Class Hierarchy   Alphabetical List   Data Structures   File List   Data Fields  

sword::CommandLineManager Class Reference

Command line parser. More...


Public Methods

 CommandLineManager (const std::string &title, const std::string &version, const std::string &copyright, const std::string &usage, int &argc, char **argv, bool fail=true)
 Create a CommandLine parser.

void help (std::ostream &os) const
 Print the help message.

void version (std::ostream &os) const
 Print the version information.

bool wantHelp () const
 Has the user required help on this application?

bool wantVersion () const
 Has the user required version information on this application?

void process ()
 Process the command line.

size_t size () const
 The number of registered options.


Friends

class CommandLineOptionBase


Detailed Description

Command line parser.

Command line arguments (argc, argv) can be analysed using this manager class together with the helper classes CommandLineOption and CommandLineFlag. It can handle complex combinaison of command line options and flags. Options can be given to the parser in any order, and it can recognise both "options" (i.e. command line flags which require a value):

or command line "flags" (i.e. command line flags which can only be "on" or "off"):

After the parsing, the (argc, argv) couple will be modified so that only the unrecognised options remain. For instance if your program needs options and files, CommandLineManager will process the options and you will get only your file names left in the (argc, argv) couple.

Example:

  #include "sword/sword.h"
  #include "sword/CommandLineManager.h"
  using namespace sword;

  int main(int argc, char *argv[])
  {
      // Create the manager
      CommandLineManager clm(
          "Foo", "1.0", "Copyright (C) ACME Corporation",
          "[OPTIONS] FILES", argc, argv);

      // Create the options and flags to recognize
      CommandLineOption<std::string> cl_o(clm, 'o', "output", "Output File Name", false);
      CommandLineOption<int>         cl_d(clm, 'd', "debug",  "Debug Level",      false, 1);
      CommandLineFlag  <bool>        cl_v(clm, 'v', "verbose", "Switch to Verbose Level", false);

      // go and process the command line
      clm.process();

      // display the help if it was required
      if (clm.wantHelp())    { clm.help(std::cout);    return 1; }
      if (clm.wantVersion()) { clm.version(std::cout); return 1; }

      // check some more conditions
      if (!cl_o.isSet()) { std::cout << "Missing '-o' argument, try --help"; return 1; }
      if (argc == 1) { std::cout << "Missing files arguments, try --help"; return 1; }

      // use the results
      std::cout << "Output file is : " << cl_o.value() << std::endl;
      std::cout << "Debug level is : " << cl_d.value() << std::endl;
      std::cout << "Verbose is     : " << (cl_v.value() ? "on" : "off") << std::endl;
      std::cout << "Files are      : ";
      for(int i=1; i<argc; ++i)
         std::cout << argv[i] << ", ";
      std::cout << std::endl;

      return 0;
  }
  


Constructor & Destructor Documentation

sword::CommandLineManager::CommandLineManager const std::string &    title,
const std::string &    version,
const std::string &    copyright,
const std::string &    usage,
int &    argc,
char **    argv,
bool    fail = true
 

Create a CommandLine parser.

Initialize the command line parser with enough information to display proper help on the command line as well as the actual command line arguments to parse.

Parameters:
argc reference to the number of command line options
argv the command line options
title the title of the application.
version the version of the application
copyright The copyright of the application.
usage What to print as syntax (may be empty)
fail Whether to fail on unknown options


Member Function Documentation

void sword::CommandLineManager::help std::ostream &    os const
 

Print the help message.

void sword::CommandLineManager::process  
 

Process the command line.

Exceptions:
ParsingException 

size_t sword::CommandLineManager::size   const
 

The number of registered options.

void sword::CommandLineManager::version std::ostream &    os const
 

Print the version information.

bool sword::CommandLineManager::wantHelp   const
 

Has the user required help on this application?

bool sword::CommandLineManager::wantVersion   const
 

Has the user required version information on this application?


Generated on Tue Dec 23 20:08:56 2003 for SWORD by doxygen1.3-rc2