CommandLineArguments.hxx.in 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. class @KWSYS_NAMESPACE@_EXPORT CommandLineArguments
  29. {
  30. public:
  31. CommandLineArguments();
  32. ~CommandLineArguments();
  33. /**
  34. * Various argument types.
  35. */
  36. enum ArgumentTypeEnum {
  37. NO_ARGUMENT, // The option takes no argument --foo
  38. CONCAT_ARGUMENT,// The option takes argument after no space --foobar
  39. SPACE_ARGUMENT, // The option takes argument after space --foo bar
  40. EQUAL_ARGUMENT // The option takes argument after equal --foo=bar
  41. };
  42. /**
  43. * Various string types.
  44. */
  45. enum VariableTypeEnum {
  46. NO_VARIABLE_TYPE = 0, // The variable is not specified
  47. INT_TYPE, // The variable is integer (int)
  48. BOOL_TYPE, // The vairable is boolean (bool)
  49. DOUBLE_TYPE, // The variable is float (double)
  50. STRING_TYPE, // The variable is string (char*)
  51. STL_STRING_TYPE // The variable is string (char*)
  52. };
  53. /**
  54. * Prototypes for callbacks for callback interface.
  55. */
  56. typedef int(*CallbackType)(const char* argument, const char* value,
  57. void* call_data);
  58. typedef int(*ErrorCallbackType)(const char* argument, void* client_data);
  59. struct CallbackStructure
  60. {
  61. const char* Argument;
  62. int ArgumentType;
  63. CallbackType Callback;
  64. void* CallData;
  65. void* Variable;
  66. int VariableType;
  67. const char* Help;
  68. };
  69. /**
  70. * Initialize internal data structures. This should be called before parsing.
  71. */
  72. void Initialize(int argc, const char* const argv[]);
  73. void Initialize(int argc, char* argv[]);
  74. /**
  75. * Initialize internal data structure and pass arguments one by one. This is
  76. * convinience method for use from scripting languages where argc and argv
  77. * are not available.
  78. */
  79. void Initialize();
  80. void ProcessArgument(const char* arg);
  81. /**
  82. * This method will parse arguments and call apropriate methods.
  83. */
  84. int Parse();
  85. /**
  86. * This method will add a callback for a specific argument. The arguments to
  87. * it are argument, argument type, callback method, and call data. The
  88. * argument help specifies the help string used with this option. The
  89. * callback and call_data can be skipped.
  90. */
  91. void AddCallback(const char* argument, ArgumentTypeEnum type, CallbackType callback,
  92. void* call_data, const char* help);
  93. /**
  94. * Add handler for argument which is going to set the variable to the
  95. * specified value.
  96. */
  97. void AddArgument(const char* argument, ArgumentTypeEnum type, VariableTypeEnum vtype, void* variable, const char* help);
  98. void AddArgument(const char* argument, ArgumentTypeEnum type, bool* variable, const char* help);
  99. void AddArgument(const char* argument, ArgumentTypeEnum type, int* variable, const char* help);
  100. void AddArgument(const char* argument, ArgumentTypeEnum type, double* variable, const char* help);
  101. void AddArgument(const char* argument, ArgumentTypeEnum type, char** variable, const char* help);
  102. void AddArgument(const char* argument, ArgumentTypeEnum type, kwsys_stl::string* variable, const char* help);
  103. void AddBooleanArgument(const char* argument, bool* variable, const char* help);
  104. void AddBooleanArgument(const char* argument, int* variable, const char* help);
  105. /**
  106. * Set the callbacks for error handling.
  107. */
  108. void SetClientData(void* client_data);
  109. void SetUnknownArgumentCallback(ErrorCallbackType callback);
  110. /**
  111. * Get remaining arguments. It allocates space for argv, so you have to call
  112. * delete[] on it.
  113. */
  114. void GetRemainingArguments(int* argc, char*** argv);
  115. /**
  116. * Return string containing help. If the argument is specified, only return
  117. * help for that argument.
  118. */
  119. const char* GetHelp() { return this->Help.c_str(); }
  120. const char* GetHelp(const char* arg);
  121. /**
  122. * Get / Set the help line length. Default length is 80.
  123. */
  124. void SetLineLength();
  125. unsigned int GetLineLength();
  126. protected:
  127. void GenerateHelp();
  128. typedef CommandLineArgumentsInternal Internal;
  129. Internal* Internals;
  130. kwsys_stl::string Help;
  131. unsigned int LineLength;
  132. };
  133. } // namespace @KWSYS_NAMESPACE@
  134. /* Undefine temporary macro. */
  135. #if !defined (KWSYS_NAMESPACE) && !@KWSYS_NAMESPACE@_NAME_IS_KWSYS
  136. # undef kwsys_stl
  137. #endif
  138. #endif