Index   Main   Namespaces   Classes   Hierarchy   Annotated   Files   Compound   Global   Pages  

SgCmdLineOpt Class Reference

#include <SgCmdLineOpt.h>

List of all members.


Detailed Description

Parser for command line options.

Deprecated:
Use boost::program_options instead
Options start with the character '-'. The end of options can be indicated with "--" to allow arguments beginning with '-'.

Example:
In this example the allowed options are: a required option with integer argument "-optint", a option with string argument "-optstr" and default value "" and a option with no argument "-optbool" and default off.

    static bool s_optBool;

    static int s_optInt;

    static string s_optStr;

    static void ParseOptions(int argc, char** argv)
    {
        SgCmdLineOpt cmdLineOpt;
        vector<string> specs;
        specs.push_back("optint:");
        specs.push_back("optstr:");
        specs.push_back("optbool");
        cmdLineOpt.Parse(argc, argv, specs);
        if (! cmdLineOpt.Contains("optint"))
            throw SgException("Requires option -optint");
        cmdLineOpt.GetInteger("optint", s_optInt);
        s_optStr = cmdLineOpt.GetString("optstr", "");
        s_optBool = cmdLineOpt.Contains("optbool");
    }

    int main(int argc, char** argv)
    {
        try
        {
            ParseOptions(argc, argv);
            // ...
        }
        catch (const std::exception& e)
        {
            SgDebug() << e.what() << '\n';
            return 1;
        }
        return 0;
    }
    

Definition at line 67 of file SgCmdLineOpt.h.

Public Member Functions

 SgCmdLineOpt ()
bool Contains (const char *option) const
const std::vector< std::string > & GetArguments () const
 Get a list of the remaining command line arguments that are not an option.
double GetDouble (const char *option) const
 Get value of a floating point option.
double GetDouble (const char *option, double defaultValue) const
 Get value of a floating point option or use default value.
int GetInteger (const char *option) const
 Get value of an integer option.
int GetInteger (const char *option, int defaultValue) const
 Get value of an integer option or use default value.
std::string GetString (const char *option) const
 Get value of a string option.
std::string GetString (const char *option, const std::string &defaultValue) const
 Get value of a string option or use default value.
void Parse (int argc, char *argv[], const std::vector< std::string > &specs)
 Parse options from main(argc, argv).
void Parse (int argc, const char *argv[], const std::vector< std::string > &specs)
 Parse options from user created array.

Private Attributes

std::vector< std::string > m_args
std::map< std::string, std::string > m_map


Constructor & Destructor Documentation

SgCmdLineOpt::SgCmdLineOpt (  ) 

Definition at line 19 of file SgCmdLineOpt.cpp.


Member Function Documentation

bool SgCmdLineOpt::Contains ( const char *  option  )  const

Definition at line 23 of file SgCmdLineOpt.cpp.

References m_map.

const vector< string > & SgCmdLineOpt::GetArguments (  )  const

Get a list of the remaining command line arguments that are not an option.

Definition at line 28 of file SgCmdLineOpt.cpp.

References m_args.

double SgCmdLineOpt::GetDouble ( const char *  option,
double  defaultValue 
) const

Get value of a floating point option or use default value.

Exceptions:
SgException on error

Definition at line 38 of file SgCmdLineOpt.cpp.

References m_map.

double SgCmdLineOpt::GetDouble ( const char *  option  )  const

Get value of a floating point option.

Exceptions:
SgException on error

Definition at line 33 of file SgCmdLineOpt.cpp.

int SgCmdLineOpt::GetInteger ( const char *  option,
int  defaultValue 
) const

Get value of an integer option or use default value.

Exceptions:
SgException on error

Definition at line 58 of file SgCmdLineOpt.cpp.

References m_map.

int SgCmdLineOpt::GetInteger ( const char *  option  )  const

Get value of an integer option.

Exceptions:
SgException on error

Definition at line 53 of file SgCmdLineOpt.cpp.

string SgCmdLineOpt::GetString ( const char *  option,
const std::string &  defaultValue 
) const

Get value of a string option or use default value.

Definition at line 78 of file SgCmdLineOpt.cpp.

References m_map.

string SgCmdLineOpt::GetString ( const char *  option  )  const

Get value of a string option.

Definition at line 73 of file SgCmdLineOpt.cpp.

void SgCmdLineOpt::Parse ( int  argc,
const char *  argv[],
const std::vector< std::string > &  specs 
)

Parse options from user created array.

Uses const char* arguments. The only portable version of a C++ main() function uses char* arguments, but for testing this class with a user created array, it is better to use const char*, because assigning a string constant to char* causes compiler warnings with some compilers.

Definition at line 133 of file SgCmdLineOpt.cpp.

References Parse().

void SgCmdLineOpt::Parse ( int  argc,
char *  argv[],
const std::vector< std::string > &  specs 
)

Parse options from main(argc, argv).

Parameters:
argc Number of elements in argv
argv Argument vector from main().
specs Array of valid options (not including the leading '-'). Append ':' to options that are allowed to have an argument.
Exceptions:
SgException on error

Definition at line 87 of file SgCmdLineOpt.cpp.

References m_args, and m_map.

Referenced by Parse().


Member Data Documentation

std::vector<std::string> SgCmdLineOpt::m_args [private]

Definition at line 127 of file SgCmdLineOpt.h.

Referenced by GetArguments(), and Parse().

std::map<std::string, std::string> SgCmdLineOpt::m_map [private]

Definition at line 129 of file SgCmdLineOpt.h.

Referenced by Contains(), GetDouble(), GetInteger(), GetString(), and Parse().


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


17 Jun 2010 Doxygen 1.4.7