CommandLineArguments.hxx.in 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*============================================================================
  2. KWSys - Kitware System Library
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #ifndef @KWSYS_NAMESPACE@_CommandLineArguments_hxx
  11. #define @KWSYS_NAMESPACE@_CommandLineArguments_hxx
  12. #include <@KWSYS_NAMESPACE@/Configure.h>
  13. #include <@KWSYS_NAMESPACE@/Configure.hxx>
  14. #include <string>
  15. #include <vector>
  16. namespace @KWSYS_NAMESPACE@
  17. {
  18. class CommandLineArgumentsInternal;
  19. struct CommandLineArgumentsCallbackStructure;
  20. /** \class CommandLineArguments
  21. * \brief Command line arguments processing code.
  22. *
  23. * Find specified arguments with optional options and execute specified methods
  24. * or set given variables.
  25. *
  26. * The two interfaces it knows are callback based and variable based. For
  27. * callback based, you have to register callback for particular argument using
  28. * AddCallback method. When that argument is passed, the callback will be
  29. * called with argument, value, and call data. For boolean (NO_ARGUMENT)
  30. * arguments, the value is "1". If the callback returns 0 the argument parsing
  31. * will stop with an error.
  32. *
  33. * For the variable interface you associate variable with each argument. When
  34. * the argument is specified, the variable is set to the specified value casted
  35. * to the appropriate type. For boolean (NO_ARGUMENT), the value is "1".
  36. *
  37. * Both interfaces can be used at the same time.
  38. *
  39. * Possible argument types are:
  40. * NO_ARGUMENT - The argument takes no value : --A
  41. * CONCAT_ARGUMENT - The argument takes value after no space : --Aval
  42. * SPACE_ARGUMENT - The argument takes value after space : --A val
  43. * EQUAL_ARGUMENT - The argument takes value after equal : --A=val
  44. * MULTI_ARGUMENT - The argument takes values after space : --A val1 val2 val3 ...
  45. *
  46. * Example use:
  47. *
  48. * kwsys::CommandLineArguments arg;
  49. * arg.Initialize(argc, argv);
  50. * typedef kwsys::CommandLineArguments argT;
  51. * arg.AddArgument("--something", argT::EQUAL_ARGUMENT, &some_variable,
  52. * "This is help string for --something");
  53. * if ( !arg.Parse() )
  54. * {
  55. * std::cerr << "Problem parsing arguments" << std::endl;
  56. * res = 1;
  57. * }
  58. *
  59. */
  60. class @KWSYS_NAMESPACE@_EXPORT CommandLineArguments
  61. {
  62. public:
  63. CommandLineArguments();
  64. ~CommandLineArguments();
  65. /**
  66. * Various argument types.
  67. */
  68. enum ArgumentTypeEnum {
  69. NO_ARGUMENT,
  70. CONCAT_ARGUMENT,
  71. SPACE_ARGUMENT,
  72. EQUAL_ARGUMENT,
  73. MULTI_ARGUMENT
  74. };
  75. /**
  76. * Various variable types. When using the variable interface, this specifies
  77. * what type the variable is.
  78. */
  79. enum VariableTypeEnum {
  80. NO_VARIABLE_TYPE = 0, // The variable is not specified
  81. INT_TYPE, // The variable is integer (int)
  82. BOOL_TYPE, // The variable is boolean (bool)
  83. DOUBLE_TYPE, // The variable is float (double)
  84. STRING_TYPE, // The variable is string (char*)
  85. STL_STRING_TYPE, // The variable is string (char*)
  86. VECTOR_INT_TYPE, // The variable is integer (int)
  87. VECTOR_BOOL_TYPE, // The variable is boolean (bool)
  88. VECTOR_DOUBLE_TYPE, // The variable is float (double)
  89. VECTOR_STRING_TYPE, // The variable is string (char*)
  90. VECTOR_STL_STRING_TYPE, // The variable is string (char*)
  91. LAST_VARIABLE_TYPE
  92. };
  93. /**
  94. * Prototypes for callbacks for callback interface.
  95. */
  96. typedef int(*CallbackType)(const char* argument, const char* value,
  97. void* call_data);
  98. typedef int(*ErrorCallbackType)(const char* argument, void* client_data);
  99. /**
  100. * Initialize internal data structures. This should be called before parsing.
  101. */
  102. void Initialize(int argc, const char* const argv[]);
  103. void Initialize(int argc, char* argv[]);
  104. /**
  105. * Initialize internal data structure and pass arguments one by one. This is
  106. * convenience method for use from scripting languages where argc and argv
  107. * are not available.
  108. */
  109. void Initialize();
  110. void ProcessArgument(const char* arg);
  111. /**
  112. * This method will parse arguments and call appropriate methods.
  113. */
  114. int Parse();
  115. /**
  116. * This method will add a callback for a specific argument. The arguments to
  117. * it are argument, argument type, callback method, and call data. The
  118. * argument help specifies the help string used with this option. The
  119. * callback and call_data can be skipped.
  120. */
  121. void AddCallback(const char* argument, ArgumentTypeEnum type,
  122. CallbackType callback, void* call_data, const char* help);
  123. /**
  124. * Add handler for argument which is going to set the variable to the
  125. * specified value. If the argument is specified, the option is casted to the
  126. * appropriate type.
  127. */
  128. void AddArgument(const char* argument, ArgumentTypeEnum type,
  129. bool* variable, const char* help);
  130. void AddArgument(const char* argument, ArgumentTypeEnum type,
  131. int* variable, const char* help);
  132. void AddArgument(const char* argument, ArgumentTypeEnum type,
  133. double* variable, const char* help);
  134. void AddArgument(const char* argument, ArgumentTypeEnum type,
  135. char** variable, const char* help);
  136. void AddArgument(const char* argument, ArgumentTypeEnum type,
  137. std::string* variable, const char* help);
  138. /**
  139. * Add handler for argument which is going to set the variable to the
  140. * specified value. If the argument is specified, the option is casted to the
  141. * appropriate type. This will handle the multi argument values.
  142. */
  143. void AddArgument(const char* argument, ArgumentTypeEnum type,
  144. std::vector<bool>* variable, const char* help);
  145. void AddArgument(const char* argument, ArgumentTypeEnum type,
  146. std::vector<int>* variable, const char* help);
  147. void AddArgument(const char* argument, ArgumentTypeEnum type,
  148. std::vector<double>* variable, const char* help);
  149. void AddArgument(const char* argument, ArgumentTypeEnum type,
  150. std::vector<char*>* variable, const char* help);
  151. void AddArgument(const char* argument, ArgumentTypeEnum type,
  152. std::vector<std::string>* variable, const char* help);
  153. /**
  154. * Add handler for boolean argument. The argument does not take any option
  155. * and if it is specified, the value of the variable is true/1, otherwise it
  156. * is false/0.
  157. */
  158. void AddBooleanArgument(const char* argument,
  159. bool* variable, const char* help);
  160. void AddBooleanArgument(const char* argument,
  161. int* variable, const char* help);
  162. void AddBooleanArgument(const char* argument,
  163. double* variable, const char* help);
  164. void AddBooleanArgument(const char* argument,
  165. char** variable, const char* help);
  166. void AddBooleanArgument(const char* argument,
  167. std::string* variable, const char* help);
  168. /**
  169. * Set the callbacks for error handling.
  170. */
  171. void SetClientData(void* client_data);
  172. void SetUnknownArgumentCallback(ErrorCallbackType callback);
  173. /**
  174. * Get remaining arguments. It allocates space for argv, so you have to call
  175. * delete[] on it.
  176. */
  177. void GetRemainingArguments(int* argc, char*** argv);
  178. void DeleteRemainingArguments(int argc, char*** argv);
  179. /**
  180. * If StoreUnusedArguments is set to true, then all unknown arguments will be
  181. * stored and the user can access the modified argc, argv without known
  182. * arguments.
  183. */
  184. void StoreUnusedArguments(bool val) { this->StoreUnusedArgumentsFlag = val; }
  185. void GetUnusedArguments(int* argc, char*** argv);
  186. /**
  187. * Return string containing help. If the argument is specified, only return
  188. * help for that argument.
  189. */
  190. const char* GetHelp() { return this->Help.c_str(); }
  191. const char* GetHelp(const char* arg);
  192. /**
  193. * Get / Set the help line length. This length is used when generating the
  194. * help page. Default length is 80.
  195. */
  196. void SetLineLength(unsigned int);
  197. unsigned int GetLineLength();
  198. /**
  199. * Get the executable name (argv0). This is only available when using
  200. * Initialize with argc/argv.
  201. */
  202. const char* GetArgv0();
  203. /**
  204. * Get index of the last argument parsed. This is the last argument that was
  205. * parsed ok in the original argc/argv list.
  206. */
  207. unsigned int GetLastArgument();
  208. protected:
  209. void GenerateHelp();
  210. //! This is internal method that registers variable with argument
  211. void AddArgument(const char* argument, ArgumentTypeEnum type,
  212. VariableTypeEnum vtype, void* variable, const char* help);
  213. bool GetMatchedArguments(std::vector<std::string>* matches,
  214. const std::string& arg);
  215. //! Populate individual variables
  216. bool PopulateVariable(CommandLineArgumentsCallbackStructure* cs,
  217. const char* value);
  218. //! Populate individual variables of type ...
  219. void PopulateVariable(bool* variable, const std::string& value);
  220. void PopulateVariable(int* variable, const std::string& value);
  221. void PopulateVariable(double* variable, const std::string& value);
  222. void PopulateVariable(char** variable, const std::string& value);
  223. void PopulateVariable(std::string* variable, const std::string& value);
  224. void PopulateVariable(std::vector<bool>* variable, const std::string& value);
  225. void PopulateVariable(std::vector<int>* variable, const std::string& value);
  226. void PopulateVariable(std::vector<double>* variable, const std::string& value);
  227. void PopulateVariable(std::vector<char*>* variable, const std::string& value);
  228. void PopulateVariable(std::vector<std::string>* variable, const std::string& value);
  229. typedef CommandLineArgumentsInternal Internal;
  230. Internal* Internals;
  231. std::string Help;
  232. unsigned int LineLength;
  233. bool StoreUnusedArgumentsFlag;
  234. };
  235. } // namespace @KWSYS_NAMESPACE@
  236. #endif