ArgumentParser
Go to the documentation of this file.
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details.
*/
#ifndef OSG_ARGUMENTPARSER
#define OSG_ARGUMENTPARSER 1
#include <osg/Export>
#include <osg/ref_ptr>
#include <osg/ApplicationUsage>
#include <map>
#include <string>
#include <ostream>
namespace osg {
class OSG_EXPORT ArgumentParser
{
public:
class OSG_EXPORT Parameter
{
public:
enum ParameterType
{
BOOL_PARAMETER,
FLOAT_PARAMETER,
DOUBLE_PARAMETER,
INT_PARAMETER,
UNSIGNED_INT_PARAMETER,
STRING_PARAMETER
};
union ValueUnion
{
bool* _bool;
float* _float;
double* _double;
int* _int;
unsigned int* _uint;
std::string* _string;
};
Parameter(bool& value) { _type = BOOL_PARAMETER; _value._bool = &value; }
Parameter(float& value) { _type = FLOAT_PARAMETER; _value._float = &value; }
Parameter(double& value) { _type = DOUBLE_PARAMETER; _value._double = &value; }
Parameter(int& value) { _type = INT_PARAMETER; _value._int = &value; }
Parameter(unsigned int& value) { _type = UNSIGNED_INT_PARAMETER; _value._uint = &value; }
Parameter(std::string& value) { _type = STRING_PARAMETER; _value._string = &value; }
Parameter(const Parameter& param) { _type = param._type; _value = param._value; }
Parameter& operator = (const Parameter& param) { _type = param._type; _value = param._value; return *this; }
bool valid(const char* str) const;
bool assign(const char* str);
protected:
ParameterType _type;
ValueUnion _value;
};
static bool isOption(const char* str);
static bool isString(const char* str);
static bool isNumber(const char* str);
static bool isBool(const char* str);
public:
ArgumentParser(int* argc,char **argv);
void setApplicationUsage(ApplicationUsage* usage) { _usage = usage; }
ApplicationUsage* getApplicationUsage() { return _usage.get(); }
const ApplicationUsage* getApplicationUsage() const { return _usage.get(); }
int& argc() { return *_argc; }
char** argv() { return _argv; }
char* operator [] (int pos) { return _argv[pos]; }
const char* operator [] (int pos) const { return _argv[pos]; }
std::string getApplicationName() const;
int find(const std::string& str) const;
bool isOption(int pos) const;
bool isString(int pos) const;
bool isNumber(int pos) const;
bool containsOptions() const;
void remove(int pos,int num=1);
bool match(int pos, const std::string& str) const;
bool read(const std::string& str);
bool read(const std::string& str, Parameter value1);
bool read(const std::string& str, Parameter value1, Parameter value2);
bool read(const std::string& str, Parameter value1, Parameter value2, Parameter value3);
bool read(const std::string& str, Parameter value1, Parameter value2, Parameter value3, Parameter value4);
bool read(const std::string& str, Parameter value1, Parameter value2, Parameter value3, Parameter value4, Parameter value5);
bool read(const std::string& str, Parameter value1, Parameter value2, Parameter value3, Parameter value4, Parameter value5, Parameter value6);
bool read(const std::string& str, Parameter value1, Parameter value2, Parameter value3, Parameter value4, Parameter value5, Parameter value6, Parameter value7);
bool read(const std::string& str, Parameter value1, Parameter value2, Parameter value3, Parameter value4, Parameter value5, Parameter value6, Parameter value7, Parameter value8);
bool read(int pos, const std::string& str);
bool read(int pos, const std::string& str, Parameter value1);
bool read(int pos, const std::string& str, Parameter value1, Parameter value2);
bool read(int pos, const std::string& str, Parameter value1, Parameter value2, Parameter value3);
bool read(int pos, const std::string& str, Parameter value1, Parameter value2, Parameter value3, Parameter value4);
bool read(int pos, const std::string& str, Parameter value1, Parameter value2, Parameter value3, Parameter value4, Parameter value5);
bool read(int pos, const std::string& str, Parameter value1, Parameter value2, Parameter value3, Parameter value4, Parameter value5, Parameter value6);
bool read(int pos, const std::string& str, Parameter value1, Parameter value2, Parameter value3, Parameter value4, Parameter value5, Parameter value6, Parameter value7);
bool read(int pos, const std::string& str, Parameter value1, Parameter value2, Parameter value3, Parameter value4, Parameter value5, Parameter value6, Parameter value7, Parameter value8);
enum ErrorSeverity
{
BENIGN = 0,
CRITICAL = 1
};
typedef std::map<std::string,ErrorSeverity> ErrorMessageMap;
bool errors(ErrorSeverity severity=BENIGN) const;
void reportError(const std::string& message,ErrorSeverity severity=CRITICAL);
void reportRemainingOptionsAsUnrecognized(ErrorSeverity severity=BENIGN);
ErrorMessageMap& getErrorMessageMap() { return _errorMessageMap; }
const ErrorMessageMap& getErrorMessageMap() const { return _errorMessageMap; }
void writeErrorMessages(std::ostream& output,ErrorSeverity sevrity=BENIGN);
ApplicationUsage::Type readHelpType();
protected:
int* _argc;
char** _argv;
ErrorMessageMap _errorMessageMap;
ref_ptr<ApplicationUsage> _usage;
};
}
#endif
ApplicationUsage
osg::ApplicationUsage
Definition ApplicationUsage:26
osg::ApplicationUsage::Type
Type
Definition ApplicationUsage:48
osg::ArgumentParser::Parameter
Definition ArgumentParser:32
osg::ArgumentParser::Parameter::Parameter
Parameter(float &value)
Definition ArgumentParser:56
osg::ArgumentParser::Parameter::Parameter
Parameter(bool &value)
Definition ArgumentParser:54
osg::ArgumentParser::Parameter::assign
bool assign(const char *str)
osg::ArgumentParser::Parameter::Parameter
Parameter(int &value)
Definition ArgumentParser:60
osg::ArgumentParser::Parameter::_value
ValueUnion _value
Definition ArgumentParser:76
osg::ArgumentParser::Parameter::Parameter
Parameter(unsigned int &value)
Definition ArgumentParser:62
osg::ArgumentParser::Parameter::valid
bool valid(const char *str) const
osg::ArgumentParser::Parameter::Parameter
Parameter(double &value)
Definition ArgumentParser:58
osg::ArgumentParser::Parameter::Parameter
Parameter(std::string &value)
Definition ArgumentParser:64
osg::ArgumentParser::Parameter::ParameterType
ParameterType
Definition ArgumentParser:35
osg::ArgumentParser::Parameter::INT_PARAMETER
@ INT_PARAMETER
Definition ArgumentParser:39
osg::ArgumentParser::Parameter::DOUBLE_PARAMETER
@ DOUBLE_PARAMETER
Definition ArgumentParser:38
osg::ArgumentParser::Parameter::FLOAT_PARAMETER
@ FLOAT_PARAMETER
Definition ArgumentParser:37
osg::ArgumentParser::Parameter::BOOL_PARAMETER
@ BOOL_PARAMETER
Definition ArgumentParser:36
osg::ArgumentParser::Parameter::UNSIGNED_INT_PARAMETER
@ UNSIGNED_INT_PARAMETER
Definition ArgumentParser:40
osg::ArgumentParser::Parameter::STRING_PARAMETER
@ STRING_PARAMETER
Definition ArgumentParser:41
osg::ArgumentParser::Parameter::Parameter
Parameter(const Parameter &param)
Definition ArgumentParser:66
osg::ArgumentParser::Parameter::_type
ParameterType _type
Definition ArgumentParser:75
osg::ArgumentParser::readHelpType
ApplicationUsage::Type readHelpType()
osg::ArgumentParser::getApplicationName
std::string getApplicationName() const
osg::ArgumentParser::isString
static bool isString(const char *str)
osg::ArgumentParser::argc
int & argc()
Definition ArgumentParser:102
osg::ArgumentParser::isOption
static bool isOption(const char *str)
osg::ArgumentParser::reportRemainingOptionsAsUnrecognized
void reportRemainingOptionsAsUnrecognized(ErrorSeverity severity=BENIGN)
osg::ArgumentParser::read
bool read(const std::string &str)
osg::ArgumentParser::read
bool read(int pos, const std::string &str, Parameter value1, Parameter value2, Parameter value3, Parameter value4, Parameter value5)
osg::ArgumentParser::writeErrorMessages
void writeErrorMessages(std::ostream &output, ErrorSeverity sevrity=BENIGN)
osg::ArgumentParser::_argv
char ** _argv
Definition ArgumentParser:205
osg::ArgumentParser::read
bool read(const std::string &str, Parameter value1, Parameter value2, Parameter value3, Parameter value4)
osg::ArgumentParser::getApplicationUsage
ApplicationUsage * getApplicationUsage()
Definition ArgumentParser:98
osg::ArgumentParser::read
bool read(int pos, const std::string &str, Parameter value1, Parameter value2, Parameter value3, Parameter value4, Parameter value5, Parameter value6)
osg::ArgumentParser::isNumber
bool isNumber(int pos) const
osg::ArgumentParser::match
bool match(int pos, const std::string &str) const
osg::ArgumentParser::read
bool read(const std::string &str, Parameter value1, Parameter value2, Parameter value3, Parameter value4, Parameter value5, Parameter value6)
osg::ArgumentParser::read
bool read(int pos, const std::string &str, Parameter value1, Parameter value2, Parameter value3, Parameter value4, Parameter value5, Parameter value6, Parameter value7)
osg::ArgumentParser::read
bool read(const std::string &str, Parameter value1, Parameter value2)
osg::ArgumentParser::_errorMessageMap
ErrorMessageMap _errorMessageMap
Definition ArgumentParser:206
osg::ArgumentParser::read
bool read(const std::string &str, Parameter value1, Parameter value2, Parameter value3, Parameter value4, Parameter value5, Parameter value6, Parameter value7)
osg::ArgumentParser::read
bool read(int pos, const std::string &str, Parameter value1)
osg::ArgumentParser::argv
char ** argv()
Definition ArgumentParser:105
osg::ArgumentParser::read
bool read(int pos, const std::string &str, Parameter value1, Parameter value2)
osg::ArgumentParser::getErrorMessageMap
const ErrorMessageMap & getErrorMessageMap() const
Definition ArgumentParser:188
osg::ArgumentParser::read
bool read(int pos, const std::string &str)
osg::ArgumentParser::read
bool read(int pos, const std::string &str, Parameter value1, Parameter value2, Parameter value3, Parameter value4)
osg::ArgumentParser::read
bool read(const std::string &str, Parameter value1)
osg::ArgumentParser::read
bool read(int pos, const std::string &str, Parameter value1, Parameter value2, Parameter value3, Parameter value4, Parameter value5, Parameter value6, Parameter value7, Parameter value8)
osg::ArgumentParser::isNumber
static bool isNumber(const char *str)
osg::ArgumentParser::getApplicationUsage
const ApplicationUsage * getApplicationUsage() const
Definition ArgumentParser:99
osg::ArgumentParser::reportError
void reportError(const std::string &message, ErrorSeverity severity=CRITICAL)
osg::ArgumentParser::find
int find(const std::string &str) const
osg::ArgumentParser::read
bool read(int pos, const std::string &str, Parameter value1, Parameter value2, Parameter value3)
osg::ArgumentParser::read
bool read(const std::string &str, Parameter value1, Parameter value2, Parameter value3, Parameter value4, Parameter value5, Parameter value6, Parameter value7, Parameter value8)
osg::ArgumentParser::getErrorMessageMap
ErrorMessageMap & getErrorMessageMap()
Definition ArgumentParser:185
osg::ArgumentParser::ErrorSeverity
ErrorSeverity
Definition ArgumentParser:168
osg::ArgumentParser::CRITICAL
@ CRITICAL
Definition ArgumentParser:170
osg::ArgumentParser::BENIGN
@ BENIGN
Definition ArgumentParser:169
osg::ArgumentParser::isString
bool isString(int pos) const
osg::ArgumentParser::isOption
bool isOption(int pos) const
osg::ArgumentParser::errors
bool errors(ErrorSeverity severity=BENIGN) const
osg::ArgumentParser::containsOptions
bool containsOptions() const
osg::ArgumentParser::_argc
int * _argc
Definition ArgumentParser:204
osg::ArgumentParser::read
bool read(const std::string &str, Parameter value1, Parameter value2, Parameter value3, Parameter value4, Parameter value5)
osg::ArgumentParser::setApplicationUsage
void setApplicationUsage(ApplicationUsage *usage)
Definition ArgumentParser:97
osg::ArgumentParser::ArgumentParser
ArgumentParser(int *argc, char **argv)
osg::ArgumentParser::remove
void remove(int pos, int num=1)
osg::ArgumentParser::_usage
ref_ptr< ApplicationUsage > _usage
Definition ArgumentParser:207
osg::ArgumentParser::isBool
static bool isBool(const char *str)
osg::ArgumentParser::read
bool read(const std::string &str, Parameter value1, Parameter value2, Parameter value3)
osg::ArgumentParser::ErrorMessageMap
std::map< std::string, ErrorSeverity > ErrorMessageMap
Definition ArgumentParser:173
osg::ref_ptr
Definition ref_ptr:32
osg
Definition AlphaFunc:19
Export
OSG_EXPORT
#define OSG_EXPORT
Definition osg/Export:39
ref_ptr
osg::ArgumentParser::Parameter::ValueUnion
Definition ArgumentParser:45
osg::ArgumentParser::Parameter::ValueUnion::_bool
bool * _bool
Definition ArgumentParser:46
osg::ArgumentParser::Parameter::ValueUnion::_string
std::string * _string
Definition ArgumentParser:51
osg::ArgumentParser::Parameter::ValueUnion::_int
int * _int
Definition ArgumentParser:49
osg::ArgumentParser::Parameter::ValueUnion::_float
float * _float
Definition ArgumentParser:47
osg::ArgumentParser::Parameter::ValueUnion::_uint
unsigned int * _uint
Definition ArgumentParser:50
osg::ArgumentParser::Parameter::ValueUnion::_double
double * _double
Definition ArgumentParser:48