CommandLineArguments.hxx.in 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. CommandLineArguments(const CommandLineArguments&) = delete;
  59. CommandLineArguments& operator=(const CommandLineArguments&) = delete;
  60. /**
  61. * Various argument types.
  62. */
  63. enum ArgumentTypeEnum
  64. {
  65. NO_ARGUMENT,
  66. CONCAT_ARGUMENT,
  67. SPACE_ARGUMENT,
  68. EQUAL_ARGUMENT,
  69. MULTI_ARGUMENT
  70. };
  71. /**
  72. * Various variable types. When using the variable interface, this specifies
  73. * what type the variable is.
  74. */
  75. enum VariableTypeEnum
  76. {
  77. NO_VARIABLE_TYPE = 0, // The variable is not specified
  78. INT_TYPE, // The variable is integer (int)
  79. BOOL_TYPE, // The variable is boolean (bool)
  80. DOUBLE_TYPE, // The variable is float (double)
  81. STRING_TYPE, // The variable is string (char*)
  82. STL_STRING_TYPE, // The variable is string (char*)
  83. VECTOR_INT_TYPE, // The variable is integer (int)
  84. VECTOR_BOOL_TYPE, // The variable is boolean (bool)
  85. VECTOR_DOUBLE_TYPE, // The variable is float (double)
  86. VECTOR_STRING_TYPE, // The variable is string (char*)
  87. VECTOR_STL_STRING_TYPE, // The variable is string (char*)
  88. LAST_VARIABLE_TYPE
  89. };
  90. /**
  91. * Prototypes for callbacks for callback interface.
  92. */
  93. typedef int (*CallbackType)(const char* argument, const char* value,
  94. void* call_data);
  95. typedef int (*ErrorCallbackType)(const char* argument, void* client_data);
  96. /**
  97. * Initialize internal data structures. This should be called before parsing.
  98. */
  99. void Initialize(int argc, const char* const argv[]);
  100. void Initialize(int argc, char* argv[]);
  101. /**
  102. * Initialize internal data structure and pass arguments one by one. This is
  103. * convenience method for use from scripting languages where argc and argv
  104. * are not available.
  105. */
  106. void Initialize();
  107. void ProcessArgument(const char* arg);
  108. /**
  109. * This method will parse arguments and call appropriate methods.
  110. */
  111. int Parse();
  112. /**
  113. * This method will add a callback for a specific argument. The arguments to
  114. * it are argument, argument type, callback method, and call data. The
  115. * argument help specifies the help string used with this option. The
  116. * callback and call_data can be skipped.
  117. */
  118. void AddCallback(const char* argument, ArgumentTypeEnum type,
  119. CallbackType callback, void* call_data, const char* help);
  120. /**
  121. * Add handler for argument which is going to set the variable to the
  122. * specified value. If the argument is specified, the option is casted to the
  123. * appropriate type.
  124. */
  125. void AddArgument(const char* argument, ArgumentTypeEnum type, bool* variable,
  126. const char* help);
  127. void AddArgument(const char* argument, ArgumentTypeEnum type, int* variable,
  128. const char* help);
  129. void AddArgument(const char* argument, ArgumentTypeEnum type,
  130. double* variable, const char* help);
  131. void AddArgument(const char* argument, ArgumentTypeEnum type,
  132. char** variable, const char* help);
  133. void AddArgument(const char* argument, ArgumentTypeEnum type,
  134. std::string* variable, const char* help);
  135. /**
  136. * Add handler for argument which is going to set the variable to the
  137. * specified value. If the argument is specified, the option is casted to the
  138. * appropriate type. This will handle the multi argument values.
  139. */
  140. void AddArgument(const char* argument, ArgumentTypeEnum type,
  141. std::vector<bool>* variable, const char* help);
  142. void AddArgument(const char* argument, ArgumentTypeEnum type,
  143. std::vector<int>* variable, const char* help);
  144. void AddArgument(const char* argument, ArgumentTypeEnum type,
  145. std::vector<double>* variable, const char* help);
  146. void AddArgument(const char* argument, ArgumentTypeEnum type,
  147. std::vector<char*>* variable, const char* help);
  148. void AddArgument(const char* argument, ArgumentTypeEnum type,
  149. std::vector<std::string>* variable, const char* help);
  150. /**
  151. * Add handler for boolean argument. The argument does not take any option
  152. * and if it is specified, the value of the variable is true/1, otherwise it
  153. * is false/0.
  154. */
  155. void AddBooleanArgument(const char* argument, bool* variable,
  156. const char* help);
  157. void AddBooleanArgument(const char* argument, int* variable,
  158. const char* help);
  159. void AddBooleanArgument(const char* argument, double* variable,
  160. const char* help);
  161. void AddBooleanArgument(const char* argument, char** variable,
  162. const char* help);
  163. void AddBooleanArgument(const char* argument, std::string* variable,
  164. const char* help);
  165. /**
  166. * Set the callbacks for error handling.
  167. */
  168. void SetClientData(void* client_data);
  169. void SetUnknownArgumentCallback(ErrorCallbackType callback);
  170. /**
  171. * Get remaining arguments. It allocates space for argv, so you have to call
  172. * delete[] on it.
  173. */
  174. void GetRemainingArguments(int* argc, char*** argv);
  175. void DeleteRemainingArguments(int argc, char*** argv);
  176. /**
  177. * If StoreUnusedArguments is set to true, then all unknown arguments will be
  178. * stored and the user can access the modified argc, argv without known
  179. * arguments.
  180. */
  181. void StoreUnusedArguments(bool val) { this->StoreUnusedArgumentsFlag = val; }
  182. void GetUnusedArguments(int* argc, char*** argv);
  183. /**
  184. * Return string containing help. If the argument is specified, only return
  185. * help for that argument.
  186. */
  187. const char* GetHelp() { return this->Help.c_str(); }
  188. const char* GetHelp(const char* arg);
  189. /**
  190. * Get / Set the help line length. This length is used when generating the
  191. * help page. Default length is 80.
  192. */
  193. void SetLineLength(unsigned int);
  194. unsigned int GetLineLength();
  195. /**
  196. * Get the executable name (argv0). This is only available when using
  197. * Initialize with argc/argv.
  198. */
  199. const char* GetArgv0();
  200. /**
  201. * Get index of the last argument parsed. This is the last argument that was
  202. * parsed ok in the original argc/argv list.
  203. */
  204. unsigned int GetLastArgument();
  205. protected:
  206. void GenerateHelp();
  207. //! This is internal method that registers variable with argument
  208. void AddArgument(const char* argument, ArgumentTypeEnum type,
  209. VariableTypeEnum vtype, void* variable, const char* help);
  210. bool GetMatchedArguments(std::vector<std::string>* matches,
  211. const std::string& arg);
  212. //! Populate individual variables
  213. bool PopulateVariable(CommandLineArgumentsCallbackStructure* cs,
  214. const char* value);
  215. //! Populate individual variables of type ...
  216. void PopulateVariable(bool* variable, const std::string& value);
  217. void PopulateVariable(int* variable, const std::string& value);
  218. void PopulateVariable(double* variable, const std::string& value);
  219. void PopulateVariable(char** variable, const std::string& value);
  220. void PopulateVariable(std::string* variable, const std::string& value);
  221. void PopulateVariable(std::vector<bool>* variable, const std::string& value);
  222. void PopulateVariable(std::vector<int>* variable, const std::string& value);
  223. void PopulateVariable(std::vector<double>* variable,
  224. const std::string& value);
  225. void PopulateVariable(std::vector<char*>* variable,
  226. const std::string& value);
  227. void PopulateVariable(std::vector<std::string>* variable,
  228. 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