CommandLineArguments.hxx.in 10 KB

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