1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
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.
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.
14#ifndef OSG_ARGUMENTPARSER
15#define OSG_ARGUMENTPARSER 1
19#include <osg/ApplicationUsage>
27class OSG_EXPORT ArgumentParser
31 class OSG_EXPORT Parameter
40 UNSIGNED_INT_PARAMETER,
54 Parameter(bool& value) { _type = BOOL_PARAMETER; _value._bool = &value; }
56 Parameter(float& value) { _type = FLOAT_PARAMETER; _value._float = &value; }
58 Parameter(double& value) { _type = DOUBLE_PARAMETER; _value._double = &value; }
60 Parameter(int& value) { _type = INT_PARAMETER; _value._int = &value; }
62 Parameter(unsigned int& value) { _type = UNSIGNED_INT_PARAMETER; _value._uint = &value; }
64 Parameter(std::string& value) { _type = STRING_PARAMETER; _value._string = &value; }
66 Parameter(const Parameter& param) { _type = param._type; _value = param._value; }
68 Parameter& operator = (const Parameter& param) { _type = param._type; _value = param._value; return *this; }
70 bool valid(const char* str) const;
71 bool assign(const char* str);
79 /** Return true if the specified string is an option in the form
80 * -option or --option. */
81 static bool isOption(const char* str);
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);
87 /** Return true if specified parameter is a number. */
88 static bool isNumber(const char* str);
90 /** Return true if specified parameter is a bool. */
91 static bool isBool(const char* str);
95 ArgumentParser(int* argc,char **argv);
97 void setApplicationUsage(ApplicationUsage* usage) { _usage = usage; }
98 ApplicationUsage* getApplicationUsage() { return _usage.get(); }
99 const ApplicationUsage* getApplicationUsage() const { return _usage.get(); }
101 /** Return the argument count. */
102 int& argc() { return *_argc; }
104 /** Return the argument array. */
105 char** argv() { return _argv; }
107 /** Return the char* argument at the specified position. */
108 char* operator [] (int pos) { return _argv[pos]; }
110 /** Return the const char* argument at the specified position. */
111 const char* operator [] (int pos) const { return _argv[pos]; }
113 /** Return the application name, as specified by argv[0] */
114 std::string getApplicationName() const;
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;
120 /** Return true if the specified parameter is an option in the form of
121 * -option or --option. */
122 bool isOption(int pos) const;
124 /** Return true if the specified parameter is a string not in
125 * the form of an option. */
126 bool isString(int pos) const;
128 /** Return true if the specified parameter is a number. */
129 bool isNumber(int pos) const;
131 bool containsOptions() const;
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);
137 /** Return true if the specified argument matches the given string. */
138 bool match(int pos, const std::string& str) const;
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);
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);
173 typedef std::map<std::string,ErrorSeverity> ErrorMessageMap;
175 /** Return the error flag, true if an error has occurred when reading arguments. */
176 bool errors(ErrorSeverity severity=BENIGN) const;
178 /** Report an error message by adding to the ErrorMessageMap. */
179 void reportError(const std::string& message,ErrorSeverity severity=CRITICAL);
181 /** For each remaining option, report it as unrecognized. */
182 void reportRemainingOptionsAsUnrecognized(ErrorSeverity severity=BENIGN);
184 /** Return the error message, if any has occurred. */
185 ErrorMessageMap& getErrorMessageMap() { return _errorMessageMap; }
187 /** Return the error message, if any has occurred. */
188 const ErrorMessageMap& getErrorMessageMap() const { return _errorMessageMap; }
190 /** Write error messages to the given ostream, if at or above the given severity. */
191 void writeErrorMessages(std::ostream& output,ErrorSeverity sevrity=BENIGN);
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();
206 ErrorMessageMap _errorMessageMap;
207 ref_ptr<ApplicationUsage> _usage;