| 1 | /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield |
|---|
| 2 | * |
|---|
| 3 | * This library is open source and may be redistributed and/or modified under |
|---|
| 4 | * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or |
|---|
| 5 | * (at your option) any later version. The full license is in LICENSE file |
|---|
| 6 | * included with this distribution, and on the openscenegraph.org website. |
|---|
| 7 | * |
|---|
| 8 | * This library is distributed in the hope that it will be useful, |
|---|
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 11 | * OpenSceneGraph Public License for more details. |
|---|
| 12 | */ |
|---|
| 13 | |
|---|
| 14 | #ifndef OSG_ARGUMENTPARSER |
|---|
| 15 | #define OSG_ARGUMENTPARSER 1 |
|---|
| 16 | |
|---|
| 17 | #include <osg/Export> |
|---|
| 18 | #include <osg/ref_ptr> |
|---|
| 19 | #include <osg/ApplicationUsage> |
|---|
| 20 | |
|---|
| 21 | #include <map> |
|---|
| 22 | #include <string> |
|---|
| 23 | #include <ostream> |
|---|
| 24 | |
|---|
| 25 | namespace osg { |
|---|
| 26 | |
|---|
| 27 | class OSG_EXPORT ArgumentParser |
|---|
| 28 | { |
|---|
| 29 | public: |
|---|
| 30 | |
|---|
| 31 | class OSG_EXPORT Parameter |
|---|
| 32 | { |
|---|
| 33 | public: |
|---|
| 34 | enum ParameterType |
|---|
| 35 | { |
|---|
| 36 | BOOL_PARAMETER, |
|---|
| 37 | FLOAT_PARAMETER, |
|---|
| 38 | DOUBLE_PARAMETER, |
|---|
| 39 | INT_PARAMETER, |
|---|
| 40 | UNSIGNED_INT_PARAMETER, |
|---|
| 41 | STRING_PARAMETER |
|---|
| 42 | }; |
|---|
| 43 | |
|---|
| 44 | union ValueUnion |
|---|
| 45 | { |
|---|
| 46 | bool* _bool; |
|---|
| 47 | float* _float; |
|---|
| 48 | double* _double; |
|---|
| 49 | int* _int; |
|---|
| 50 | unsigned int* _uint; |
|---|
| 51 | std::string* _string; |
|---|
| 52 | }; |
|---|
| 53 | |
|---|
| 54 | Parameter(bool& value) { _type = BOOL_PARAMETER; _value._bool = &value; } |
|---|
| 55 | |
|---|
| 56 | Parameter(float& value) { _type = FLOAT_PARAMETER; _value._float = &value; } |
|---|
| 57 | |
|---|
| 58 | Parameter(double& value) { _type = DOUBLE_PARAMETER; _value._double = &value; } |
|---|
| 59 | |
|---|
| 60 | Parameter(int& value) { _type = INT_PARAMETER; _value._int = &value; } |
|---|
| 61 | |
|---|
| 62 | Parameter(unsigned int& value) { _type = UNSIGNED_INT_PARAMETER; _value._uint = &value; } |
|---|
| 63 | |
|---|
| 64 | Parameter(std::string& value) { _type = STRING_PARAMETER; _value._string = &value; } |
|---|
| 65 | |
|---|
| 66 | Parameter(const Parameter& param) { _type = param._type; _value = param._value; } |
|---|
| 67 | |
|---|
| 68 | Parameter& operator = (const Parameter& param) { _type = param._type; _value = param._value; return *this; } |
|---|
| 69 | |
|---|
| 70 | bool valid(const char* str) const; |
|---|
| 71 | bool assign(const char* str); |
|---|
| 72 | |
|---|
| 73 | protected: |
|---|
| 74 | |
|---|
| 75 | ParameterType _type; |
|---|
| 76 | ValueUnion _value; |
|---|
| 77 | }; |
|---|
| 78 | |
|---|
| 79 | /** Return true if the specified string is an option in the form |
|---|
| 80 | * -option or --option. */ |
|---|
| 81 | static bool isOption(const char* str); |
|---|
| 82 | |
|---|
| 83 | /** Return true if string is non-NULL and not an option in the form |
|---|
| 84 | * -option or --option. */ |
|---|
| 85 | static bool isString(const char* str); |
|---|
| 86 | |
|---|
| 87 | /** Return true if specified parameter is a number. */ |
|---|
| 88 | static bool isNumber(const char* str); |
|---|
| 89 | |
|---|
| 90 | /** Return true if specified parameter is a bool. */ |
|---|
| 91 | static bool isBool(const char* str); |
|---|
| 92 | |
|---|
| 93 | public: |
|---|
| 94 | |
|---|
| 95 | ArgumentParser(int* argc,char **argv); |
|---|
| 96 | |
|---|
| 97 | void setApplicationUsage(ApplicationUsage* usage) { _usage = usage; } |
|---|
| 98 | ApplicationUsage* getApplicationUsage() { return _usage.get(); } |
|---|
| 99 | const ApplicationUsage* getApplicationUsage() const { return _usage.get(); } |
|---|
| 100 | |
|---|
| 101 | /** Return the argument count. */ |
|---|
| 102 | int& argc() { return *_argc; } |
|---|
| 103 | |
|---|
| 104 | /** Return the argument array. */ |
|---|
| 105 | char** argv() { return _argv; } |
|---|
| 106 | |
|---|
| 107 | /** Return the char* argument at the specified position. */ |
|---|
| 108 | char* operator [] (int pos) { return _argv[pos]; } |
|---|
| 109 | |
|---|
| 110 | /** Return the const char* argument at the specified position. */ |
|---|
| 111 | const char* operator [] (int pos) const { return _argv[pos]; } |
|---|
| 112 | |
|---|
| 113 | /** Return the application name, as specified by argv[0] */ |
|---|
| 114 | std::string getApplicationName() const; |
|---|
| 115 | |
|---|
| 116 | /** Return the position of an occurrence of a string in the argument list. |
|---|
| 117 | * Return -1 if no string is found. */ |
|---|
| 118 | int find(const std::string& str) const; |
|---|
| 119 | |
|---|
| 120 | /** Return true if the specified parameter is an option in the form of |
|---|
| 121 | * -option or --option. */ |
|---|
| 122 | bool isOption(int pos) const; |
|---|
| 123 | |
|---|
| 124 | /** Return true if the specified parameter is a string not in |
|---|
| 125 | * the form of an option. */ |
|---|
| 126 | bool isString(int pos) const; |
|---|
| 127 | |
|---|
| 128 | /** Return true if the specified parameter is a number. */ |
|---|
| 129 | bool isNumber(int pos) const; |
|---|
| 130 | |
|---|
| 131 | bool containsOptions() const; |
|---|
| 132 | |
|---|
| 133 | /** Remove one or more arguments from the argv argument list, |
|---|
| 134 | * and decrement the argc respectively. */ |
|---|
| 135 | void remove(int pos,int num=1); |
|---|
| 136 | |
|---|
| 137 | /** Return true if the specified argument matches the given string. */ |
|---|
| 138 | bool match(int pos, const std::string& str) const; |
|---|
| 139 | |
|---|
| 140 | /** Search for an occurrence of a string in the argument list. If found, |
|---|
| 141 | * remove that occurrence and return true. Otherwise, return false. */ |
|---|
| 142 | bool read(const std::string& str); |
|---|
| 143 | bool read(const std::string& str, Parameter value1); |
|---|
| 144 | bool read(const std::string& str, Parameter value1, Parameter value2); |
|---|
| 145 | bool read(const std::string& str, Parameter value1, Parameter value2, Parameter value3); |
|---|
| 146 | bool read(const std::string& str, Parameter value1, Parameter value2, Parameter value3, Parameter value4); |
|---|
| 147 | bool read(const std::string& str, Parameter value1, Parameter value2, Parameter value3, Parameter value4, Parameter value5); |
|---|
| 148 | bool read(const std::string& str, Parameter value1, Parameter value2, Parameter value3, Parameter value4, Parameter value5, Parameter value6); |
|---|
| 149 | bool read(const std::string& str, Parameter value1, Parameter value2, Parameter value3, Parameter value4, Parameter value5, Parameter value6, Parameter value7); |
|---|
| 150 | bool read(const std::string& str, Parameter value1, Parameter value2, Parameter value3, Parameter value4, Parameter value5, Parameter value6, Parameter value7, Parameter value8); |
|---|
| 151 | |
|---|
| 152 | |
|---|
| 153 | /** If the argument value at the specified position matches the given string, |
|---|
| 154 | * and subsequent parameters are also matched, then set the parameter values, |
|---|
| 155 | * remove the arguments from the list, and return true. Otherwise, return false. */ |
|---|
| 156 | bool read(int pos, const std::string& str); |
|---|
| 157 | bool read(int pos, const std::string& str, Parameter value1); |
|---|
| 158 | bool read(int pos, const std::string& str, Parameter value1, Parameter value2); |
|---|
| 159 | bool read(int pos, const std::string& str, Parameter value1, Parameter value2, Parameter value3); |
|---|
| 160 | bool read(int pos, const std::string& str, Parameter value1, Parameter value2, Parameter value3, Parameter value4); |
|---|
| 161 | bool read(int pos, const std::string& str, Parameter value1, Parameter value2, Parameter value3, Parameter value4, Parameter value5); |
|---|
| 162 | bool read(int pos, const std::string& str, Parameter value1, Parameter value2, Parameter value3, Parameter value4, Parameter value5, Parameter value6); |
|---|
| 163 | bool read(int pos, const std::string& str, Parameter value1, Parameter value2, Parameter value3, Parameter value4, Parameter value5, Parameter value6, Parameter value7); |
|---|
| 164 | bool read(int pos, const std::string& str, Parameter value1, Parameter value2, Parameter value3, Parameter value4, Parameter value5, Parameter value6, Parameter value7, Parameter value8); |
|---|
| 165 | |
|---|
| 166 | |
|---|
| 167 | enum ErrorSeverity |
|---|
| 168 | { |
|---|
| 169 | BENIGN = 0, |
|---|
| 170 | CRITICAL = 1 |
|---|
| 171 | }; |
|---|
| 172 | |
|---|
| 173 | typedef std::map<std::string,ErrorSeverity> ErrorMessageMap; |
|---|
| 174 | |
|---|
| 175 | /** Return the error flag, true if an error has occurred when reading arguments. */ |
|---|
| 176 | bool errors(ErrorSeverity severity=BENIGN) const; |
|---|
| 177 | |
|---|
| 178 | /** Report an error message by adding to the ErrorMessageMap. */ |
|---|
| 179 | void reportError(const std::string& message,ErrorSeverity severity=CRITICAL); |
|---|
| 180 | |
|---|
| 181 | /** For each remaining option, report it as unrecognized. */ |
|---|
| 182 | void reportRemainingOptionsAsUnrecognized(ErrorSeverity severity=BENIGN); |
|---|
| 183 | |
|---|
| 184 | /** Return the error message, if any has occurred. */ |
|---|
| 185 | ErrorMessageMap& getErrorMessageMap() { return _errorMessageMap; } |
|---|
| 186 | |
|---|
| 187 | /** Return the error message, if any has occurred. */ |
|---|
| 188 | const ErrorMessageMap& getErrorMessageMap() const { return _errorMessageMap; } |
|---|
| 189 | |
|---|
| 190 | /** Write error messages to the given ostream, if at or above the given severity. */ |
|---|
| 191 | void writeErrorMessages(std::ostream& output,ErrorSeverity sevrity=BENIGN); |
|---|
| 192 | |
|---|
| 193 | |
|---|
| 194 | /** This convenience method handles help requests on the command line. |
|---|
| 195 | * Return the type(s) of help requested. The return value of this |
|---|
| 196 | * function is suitable for passing into getApplicationUsage()->write(). |
|---|
| 197 | * If ApplicationUsage::NO_HELP is returned then no help commandline option |
|---|
| 198 | * was found on the command line. */ |
|---|
| 199 | ApplicationUsage::Type readHelpType(); |
|---|
| 200 | |
|---|
| 201 | |
|---|
| 202 | protected: |
|---|
| 203 | |
|---|
| 204 | int* _argc; |
|---|
| 205 | char** _argv; |
|---|
| 206 | ErrorMessageMap _errorMessageMap; |
|---|
| 207 | ref_ptr<ApplicationUsage> _usage; |
|---|
| 208 | |
|---|
| 209 | }; |
|---|
| 210 | |
|---|
| 211 | } |
|---|
| 212 | |
|---|
| 213 | #endif |
|---|