Resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Source / kwsys / CommandLineArguments.hxx.in
blob1f2bed68899b2ddf0b4721da4d8413e3563b020a
1 /*=========================================================================
3 Program: KWSys - Kitware System Library
4 Module: $RCSfile: CommandLineArguments.hxx.in,v $
6 Copyright (c) Kitware, Inc., Insight Consortium. All rights reserved.
7 See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 This software is distributed WITHOUT ANY WARRANTY; without even
10 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 PURPOSE. See the above copyright notices for more information.
13 =========================================================================*/
14 #ifndef @KWSYS_NAMESPACE@_CommandLineArguments_hxx
15 #define @KWSYS_NAMESPACE@_CommandLineArguments_hxx
17 #include <@KWSYS_NAMESPACE@/Configure.h>
18 #include <@KWSYS_NAMESPACE@/Configure.hxx>
20 #include <@KWSYS_NAMESPACE@/stl/string>
21 #include <@KWSYS_NAMESPACE@/stl/vector>
23 /* Define this macro temporarily to keep the code readable. */
24 #if !defined (KWSYS_NAMESPACE) && !@KWSYS_NAMESPACE@_NAME_IS_KWSYS
25 # define kwsys_stl @KWSYS_NAMESPACE@_stl
26 #endif
28 namespace @KWSYS_NAMESPACE@
31 class CommandLineArgumentsInternal;
32 struct CommandLineArgumentsCallbackStructure;
34 /** \class CommandLineArguments
35 * \brief Command line arguments processing code.
37 * Find specified arguments with optional options and execute specified methods
38 * or set given variables.
40 * The two interfaces it knows are callback based and variable based. For
41 * callback based, you have to register callback for particular argument using
42 * AddCallback method. When that argument is passed, the callback will be
43 * called with argument, value, and call data. For boolean (NO_ARGUMENT)
44 * arguments, the value is "1". If the callback returns 0 the argument parsing
45 * will stop with an error.
47 * For the variable interface you associate variable with each argument. When
48 * the argument is specified, the variable is set to the specified value casted
49 * to the apropriate type. For boolean (NO_ARGUMENT), the value is "1".
51 * Both interfaces can be used at the same time.
53 * Possible argument types are:
54 * NO_ARGUMENT - The argument takes no value : --A
55 * CONCAT_ARGUMENT - The argument takes value after no space : --Aval
56 * SPACE_ARGUMENT - The argument takes value after space : --A val
57 * EQUAL_ARGUMENT - The argument takes value after equal : --A=val
58 * MULTI_ARGUMENT - The argument takes values after space : --A val1 val2 val3 ...
60 * Example use:
62 * kwsys::CommandLineArguments arg;
63 * arg.Initialize(argc, argv);
64 * typedef kwsys::CommandLineArguments argT;
65 * arg.AddArgument("--something", argT::EQUAL_ARGUMENT, &some_variable,
66 * "This is help string for --something");
67 * if ( !arg.Parse() )
68 * {
69 * kwsys_ios::cerr << "Problem parsing arguments" << kwsys_ios::endl;
70 * res = 1;
71 * }
75 class @KWSYS_NAMESPACE@_EXPORT CommandLineArguments
77 public:
78 CommandLineArguments();
79 ~CommandLineArguments();
81 /**
82 * Various argument types.
84 enum ArgumentTypeEnum {
85 NO_ARGUMENT,
86 CONCAT_ARGUMENT,
87 SPACE_ARGUMENT,
88 EQUAL_ARGUMENT,
89 MULTI_ARGUMENT
92 /**
93 * Various variable types. When using the variable interface, this specifies
94 * what type the variable is.
96 enum VariableTypeEnum {
97 NO_VARIABLE_TYPE = 0, // The variable is not specified
98 INT_TYPE, // The variable is integer (int)
99 BOOL_TYPE, // The variable is boolean (bool)
100 DOUBLE_TYPE, // The variable is float (double)
101 STRING_TYPE, // The variable is string (char*)
102 STL_STRING_TYPE, // The variable is string (char*)
103 VECTOR_INT_TYPE, // The variable is integer (int)
104 VECTOR_BOOL_TYPE, // The vairable is boolean (bool)
105 VECTOR_DOUBLE_TYPE, // The variable is float (double)
106 VECTOR_STRING_TYPE, // The variable is string (char*)
107 VECTOR_STL_STRING_TYPE, // The variable is string (char*)
108 LAST_VARIABLE_TYPE
112 * Prototypes for callbacks for callback interface.
114 typedef int(*CallbackType)(const char* argument, const char* value,
115 void* call_data);
116 typedef int(*ErrorCallbackType)(const char* argument, void* client_data);
119 * Initialize internal data structures. This should be called before parsing.
121 void Initialize(int argc, const char* const argv[]);
122 void Initialize(int argc, char* argv[]);
125 * Initialize internal data structure and pass arguments one by one. This is
126 * convenience method for use from scripting languages where argc and argv
127 * are not available.
129 void Initialize();
130 void ProcessArgument(const char* arg);
133 * This method will parse arguments and call apropriate methods.
135 int Parse();
138 * This method will add a callback for a specific argument. The arguments to
139 * it are argument, argument type, callback method, and call data. The
140 * argument help specifies the help string used with this option. The
141 * callback and call_data can be skipped.
143 void AddCallback(const char* argument, ArgumentTypeEnum type,
144 CallbackType callback, void* call_data, const char* help);
147 * Add handler for argument which is going to set the variable to the
148 * specified value. If the argument is specified, the option is casted to the
149 * apropriate type.
151 void AddArgument(const char* argument, ArgumentTypeEnum type,
152 bool* variable, const char* help);
153 void AddArgument(const char* argument, ArgumentTypeEnum type,
154 int* variable, const char* help);
155 void AddArgument(const char* argument, ArgumentTypeEnum type,
156 double* variable, const char* help);
157 void AddArgument(const char* argument, ArgumentTypeEnum type,
158 char** variable, const char* help);
159 void AddArgument(const char* argument, ArgumentTypeEnum type,
160 kwsys_stl::string* variable, const char* help);
163 * Add handler for argument which is going to set the variable to the
164 * specified value. If the argument is specified, the option is casted to the
165 * apropriate type. This will handle the multi argument values.
167 void AddArgument(const char* argument, ArgumentTypeEnum type,
168 kwsys_stl::vector<bool>* variable, const char* help);
169 void AddArgument(const char* argument, ArgumentTypeEnum type,
170 kwsys_stl::vector<int>* variable, const char* help);
171 void AddArgument(const char* argument, ArgumentTypeEnum type,
172 kwsys_stl::vector<double>* variable, const char* help);
173 void AddArgument(const char* argument, ArgumentTypeEnum type,
174 kwsys_stl::vector<char*>* variable, const char* help);
175 void AddArgument(const char* argument, ArgumentTypeEnum type,
176 kwsys_stl::vector<kwsys_stl::string>* variable, const char* help);
179 * Add handler for boolean argument. The argument does not take any option
180 * and if it is specified, the value of the variable is true/1, otherwise it
181 * is false/0.
183 void AddBooleanArgument(const char* argument,
184 bool* variable, const char* help);
185 void AddBooleanArgument(const char* argument,
186 int* variable, const char* help);
187 void AddBooleanArgument(const char* argument,
188 double* variable, const char* help);
189 void AddBooleanArgument(const char* argument,
190 char** variable, const char* help);
191 void AddBooleanArgument(const char* argument,
192 kwsys_stl::string* variable, const char* help);
195 * Set the callbacks for error handling.
197 void SetClientData(void* client_data);
198 void SetUnknownArgumentCallback(ErrorCallbackType callback);
201 * Get remaining arguments. It allocates space for argv, so you have to call
202 * delete[] on it.
204 void GetRemainingArguments(int* argc, char*** argv);
205 void DeleteRemainingArguments(int argc, char*** argv);
208 * If StoreUnusedArguments is set to true, then all unknown arguments will be
209 * stored and the user can access the modified argc, argv without known
210 * arguments.
212 void StoreUnusedArguments(bool val) { this->StoreUnusedArgumentsFlag = val; }
213 void GetUnusedArguments(int* argc, char*** argv);
216 * Return string containing help. If the argument is specified, only return
217 * help for that argument.
219 const char* GetHelp() { return this->Help.c_str(); }
220 const char* GetHelp(const char* arg);
223 * Get / Set the help line length. This length is used when generating the
224 * help page. Default length is 80.
226 void SetLineLength(unsigned int);
227 unsigned int GetLineLength();
230 * Get the executable name (argv0). This is only available when using
231 * Initialize with argc/argv.
233 const char* GetArgv0();
236 * Get index of the last argument parsed. This is the last argument that was
237 * parsed ok in the original argc/argv list.
239 unsigned int GetLastArgument();
241 protected:
242 void GenerateHelp();
244 //! This is internal method that registers variable with argument
245 void AddArgument(const char* argument, ArgumentTypeEnum type,
246 VariableTypeEnum vtype, void* variable, const char* help);
248 bool GetMatchedArguments(kwsys_stl::vector<kwsys_stl::string>* matches,
249 const kwsys_stl::string& arg);
251 //! Populate individual variables
252 bool PopulateVariable(CommandLineArgumentsCallbackStructure* cs,
253 const char* value);
255 //! Populate individual variables of type ...
256 void PopulateVariable(bool* variable, const kwsys_stl::string& value);
257 void PopulateVariable(int* variable, const kwsys_stl::string& value);
258 void PopulateVariable(double* variable, const kwsys_stl::string& value);
259 void PopulateVariable(char** variable, const kwsys_stl::string& value);
260 void PopulateVariable(kwsys_stl::string* variable, const kwsys_stl::string& value);
261 void PopulateVariable(kwsys_stl::vector<bool>* variable, const kwsys_stl::string& value);
262 void PopulateVariable(kwsys_stl::vector<int>* variable, const kwsys_stl::string& value);
263 void PopulateVariable(kwsys_stl::vector<double>* variable, const kwsys_stl::string& value);
264 void PopulateVariable(kwsys_stl::vector<char*>* variable, const kwsys_stl::string& value);
265 void PopulateVariable(kwsys_stl::vector<kwsys_stl::string>* variable, const kwsys_stl::string& value);
267 typedef CommandLineArgumentsInternal Internal;
268 Internal* Internals;
269 kwsys_stl::string Help;
271 unsigned int LineLength;
273 bool StoreUnusedArgumentsFlag;
276 } // namespace @KWSYS_NAMESPACE@
278 /* Undefine temporary macro. */
279 #if !defined (KWSYS_NAMESPACE) && !@KWSYS_NAMESPACE@_NAME_IS_KWSYS
280 # undef kwsys_stl
281 #endif
283 #endif