ArgumentParser 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. #ifndef OSG_ARGUMENTPARSER
  14. #define OSG_ARGUMENTPARSER 1
  15. #include <osg/Export>
  16. #include <osg/ref_ptr>
  17. #include <osg/ApplicationUsage>
  18. #include <map>
  19. #include <string>
  20. #include <ostream>
  21. namespace osg {
  22. class OSG_EXPORT ArgumentParser
  23. {
  24. public:
  25. class OSG_EXPORT Parameter
  26. {
  27. public:
  28. enum ParameterType
  29. {
  30. BOOL_PARAMETER,
  31. FLOAT_PARAMETER,
  32. DOUBLE_PARAMETER,
  33. INT_PARAMETER,
  34. UNSIGNED_INT_PARAMETER,
  35. STRING_PARAMETER
  36. };
  37. union ValueUnion
  38. {
  39. bool* _bool;
  40. float* _float;
  41. double* _double;
  42. int* _int;
  43. unsigned int* _uint;
  44. std::string* _string;
  45. };
  46. Parameter(bool& value) { _type = BOOL_PARAMETER; _value._bool = &value; }
  47. Parameter(float& value) { _type = FLOAT_PARAMETER; _value._float = &value; }
  48. Parameter(double& value) { _type = DOUBLE_PARAMETER; _value._double = &value; }
  49. Parameter(int& value) { _type = INT_PARAMETER; _value._int = &value; }
  50. Parameter(unsigned int& value) { _type = UNSIGNED_INT_PARAMETER; _value._uint = &value; }
  51. Parameter(std::string& value) { _type = STRING_PARAMETER; _value._string = &value; }
  52. Parameter(const Parameter& param) { _type = param._type; _value = param._value; }
  53. Parameter& operator = (const Parameter& param) { _type = param._type; _value = param._value; return *this; }
  54. bool valid(const char* str) const;
  55. bool assign(const char* str);
  56. protected:
  57. ParameterType _type;
  58. ValueUnion _value;
  59. };
  60. /** Return true if the specified string is an option in the form
  61. * -option or --option. */
  62. static bool isOption(const char* str);
  63. /** Return true if string is non-NULL and not an option in the form
  64. * -option or --option. */
  65. static bool isString(const char* str);
  66. /** Return true if specified parameter is a number. */
  67. static bool isNumber(const char* str);
  68. /** Return true if specified parameter is a bool. */
  69. static bool isBool(const char* str);
  70. public:
  71. ArgumentParser(int* argc,char **argv);
  72. void setApplicationUsage(ApplicationUsage* usage) { _usage = usage; }
  73. ApplicationUsage* getApplicationUsage() { return _usage.get(); }
  74. const ApplicationUsage* getApplicationUsage() const { return _usage.get(); }
  75. /** Return the argument count. */
  76. int& argc() { return *_argc; }
  77. /** Return the argument array. */
  78. char** argv() { return _argv; }
  79. /** Return the char* argument at the specified position. */
  80. char* operator [] (int pos) { return _argv[pos]; }
  81. /** Return the const char* argument at the specified position. */
  82. const char* operator [] (int pos) const { return _argv[pos]; }
  83. /** Return the application name, as specified by argv[0] */
  84. std::string getApplicationName() const;
  85. /** Return the position of an occurrence of a string in the argument list.
  86. * Return -1 if no string is found. */
  87. int find(const std::string& str) const;
  88. /** Return true if the specified parameter is an option in the form of
  89. * -option or --option. */
  90. bool isOption(int pos) const;
  91. /** Return true if the specified parameter is a string not in
  92. * the form of an option. */
  93. bool isString(int pos) const;
  94. /** Return true if the specified parameter is a number. */
  95. bool isNumber(int pos) const;
  96. bool containsOptions() const;
  97. /** Remove one or more arguments from the argv argument list,
  98. * and decrement the argc respectively. */
  99. void remove(int pos,int num=1);
  100. /** Return true if the specified argument matches the given string. */
  101. bool match(int pos, const std::string& str) const;
  102. /** Search for an occurrence of a string in the argument list. If found,
  103. * remove that occurrence and return true. Otherwise, return false. */
  104. bool read(const std::string& str);
  105. bool read(const std::string& str, Parameter value1);
  106. bool read(const std::string& str, Parameter value1, Parameter value2);
  107. bool read(const std::string& str, Parameter value1, Parameter value2, Parameter value3);
  108. bool read(const std::string& str, Parameter value1, Parameter value2, Parameter value3, Parameter value4);
  109. bool read(const std::string& str, Parameter value1, Parameter value2, Parameter value3, Parameter value4, Parameter value5);
  110. bool read(const std::string& str, Parameter value1, Parameter value2, Parameter value3, Parameter value4, Parameter value5, Parameter value6);
  111. bool read(const std::string& str, Parameter value1, Parameter value2, Parameter value3, Parameter value4, Parameter value5, Parameter value6, Parameter value7);
  112. bool read(const std::string& str, Parameter value1, Parameter value2, Parameter value3, Parameter value4, Parameter value5, Parameter value6, Parameter value7, Parameter value8);
  113. /** If the argument value at the specified position matches the given string,
  114. * and subsequent parameters are also matched, then set the parameter values,
  115. * remove the arguments from the list, and return true. Otherwise, return false. */
  116. bool read(int pos, const std::string& str);
  117. bool read(int pos, const std::string& str, Parameter value1);
  118. bool read(int pos, const std::string& str, Parameter value1, Parameter value2);
  119. bool read(int pos, const std::string& str, Parameter value1, Parameter value2, Parameter value3);
  120. bool read(int pos, const std::string& str, Parameter value1, Parameter value2, Parameter value3, Parameter value4);
  121. bool read(int pos, const std::string& str, Parameter value1, Parameter value2, Parameter value3, Parameter value4, Parameter value5);
  122. bool read(int pos, const std::string& str, Parameter value1, Parameter value2, Parameter value3, Parameter value4, Parameter value5, Parameter value6);
  123. bool read(int pos, const std::string& str, Parameter value1, Parameter value2, Parameter value3, Parameter value4, Parameter value5, Parameter value6, Parameter value7);
  124. bool read(int pos, const std::string& str, Parameter value1, Parameter value2, Parameter value3, Parameter value4, Parameter value5, Parameter value6, Parameter value7, Parameter value8);
  125. enum ErrorSeverity
  126. {
  127. BENIGN = 0,
  128. CRITICAL = 1
  129. };
  130. typedef std::map<std::string,ErrorSeverity> ErrorMessageMap;
  131. /** Return the error flag, true if an error has occurred when reading arguments. */
  132. bool errors(ErrorSeverity severity=BENIGN) const;
  133. /** Report an error message by adding to the ErrorMessageMap. */
  134. void reportError(const std::string& message,ErrorSeverity severity=CRITICAL);
  135. /** For each remaining option, report it as unrecognized. */
  136. void reportRemainingOptionsAsUnrecognized(ErrorSeverity severity=BENIGN);
  137. /** Return the error message, if any has occurred. */
  138. ErrorMessageMap& getErrorMessageMap() { return _errorMessageMap; }
  139. /** Return the error message, if any has occurred. */
  140. const ErrorMessageMap& getErrorMessageMap() const { return _errorMessageMap; }
  141. /** Write error messages to the given ostream, if at or above the given severity. */
  142. void writeErrorMessages(std::ostream& output,ErrorSeverity sevrity=BENIGN);
  143. /** This convenience method handles help requests on the command line.
  144. * Return the type(s) of help requested. The return value of this
  145. * function is suitable for passing into getApplicationUsage()->write().
  146. * If ApplicationUsage::NO_HELP is returned then no help commandline option
  147. * was found on the command line. */
  148. ApplicationUsage::Type readHelpType();
  149. protected:
  150. int* _argc;
  151. char** _argv;
  152. ErrorMessageMap _errorMessageMap;
  153. ref_ptr<ApplicationUsage> _usage;
  154. };
  155. }
  156. #endif