You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

187 lines
5.4KB

  1. /************************************************************************/
  2. /*! \class CommandLine
  3. \brief Command-line opition parser.
  4. Copyright (c) 2005 Robin Davies.
  5. Permission is hereby granted, free of charge, to any person
  6. obtaining a copy of this software and associated documentation files
  7. (the "Software"), to deal in the Software without restriction,
  8. including without limitation the rights to use, copy, modify, merge,
  9. publish, distribute, sublicense, and/or sell copies of the Software,
  10. and to permit persons to whom the Software is furnished to do so,
  11. subject to the following conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  17. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
  18. ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  19. CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  20. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. */
  22. /************************************************************************/
  23. #ifndef STDOPT_H
  24. #define STDOPT_H
  25. #include <vector>
  26. #include <string>
  27. #include <sstream>
  28. #include <exception>
  29. namespace stdopt
  30. {
  31. class CommandLineException: public std::exception {
  32. public:
  33. CommandLineException(const std::string &error)
  34. {
  35. s = error;
  36. }
  37. const char*what() { return s.c_str(); }
  38. private:
  39. std::string s;
  40. };
  41. class CommandLine
  42. {
  43. public:
  44. CommandLine();
  45. virtual ~CommandLine();
  46. void ProcessCommandLine(int argc, char**argv);
  47. void ProcessCommandLine(const std::vector<std::string>& cmdline);
  48. template <class TVAL> void AddOption(const char*name,TVAL*pResult, TVAL defaultValue)
  49. {
  50. this->optionHandlers.push_back(new COptionHandler<TVAL>(name,pResult));
  51. *pResult = defaultValue;
  52. }
  53. template <class TVAL> void AddOption(const char*name,TVAL*pResult)
  54. {
  55. this->optionHandlers.push_back(new COptionHandler<TVAL>(name,pResult));
  56. }
  57. const std::vector<std::string> &GetArguments() { return args; }
  58. template <class T> void GetArgument(size_t arg, T*pVal)
  59. {
  60. if (arg >= args.size()) {
  61. std::stringstream os;
  62. os << "Argument " << (arg+1) << " not provided.";
  63. throw CommandLineException(os.str());
  64. }
  65. std::stringstream is(args[arg]);
  66. T value;
  67. is >> value;
  68. if (!is.fail() && is.eof())
  69. {
  70. *pVal = value;
  71. } else {
  72. std::stringstream os;
  73. os << "Argument " << (arg+1) << " was not in the correct format.";
  74. throw CommandLineException(os.str());
  75. }
  76. }
  77. void GetArgument(size_t arg, std::string*pVal)
  78. {
  79. if (arg >= args.size()) {
  80. std::stringstream os;
  81. os << "Argument " << (arg+1) << " not provided.";
  82. throw CommandLineException(os.str());
  83. }
  84. *pVal = args[arg];
  85. }
  86. private:
  87. class COptionHandlerBase {
  88. public:
  89. COptionHandlerBase(const std::string & name) { this->name = name;}
  90. virtual ~COptionHandlerBase() { };
  91. const std::string &getName() const { return name; }
  92. virtual bool HasArgument()const = 0;
  93. virtual void Process(const char* value) const = 0;
  94. private:
  95. std::string name;
  96. };
  97. template <class T> class COptionHandler: public COptionHandlerBase {
  98. public:
  99. COptionHandler(const std::string &name,T *result, T defaultValue = T())
  100. : COptionHandlerBase(name)
  101. {
  102. _pResult = result;
  103. *_pResult = defaultValue;
  104. }
  105. virtual bool HasArgument() const;
  106. virtual void Process(const char *strValue) const {
  107. std::stringstream is(strValue);
  108. T value;
  109. is >> value;
  110. if (!is.fail() && is.eof())
  111. {
  112. *_pResult = value;
  113. } else {
  114. std::stringstream os;
  115. os << "Invalid value provided for option '" << getName() << "'.";
  116. throw CommandLineException(os.str().c_str());
  117. }
  118. }
  119. private:
  120. std::string strName;
  121. T*_pResult;
  122. };
  123. const CommandLine::COptionHandlerBase*GetOptionHandler(const std::string &name) const;
  124. std::vector<std::string > args;
  125. std::vector<COptionHandlerBase*> optionHandlers;
  126. };
  127. // Argument specializations for bool.
  128. template <class T> bool CommandLine::COptionHandler<T>::HasArgument()const {
  129. return true;
  130. };
  131. inline bool CommandLine::COptionHandler<bool>::HasArgument() const {
  132. return false;
  133. }
  134. inline void CommandLine::COptionHandler<bool>::Process(const char*strValue) const {
  135. if (strValue == NULL)
  136. {
  137. *_pResult = true;
  138. return;
  139. }
  140. switch (*strValue)
  141. {
  142. case '\0':
  143. case '+':
  144. *_pResult = true;
  145. break;
  146. case '-':
  147. *_pResult = false;
  148. break;
  149. default:
  150. throw CommandLineException("Please specify '+' or '-' for boolean options.");
  151. }
  152. }
  153. // specializations for std::string.
  154. inline void CommandLine::COptionHandler<std::string >::Process(const char*strValue)const {
  155. *_pResult = strValue;
  156. }
  157. // specializations for std::vector<std::string>
  158. inline void CommandLine::COptionHandler<std::vector<std::string> >::Process(const char*strValue)const {
  159. _pResult->push_back(strValue);
  160. }
  161. };
  162. #endif