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

Document ID: Generated on Mon Apr 8 19:24:01 EDT 2013 from SVN revision 125877
Copyright © 2005-2013 VT MÄK. All Rights Reserved (www.mak.com)