2003-02-19 00:36:42 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2003 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 <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 {
|
|
|
|
|
2003-02-19 05:58:40 +08:00
|
|
|
// forward declare
|
|
|
|
class ApplicationUsage;
|
|
|
|
|
2003-02-19 00:36:42 +08:00
|
|
|
class SG_EXPORT ArgumentParser
|
|
|
|
{
|
|
|
|
public:
|
2004-03-20 06:03:09 +08:00
|
|
|
|
2004-09-28 17:14:04 +08:00
|
|
|
class SG_EXPORT Parameter
|
2004-03-20 06:03:09 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
enum ParameterType
|
|
|
|
{
|
|
|
|
FLOAT_PARAMETER,
|
|
|
|
DOUBLE_PARAMETER,
|
|
|
|
INT_PARAMETER,
|
|
|
|
UNSIGNED_INT_PARAMETER,
|
|
|
|
STRING_PARAMETER,
|
|
|
|
};
|
|
|
|
|
|
|
|
union ValueUnion
|
|
|
|
{
|
|
|
|
float* _float;
|
|
|
|
double* _double;
|
|
|
|
int* _int;
|
|
|
|
unsigned int* _uint;
|
|
|
|
std::string* _string;
|
|
|
|
};
|
|
|
|
|
|
|
|
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; }
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
2003-02-19 05:58:40 +08:00
|
|
|
ArgumentParser(int* argc,char **argv);
|
|
|
|
|
|
|
|
void setApplicationUsage(ApplicationUsage* usage) { _usage = usage; }
|
|
|
|
ApplicationUsage* getApplicationUsage() { return _usage; }
|
|
|
|
const ApplicationUsage* getApplicationUsage() const { return _usage; }
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
2004-09-01 16:15:36 +08:00
|
|
|
/** If the argument value at the speciified position matches the given string,
|
|
|
|
* 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);
|
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:
|
|
|
|
|
2003-02-19 05:58:40 +08:00
|
|
|
int* _argc;
|
|
|
|
char** _argv;
|
|
|
|
ErrorMessageMap _errorMessageMap;
|
|
|
|
ApplicationUsage* _usage;
|
2003-02-19 00:36:42 +08:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|