OSG  3.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ArgumentParser.h
Go to the documentation of this file.
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 
28 {
29  public:
30 
32  {
33  public:
35  {
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 
77  };
78 
81  static bool isOption(const char* str);
82 
85  static bool isString(const char* str);
86 
88  static bool isNumber(const char* str);
89 
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 
102  int& argc() { return *_argc; }
103 
105  char** argv() { return _argv; }
106 
108  char* operator [] (int pos) { return _argv[pos]; }
109 
111  const char* operator [] (int pos) const { return _argv[pos]; }
112 
114  std::string getApplicationName() const;
115 
118  int find(const std::string& str) const;
119 
122  bool isOption(int pos) const;
123 
126  bool isString(int pos) const;
127 
129  bool isNumber(int pos) const;
130 
131  bool containsOptions() const;
132 
135  void remove(int pos,int num=1);
136 
138  bool match(int pos, const std::string& str) const;
139 
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 
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 
168  {
169  BENIGN = 0,
170  CRITICAL = 1
171  };
172 
173  typedef std::map<std::string,ErrorSeverity> ErrorMessageMap;
174 
176  bool errors(ErrorSeverity severity=BENIGN) const;
177 
179  void reportError(const std::string& message,ErrorSeverity severity=CRITICAL);
180 
182  void reportRemainingOptionsAsUnrecognized(ErrorSeverity severity=BENIGN);
183 
185  ErrorMessageMap& getErrorMessageMap() { return _errorMessageMap; }
186 
188  const ErrorMessageMap& getErrorMessageMap() const { return _errorMessageMap; }
189 
191  void writeErrorMessages(std::ostream& output,ErrorSeverity sevrity=BENIGN);
192 
193 
199  ApplicationUsage::Type readHelpType();
200 
201 
202  protected:
203 
204  int* _argc;
205  char** _argv;
206  ErrorMessageMap _errorMessageMap;
208 
209 };
210 
211 }
212 
213 #endif
#define OSG_EXPORT
Definition: Export.h:43
const ApplicationUsage * getApplicationUsage() const
Parameter(std::string &value)
ErrorMessageMap & getErrorMessageMap()
ApplicationUsage * getApplicationUsage()
ErrorMessageMap _errorMessageMap
const ErrorMessageMap & getErrorMessageMap() const
std::map< std::string, ErrorSeverity > ErrorMessageMap
Parameter(const Parameter &param)
void setApplicationUsage(ApplicationUsage *usage)
Definition: AlphaFunc.h:19
ref_ptr< ApplicationUsage > _usage
Parameter(unsigned int &value)