CommandLineArguments.hxx.in 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. /* Define this macro temporarily to keep the code readable. */
  16. #if !defined (KWSYS_NAMESPACE) && !@KWSYS_NAMESPACE@_NAME_IS_KWSYS
  17. # define kwsys_stl @KWSYS_NAMESPACE@_stl
  18. #endif
  19. namespace @KWSYS_NAMESPACE@
  20. {
  21. class CommandLineArgumentsInternal;
  22. /** \class CommandLineArguments
  23. * \brief Command line arguments processing code.
  24. *
  25. * Find specified arguments with optional options and execute specified methods
  26. * or set given variables.
  27. *
  28. * The two interfaces it knows are callback based and variable based. For
  29. * callback based, you have to register callback for particular argument using
  30. * AddCallback method. When that argument is passed, the callback will be
  31. * called with argument, value, and call data. For boolean (NO_ARGUMENT)
  32. * arguments, the value is "1". If the callback returns 0 the argument parsing
  33. * will stop with an error.
  34. *
  35. * For the variable interface you associate variable with each argument. When
  36. * the argument is specified, the variable is set to the specified value casted
  37. * to the apropriate type. For boolean (NO_ARGUMENT), the value is "1".
  38. *
  39. * Both interfaces can be used at the same time.
  40. *
  41. * Possible argument types are:
  42. * NO_ARGUMENT - The argument takes no value : --A
  43. * CONCAT_ARGUMENT - The argument takes value after no space : --Aval
  44. * SPACE_ARGUMENT - The argument takes value after space : --A val
  45. * EQUAL_ARGUMENT - The argument takes value after equal : --A=val
  46. *
  47. * Example use:
  48. *
  49. * kwsys::CommandLineArguments arg;
  50. * arg.Initialize(argc, argv);
  51. * typedef kwsys::CommandLineArguments argT;
  52. * arg.AddArgument("--something", argT::EQUAL_ARGUMENT, &some_variable,
  53. * "This is help string for --something");
  54. * if ( !arg.Parse() )
  55. * {
  56. * kwsys_ios::cerr << "Problem parsing arguments" << kwsys_ios::endl;
  57. * res = 1;
  58. * }
  59. *
  60. */
  61. class @KWSYS_NAMESPACE@_EXPORT CommandLineArguments
  62. {
  63. public:
  64. CommandLineArguments();
  65. ~CommandLineArguments();
  66. /**
  67. * Various argument types.
  68. */
  69. enum ArgumentTypeEnum {
  70. NO_ARGUMENT,
  71. CONCAT_ARGUMENT,
  72. SPACE_ARGUMENT,
  73. EQUAL_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 vairable 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. };
  87. /**
  88. * Prototypes for callbacks for callback interface.
  89. */
  90. typedef int(*CallbackType)(const char* argument, const char* value,
  91. void* call_data);
  92. typedef int(*ErrorCallbackType)(const char* argument, void* client_data);
  93. /**
  94. * Initialize internal data structures. This should be called before parsing.
  95. */
  96. void Initialize(int argc, const char* const argv[]);
  97. void Initialize(int argc, char* argv[]);
  98. /**
  99. * Initialize internal data structure and pass arguments one by one. This is
  100. * convinience method for use from scripting languages where argc and argv
  101. * are not available.
  102. */
  103. void Initialize();
  104. void ProcessArgument(const char* arg);
  105. /**
  106. * This method will parse arguments and call apropriate methods.
  107. */
  108. int Parse();
  109. /**
  110. * This method will add a callback for a specific argument. The arguments to
  111. * it are argument, argument type, callback method, and call data. The
  112. * argument help specifies the help string used with this option. The
  113. * callback and call_data can be skipped.
  114. */
  115. void AddCallback(const char* argument, ArgumentTypeEnum type,
  116. CallbackType callback, void* call_data, const char* help);
  117. /**
  118. * Add handler for argument which is going to set the variable to the
  119. * specified value. If the argument is specified, the option is casted to the
  120. * apropriate type.
  121. */
  122. void AddArgument(const char* argument, ArgumentTypeEnum type, bool* variable,
  123. const char* help);
  124. void AddArgument(const char* argument, ArgumentTypeEnum type, int* variable,
  125. const char* help);
  126. void AddArgument(const char* argument, ArgumentTypeEnum type,
  127. double* variable, const char* help);
  128. void AddArgument(const char* argument, ArgumentTypeEnum type,
  129. char** variable, const char* help);
  130. void AddArgument(const char* argument, ArgumentTypeEnum type,
  131. kwsys_stl::string* variable, const char* help);
  132. /**
  133. * Add handler for boolean argument. The argument does not take any option
  134. * and if it is specified, the value of the variable is true/1, otherwise it
  135. * is false/0.
  136. */
  137. void AddBooleanArgument(const char* argument, bool* variable, const char*
  138. help);
  139. void AddBooleanArgument(const char* argument, int* variable, const char*
  140. help);
  141. /**
  142. * Set the callbacks for error handling.
  143. */
  144. void SetClientData(void* client_data);
  145. void SetUnknownArgumentCallback(ErrorCallbackType callback);
  146. /**
  147. * Get remaining arguments. It allocates space for argv, so you have to call
  148. * delete[] on it.
  149. */
  150. void GetRemainingArguments(int* argc, char*** argv);
  151. /**
  152. * Return string containing help. If the argument is specified, only return
  153. * help for that argument.
  154. */
  155. const char* GetHelp() { return this->Help.c_str(); }
  156. const char* GetHelp(const char* arg);
  157. /**
  158. * Get / Set the help line length. This length is used when generating the
  159. * help page. Default length is 80.
  160. */
  161. void SetLineLength(unsigned int);
  162. unsigned int GetLineLength();
  163. /**
  164. * Get the executable name (argv0). This is only available when using
  165. * Initialize with argc/argv.
  166. */
  167. const char* GetArgv0();
  168. /**
  169. * Get index of the last argument parsed. This is the last argument that was
  170. * parsed ok in the original argc/argv list.
  171. */
  172. unsigned int GetLastArgument();
  173. protected:
  174. void GenerateHelp();
  175. //! This is internal method that registers variable with argument
  176. void AddArgument(const char* argument, ArgumentTypeEnum type,
  177. VariableTypeEnum vtype, void* variable, const char* help);
  178. typedef CommandLineArgumentsInternal Internal;
  179. Internal* Internals;
  180. kwsys_stl::string Help;
  181. unsigned int LineLength;
  182. };
  183. } // namespace @KWSYS_NAMESPACE@
  184. /* Undefine temporary macro. */
  185. #if !defined (KWSYS_NAMESPACE) && !@KWSYS_NAMESPACE@_NAME_IS_KWSYS
  186. # undef kwsys_stl
  187. #endif
  188. #endif