2006-07-18 23:21:48 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
|
2003-02-19 00:36:42 +08:00
|
|
|
*
|
|
|
|
* 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>
|
2007-02-12 21:39:18 +08:00
|
|
|
#include <osg/ref_ptr>
|
|
|
|
#include <osg/ApplicationUsage>
|
2003-02-19 00:36:42 +08:00
|
|
|
|
|
|
|
#include <map>
|
2003-02-19 05:58:40 +08:00
|
|
|
#include <string>
|
2004-03-03 21:27:21 +08:00
|
|
|
#include <ostream>
|
2003-02-19 00:36:42 +08:00
|
|
|
|
|
|
|
namespace osg {
|
|
|
|
|
2005-04-12 01:14:17 +08:00
|
|
|
class OSG_EXPORT ArgumentParser
|
2003-02-19 00:36:42 +08:00
|
|
|
{
|
|
|
|
public:
|
2004-03-20 06:03:09 +08:00
|
|
|
|
2005-04-12 01:14:17 +08:00
|
|
|
class OSG_EXPORT Parameter
|
2004-03-20 06:03:09 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
enum ParameterType
|
|
|
|
{
|
2007-09-19 23:29:57 +08:00
|
|
|
BOOL_PARAMETER,
|
2004-03-20 06:03:09 +08:00
|
|
|
FLOAT_PARAMETER,
|
|
|
|
DOUBLE_PARAMETER,
|
|
|
|
INT_PARAMETER,
|
|
|
|
UNSIGNED_INT_PARAMETER,
|
2005-05-14 05:38:34 +08:00
|
|
|
STRING_PARAMETER
|
2004-03-20 06:03:09 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
union ValueUnion
|
|
|
|
{
|
2007-09-19 23:29:57 +08:00
|
|
|
bool* _bool;
|
2004-03-20 06:03:09 +08:00
|
|
|
float* _float;
|
|
|
|
double* _double;
|
|
|
|
int* _int;
|
|
|
|
unsigned int* _uint;
|
|
|
|
std::string* _string;
|
|
|
|
};
|
|
|
|
|
2007-09-19 23:29:57 +08:00
|
|
|
Parameter(bool& value) { _type = BOOL_PARAMETER; _value._bool = &value; }
|
|
|
|
|
2004-03-20 06:03:09 +08:00
|
|
|
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; }
|
|
|
|
|
2005-02-01 17:00:10 +08:00
|
|
|
Parameter(const Parameter& param) { _type = param._type; _value = param._value; }
|
|
|
|
|
|
|
|
Parameter& operator = (const Parameter& param) { _type = param._type; _value = param._value; return *this; }
|
|
|
|
|
2004-03-20 06:03:09 +08:00
|
|
|
bool valid(const char* str) const;
|
|
|
|
bool assign(const char* str);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
ParameterType _type;
|
|
|
|
ValueUnion _value;
|
|
|
|
};
|
|
|
|
|
2004-09-01 16:15:36 +08:00
|
|
|
/** Return true if the specified string is an option in the form
|
|
|
|
* -option or --option. */
|
2004-03-20 06:03:09 +08:00
|
|
|
static bool isOption(const char* str);
|
|
|
|
|
2004-09-01 16:15:36 +08:00
|
|
|
/** Return true if string is non-NULL and not an option in the form
|
|
|
|
* -option or --option. */
|
2004-03-20 06:03:09 +08:00
|
|
|
static bool isString(const char* str);
|
|
|
|
|
2004-09-01 16:15:36 +08:00
|
|
|
/** Return true if specified parameter is a number. */
|
2004-03-20 06:03:09 +08:00
|
|
|
static bool isNumber(const char* str);
|
|
|
|
|
2007-09-19 23:29:57 +08:00
|
|
|
/** Return true if specified parameter is a bool. */
|
|
|
|
static bool isBool(const char* str);
|
|
|
|
|
2004-03-20 06:03:09 +08:00
|
|
|
public:
|
|
|
|
|
2003-02-19 05:58:40 +08:00
|
|
|
ArgumentParser(int* argc,char **argv);
|
|
|
|
|
|
|
|
void setApplicationUsage(ApplicationUsage* usage) { _usage = usage; }
|
2007-02-12 21:39:18 +08:00
|
|
|
ApplicationUsage* getApplicationUsage() { return _usage.get(); }
|
|
|
|
const ApplicationUsage* getApplicationUsage() const { return _usage.get(); }
|
2003-02-19 05:58:40 +08:00
|
|
|
|
2004-09-01 16:15:36 +08:00
|
|
|
/** Return the argument count. */
|
2003-02-19 00:36:42 +08:00
|
|
|
int& argc() { return *_argc; }
|
|
|
|
|
2004-09-01 16:15:36 +08:00
|
|
|
/** Return the argument array. */
|
2003-02-19 00:36:42 +08:00
|
|
|
char** argv() { return _argv; }
|
|
|
|
|
2004-09-01 16:15:36 +08:00
|
|
|
/** Return the char* argument at the specified position. */
|
2003-02-19 00:36:42 +08:00
|
|
|
char* operator [] (int pos) { return _argv[pos]; }
|
|
|
|
|
2004-09-01 16:15:36 +08:00
|
|
|
/** Return the const char* argument at the specified position. */
|
2003-02-19 00:36:42 +08:00
|
|
|
const char* operator [] (int pos) const { return _argv[pos]; }
|
|
|
|
|
2004-09-01 16:15:36 +08:00
|
|
|
/** Return the application name, as specified by argv[0] */
|
2003-04-07 05:32:44 +08:00
|
|
|
std::string getApplicationName() const;
|
2003-02-19 00:36:42 +08:00
|
|
|
|
2004-09-01 16:15:36 +08:00
|
|
|
/** Return the position of an occurance of a string in the argument list.
|
|
|
|
* Return -1 if no string is found. */
|
2003-02-19 00:36:42 +08:00
|
|
|
int find(const std::string& str) const;
|
|
|
|
|
2004-09-01 16:15:36 +08:00
|
|
|
/** Return true if the specified parameter is an option in the form of
|
|
|
|
* -option or --option. */
|
2003-02-19 00:36:42 +08:00
|
|
|
bool isOption(int pos) const;
|
|
|
|
|
2004-09-01 16:15:36 +08:00
|
|
|
/** Return true if the specified parameter is a string not in
|
|
|
|
* the form of an option. */
|
2003-02-19 00:36:42 +08:00
|
|
|
bool isString(int pos) const;
|
|
|
|
|
2004-09-01 16:15:36 +08:00
|
|
|
/** Return true if the specified parameter is a number. */
|
2003-02-19 00:36:42 +08:00
|
|
|
bool isNumber(int pos) const;
|
|
|
|
|
|
|
|
bool containsOptions() const;
|
|
|
|
|
2004-09-01 16:15:36 +08:00
|
|
|
/** Remove one or more arguments from the argv argument list,
|
|
|
|
* and decrement the argc respectively. */
|
2003-02-19 00:36:42 +08:00
|
|
|
void remove(int pos,int num=1);
|
|
|
|
|
2004-09-01 16:15:36 +08:00
|
|
|
/** Return true if the specified argument matches the given string. */
|
2004-03-20 06:03:09 +08:00
|
|
|
bool match(int pos, const std::string& str) const;
|
|
|
|
|
2004-09-01 16:15:36 +08:00
|
|
|
/** Search for an occurance of a string in the argument list. If found,
|
|
|
|
* remove that occurance and return true. Otherwise, return false. */
|
2003-02-19 00:36:42 +08:00
|
|
|
bool read(const std::string& str);
|
2004-03-20 06:03:09 +08:00
|
|
|
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);
|
2005-02-01 17:00:10 +08:00
|
|
|
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);
|
2004-03-20 06:03:09 +08:00
|
|
|
|
|
|
|
|
2005-02-01 17:00:10 +08:00
|
|
|
/** If the argument value at the specified position matches the given string,
|
2004-09-01 16:15:36 +08:00
|
|
|
* and subsequent parameters are also matched, then set the parameter values,
|
|
|
|
* remove the arguments from the list, and return true. Otherwise, return false. */
|
2004-03-20 06:03:09 +08:00
|
|
|
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);
|
2005-02-01 17:00:10 +08:00
|
|
|
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);
|
2003-11-25 18:56:12 +08:00
|
|
|
|
2003-02-19 00:36:42 +08:00
|
|
|
|
|
|
|
enum ErrorSeverity
|
|
|
|
{
|
|
|
|
BENIGN = 0,
|
|
|
|
CRITICAL = 1
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef std::map<std::string,ErrorSeverity> ErrorMessageMap;
|
|
|
|
|
2004-09-01 16:15:36 +08:00
|
|
|
/** Return the error flag, true if an error has occured when reading arguments. */
|
2003-02-19 00:36:42 +08:00
|
|
|
bool errors(ErrorSeverity severity=BENIGN) const;
|
|
|
|
|
2004-09-01 16:15:36 +08:00
|
|
|
/** Report an error message by adding to the ErrorMessageMap. */
|
2003-02-19 00:36:42 +08:00
|
|
|
void reportError(const std::string& message,ErrorSeverity severity=CRITICAL);
|
|
|
|
|
2004-09-01 16:15:36 +08:00
|
|
|
/** For each remaining option, report it as unrecognized. */
|
2003-02-19 00:36:42 +08:00
|
|
|
void reportRemainingOptionsAsUnrecognized(ErrorSeverity severity=BENIGN);
|
|
|
|
|
2004-09-01 16:15:36 +08:00
|
|
|
/** Return the error message, if any has occured. */
|
2003-02-19 00:36:42 +08:00
|
|
|
ErrorMessageMap& getErrorMessageMap() { return _errorMessageMap; }
|
|
|
|
|
2004-09-01 16:15:36 +08:00
|
|
|
/** Return the error message, if any has occured. */
|
2003-02-19 00:36:42 +08:00
|
|
|
const ErrorMessageMap& getErrorMessageMap() const { return _errorMessageMap; }
|
|
|
|
|
2004-09-01 16:15:36 +08:00
|
|
|
/** Write error messages to the given ostream, if at or above the given severiity. */
|
2003-02-19 00:36:42 +08:00
|
|
|
void writeErrorMessages(std::ostream& output,ErrorSeverity sevrity=BENIGN);
|
|
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
2007-02-12 21:39:18 +08:00
|
|
|
int* _argc;
|
|
|
|
char** _argv;
|
|
|
|
ErrorMessageMap _errorMessageMap;
|
|
|
|
ref_ptr<ApplicationUsage> _usage;
|
2003-02-19 00:36:42 +08:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|