Logo Search packages:      
Sourcecode: ccbuild version File versions

bneijt::Arguments Class Reference

#include <Arguments.hh>

List of all members.


Detailed Description

Arguments singleton. Gives simple access to the give program arguments.

First initialize the singleton with options and the commandline arguments.

Arguments can be tested using the functions flagged, value and rest. Options should be a 3xN array of character pointers, ending in {0,0,0}. Options are defined with: {"flagges and long names, space seperated", "valuename, if any", "Help text explaning what the option is."}

WARNING: Arguments only supports up to 39 long options currently! See the TODO in Arguments.cc

Example of use

#include "Arguments/Arguments.hh"
#include <iostream>

#include <vector>

int main(int argc, char *argv[])
{
      using namespace bneijt;
      using namespace std;

      Arguments::Option options[] = {
        {"f force-update", "", "Force an update"},
            {"a add", "<file to add>", "Add something to a list"},
        {"b", "[maybe]", "Force an update"},
        {0, 0, 0}
      };

      Arguments::initialize(options, argc, argv);
      Arguments &args = Arguments::getInstance();
      if(args.flagged("a"))
            cout << "Add: " << args.value("a") <<"\n";

      if(args.errors().size() > 0)
      {
            vector<string> &err = args.errors();

            foreach(e, err)
            {
                  cout << "ERROR: " << (*e) << "\n";
            }
      }

      if(args.rest().size() > 0)
      {
            vector<string> &err = args.rest();

            foreach(e, err)
            {
                  cout << "REST: " << (*e) << "\n";
            }
      }

      args.destroy();   //Destroy the instance.

      return 0;
}

Definition at line 99 of file Arguments.hh.


Public Member Functions

std::vector< std::string > & errors ()
 Returns a vector containing all the commanline options generating errors.
bool flagged (std::string const &optionName) const
 Return true when an option if flagged, false otherwise.
void outputOptions (std::ostream &stream) const
 Output a list of the flags and help information onto the given stream.
std::vector< std::string > & rest ()
 Returns a vector containing all the remaining commandline options.
std::string value (std::string const &optionName) const
 Returns the argument of an option or an empty string.
std::vector< std::string > const & values (std::string const &optionName) const
 Returns the argument of an option or an empty string.

Static Public Member Functions

static void destroy ()
 Delete the instance. Use this only if you aren't going to use the Arguments class instance.
static ArgumentsgetInstance ()
 Get the instance of the Arguments class. Use this only after initializing it.
static void initialize (Option const *options, int argc, char *argv[])
 Initialize the singleton with options and argv, argc.

Private Member Functions

 Arguments (Arguments const &other)
 Not implemented.
 Arguments (Option const *options, int argc, char *argv[])
 Parse the argc and argv using the given options.
void clear ()
 Clear the class, clearing all vectors and freeing pointers.
Argumentsoperator= (Arguments const &other)
 Not implemented.

Private Attributes

std::string d_empty
 A non const empty string.
std::vector< std::string > d_emptyList
 A non const empty stringList.
std::vector< std::string > d_errors
 All options which caused errors.
std::map< std::string, bool * > d_flags
 The flags and their link to the values.
Option const * d_options
 A pointer to the options used to initialize the Arguments class.
std::vector< std::string > d_rest
 All remaining arguments.
std::map< bool *, std::vector
< std::string > > 
d_values
 Map between the argument value and the flag option.

Static Private Attributes

static Argumentss_instance
 Pointer to the singleton instance.

Classes

struct  Option
 A structure containing the defenition of a commandline option. More...

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

Generated by  Doxygen 1.6.0   Back to index