VR-Forces Developer's Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
argumentParser.h
Go to the documentation of this file.
1 /*******************************************************************************
2 ** Copyright (c) 2004 MAK Technologies, Inc.
3 ** All rights reserved.
4 *******************************************************************************/
5 /*******************************************************************************
6 ** $RCSfile: argumentParser.h,v $ $Revision: 1.2 $ $State: Exp $
7 *******************************************************************************/
8 #ifndef argumentParser_H_
9 #define argumentParser_H_
10 
11 //
12 // \file argumentParser.h
13 // \brief This file contains the declaration and description of the DtArgumentParser class.
14 //
15 
16 #include "tdbutil/tdbUtilDefines.h"
17 #include <vlutil/vlConfig.h>
18 #include <matrix/vlVector.h>
19 #include "tdbutil/arguments.h"
21 #include <sstream>
22 
23 
24 class DtFilename;
25 
26 //
27 // DtArgumentParser is a base class for command-line-argument parsers.
28 // \note The class parses instances of DtArgument classes, so while primarily
29 // intended for command-line argument parsing, it doesn't matter how the
30 // DtArguments object was created as long as it is well-formed.
31 //
32 // Derive additional argument parsing classes from this one for your specific
33 // application. Simply override the appropriate parse function for the arguments
34 // your application is concerned with.
35 // \note This base class assumes that all arguments (not the options for those
36 // arguments) are "switches", which means that they all begin with '-'. If
37 // your application does not enforce that condition, override the processNonSwitch()
38 // virtual function.
39 //
41 {
42 
43 public:
45 
46  virtual ~DtArgumentParser() = 0;
47 
48  // Begins the parsing process. Call this function when you are ready
49  // to handle the arguments.
50  void processArguments(const DtArguments& arguments);
51 
52 protected:
53 
54  // \return the current argument.
55  std::string getArgument() const;
56 
57  // Returns the number of arguments remaining to be processed. Does not
58  // differentiate between the types of arguments (switches, non-switches, etc).
59  unsigned numArgumentsLeft() const;
60 
61  // \name Argument Reading Functions
63 
64  // \return the current argument as a string. No error-checking performed.
65  void readString(std::string& stringRead);
66 
67  // \return the current argument as a string. No error-checking performed.
68  // \note Does \b not increment the argument iterator. Useful for checking that
69  // the next argument is the desired type/value before incrementing the iterator
70  void peekString(std::string& stringRead) const;
71 
72  // \return the current argument as a file name. If the current argument is not
73  // a valid filename (validFilename()), it throws an exception.
74  void readFilename(DtFilename& filenameRead);
75 
76  // \return the current argument as a double. If the current argument is not
77  // a valid double (validDouble()), it throws an exception.
78  void readDouble(double& doubleRead);
79 
80  // \return the current argument as a a vector of \b 3 doubles. If any number is
81  // not a valid double (validDouble()), it throws an exception.
82  void readVector(DtVector& vectorRead);
83 
84  // \return the current argument as an int. If the current argument is not
85  // a valid int (validInt()), it throws an exception.
86  void readInt(int& intRead);
87 
88  // \return the current argument as an unsigned int. If the current argument
89  // is not a valid unsigned int (validUnsigned()), it throws an exception.
90  void readUnsigned(unsigned& unsignedRead);
91 
92  // \return the current argument as a bool. 'y' or 'yes' result in true.
93  // 'n' or 'no' result in false.
94  // Anything else throws an exception.
95  void readBool(bool& boolRead);
97 
98 
99  // \name Argument Validation Functions
101  //
102  // \return true if a filename is valid.
103  // \note Currently doesn't check anything, but is there for future enhancement.
104  bool validFilename(const std::string& argument) const;
105 
106  // \return true if the string is a valid integer.
107  bool validInt(const std::string& argument) const;
108 
109  // \return true if the string is a valid unsigned integer.
110  bool validUnsigned(const std::string& argument) const;
111 
112  // \return true if the string is a valid double precisions floating-point number.
113  bool validDouble(const std::string& argument) const;
115 
116 
117  // \name Error-Handling Functions
119  // Throws an exception. Intended for use with the the
120  // "I-don't-know-what-this-argument-is" case.
121  void unknownArgument() const;
122 
123  // Throws an exception with the given error message
124  void argumentError(const char* message) const;
126 
127  // \return The lower-case version of the input.
128  std::string convertToLower(const std::string& inputString) const;
129 
130  // Defines the handling for arguments which are not directly supported by this
131  // object. They may be handled by another parser or they may result
132  // in an UnknownArgument() call.
133  virtual void bounce();
134 
135  // Determines if the specified argument is a switch or not, and calls the
136  // appropriate function accordingly.
137  // \see processSwitch
138  // \see processNonSwitch
139  virtual void process(const std::string& argument);
140 
141  // Process arguments which are actually switches. That is to say, they have
142  // the '-' character first in their chars.
143  virtual void processSwitch(const std::string& argument);
144 
145  // Process arguments that are \b not switches. This includes all arguments
146  // that do not have the '-' character first.
147  // \see process
148  virtual void processNonSwitch(const std::string& argument);
149 
150  // \name Individual Parsing Functions
151  // Makes it easy for derived classes to parse the arguments they are
152  // concerned with. Override the specific ones you need to handle, and the
153  // remaining will call bounce().
154  // \note The parse functions can throw DtArgumentExceptions or
155  // a derived type such as DtUnknownArgumentException.
157  virtual void parseA();
158  virtual void parseB();
159  virtual void parseC();
160  virtual void parseD();
161  virtual void parseE();
162  virtual void parseF();
163  virtual void parseG();
164  virtual void parseH();
165  virtual void parseI();
166  virtual void parseJ();
167  virtual void parseK();
168  virtual void parseL();
169  virtual void parseM();
170  virtual void parseN();
171  virtual void parseO();
172  virtual void parseP();
173  virtual void parseQ();
174  virtual void parseR();
175  virtual void parseS();
176  virtual void parseT();
177  virtual void parseU();
178  virtual void parseV();
179  virtual void parseW();
180  virtual void parseX();
181  virtual void parseY();
182  virtual void parseZ();
183  virtual void parseDoubleDash();
185 
186 private:
187  // not implemented
188  DtArgumentParser& operator=(const DtArgumentParser& other);
189  DtArgumentParser(const DtArgumentParser& other);
190 
191  // If the first character of the string is '-', then it is a switch
192  // \return A boolean indicating whether the argument is a switch or not.
193  bool isSwitch(const std::string& argument) const;
194 
195  // Throws an exception when value is not of type expectedType
196  void errorInRead(const std::string& value, const std::string& expectedType) const;
197 
198  // This is intended as an easy way of giving useful feedback.
199  // An example is best: if you call with (3, "A VECTOR") while reading the
200  // -foo argument, it will throw an exception with the message "An error was
201  // encountered in the -foo argument:\n Expected to find A VECTOR", if there
202  // are not at least three more arguments. Note the types/values of the
203  // arguments are not looked at - it merely examines the number of arguments.
204  void validateExistence(unsigned numExpectedArguments,
205  const std::string& expectedArgumentsDescription) const;
206 
208  std::string myCurrentArgument;
210 };
211 
212 
213 // Identifies an invalid or unknown argument, such as -hhelp or just plain
214 // garbage like -asdf
216 {
217 public:
218  inline DtUnknownArgumentException(const char* errorMessage,
219  const DtException *previousException = 0) throw();
220 };
221 
223  const char* errorMessage, const DtException *previousException) throw() :
224  DtArgumentException(errorMessage)
225 {
226 }
227 
228 
229 
230 // Attempts to convert a specified string input into an instance of the type
231 // specified by the destination parameter.
232 //
233 // \return A boolean indicating success or failure at conversion.
234 //
235 template< class T >
236 bool DtConvert(const std::string& input, T& destination)
237 {
238  std::istringstream convert(input);
239  convert >> destination;
240 
241  return !convert.fail();
242 }
243 
244 
245 
246 #endif
247 
248 
249 
Definition: argumentException.h:21
const DtArguments * myArguments
Definition: argumentParser.h:209
Definition: arguments.h:27
DtArguments::DtArgumentConstIter myArgumentsIter
Definition: argumentParser.h:207
Definition: argumentParser.h:215
DtArgumentContainer::const_iterator DtArgumentConstIter
Definition: arguments.h:33
std::string myCurrentArgument
Definition: argumentParser.h:208
bool DtConvert(const std::string &input, T &destination)
Definition: argumentParser.h:236
Definition: argumentParser.h:40
DtUnknownArgumentException(const char *errorMessage, const DtException *previousException=0)
Definition: argumentParser.h:222
#define DT_DLL_tdbutil
Definition: tdbUtilDefines.h:23

Document ID: Generated on Wed Mar 27 22:49:11 EDT 2024 from SVN revision 264633
Copyright © 2005-2024 MAK Technologies. All Rights Reserved (www.mak.com)