CommandLineArguments.hxx.in 9.8 KB

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