CommandLineArguments.hxx.in 9.75 KB
Newer Older
1
2
3
/*============================================================================
  KWSys - Kitware System Library
  Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
4

5
6
  Distributed under the OSI-approved BSD License (the "License");
  see accompanying file Copyright.txt for details.
7

8
9
10
11
  This software is distributed WITHOUT ANY WARRANTY; without even the
  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  See the License for more information.
============================================================================*/
12
13
14
15
16
17
#ifndef @KWSYS_NAMESPACE@_CommandLineArguments_hxx
#define @KWSYS_NAMESPACE@_CommandLineArguments_hxx

#include <@KWSYS_NAMESPACE@/Configure.h>
#include <@KWSYS_NAMESPACE@/Configure.hxx>

Brad King's avatar
Brad King committed
18
19
#include <string>
#include <vector>
20
21
22
23
24

namespace @KWSYS_NAMESPACE@
{

class CommandLineArgumentsInternal;
Andy Cedilnik's avatar
Andy Cedilnik committed
25
struct CommandLineArgumentsCallbackStructure;
26
27
28
29
30
31

/** \class CommandLineArguments
 * \brief Command line arguments processing code.
 *
 * Find specified arguments with optional options and execute specified methods
 * or set given variables.
Andy Cedilnik's avatar
Andy Cedilnik committed
32
33
34
35
36
37
38
39
40
41
 *
 * The two interfaces it knows are callback based and variable based. For
 * callback based, you have to register callback for particular argument using
 * AddCallback method. When that argument is passed, the callback will be
 * called with argument, value, and call data. For boolean (NO_ARGUMENT)
 * arguments, the value is "1". If the callback returns 0 the argument parsing
 * will stop with an error.
 *
 * For the variable interface you associate variable with each argument. When
 * the argument is specified, the variable is set to the specified value casted
42
 * to the appropriate type. For boolean (NO_ARGUMENT), the value is "1".
Andy Cedilnik's avatar
Andy Cedilnik committed
43
44
45
46
47
48
49
50
 *
 * Both interfaces can be used at the same time. 
 *
 * Possible argument types are:
 *   NO_ARGUMENT     - The argument takes no value             : --A
 *   CONCAT_ARGUMENT - The argument takes value after no space : --Aval
 *   SPACE_ARGUMENT  - The argument takes value after space    : --A val 
 *   EQUAL_ARGUMENT  - The argument takes value after equal    : --A=val
51
 *   MULTI_ARGUMENT  - The argument takes values after space   : --A val1 val2 val3 ...
Andy Cedilnik's avatar
Andy Cedilnik committed
52
53
54
55
56
57
58
59
60
61
 *
 * Example use:
 *
 * kwsys::CommandLineArguments arg;
 * arg.Initialize(argc, argv);
 * typedef kwsys::CommandLineArguments argT;
 * arg.AddArgument("--something", argT::EQUAL_ARGUMENT, &some_variable, 
 *                 "This is help string for --something");
 * if ( !arg.Parse() )
 *   {
62
 *   std::cerr << "Problem parsing arguments" << std::endl;
Andy Cedilnik's avatar
Andy Cedilnik committed
63
64
65
 *   res = 1;
 *   }
 * 
66
67
68
69
70
71
72
73
74
75
76
77
 */

class @KWSYS_NAMESPACE@_EXPORT CommandLineArguments
{
public:
  CommandLineArguments();
  ~CommandLineArguments();

  /**
   * Various argument types.
   */
  enum ArgumentTypeEnum { 
Andy Cedilnik's avatar
Andy Cedilnik committed
78
79
80
    NO_ARGUMENT,
    CONCAT_ARGUMENT,
    SPACE_ARGUMENT,
81
82
    EQUAL_ARGUMENT,
    MULTI_ARGUMENT
83
84
85
  };

  /**
Andy Cedilnik's avatar
Andy Cedilnik committed
86
87
   * Various variable types. When using the variable interface, this specifies
   * what type the variable is.
88
89
90
91
   */
  enum VariableTypeEnum {
    NO_VARIABLE_TYPE = 0, // The variable is not specified
    INT_TYPE,             // The variable is integer (int)
Alexander Neundorf's avatar
 
Alexander Neundorf committed
92
    BOOL_TYPE,            // The variable is boolean (bool)
93
94
    DOUBLE_TYPE,          // The variable is float (double)
    STRING_TYPE,          // The variable is string (char*)
95
96
    STL_STRING_TYPE,      // The variable is string (char*)
    VECTOR_INT_TYPE,             // The variable is integer (int)
97
    VECTOR_BOOL_TYPE,            // The variable is boolean (bool)
98
99
100
101
    VECTOR_DOUBLE_TYPE,          // The variable is float (double)
    VECTOR_STRING_TYPE,          // The variable is string (char*)
    VECTOR_STL_STRING_TYPE,      // The variable is string (char*)
    LAST_VARIABLE_TYPE
102
103
104
105
106
107
108
109
110
111
112
113
  };

  /**
   * Prototypes for callbacks for callback interface.
   */
  typedef int(*CallbackType)(const char* argument, const char* value, 
    void* call_data);
  typedef int(*ErrorCallbackType)(const char* argument, void* client_data);

  /**
   * Initialize internal data structures. This should be called before parsing.
   */
114
  void Initialize(int argc, const char* const argv[]);
Ken Martin's avatar
Ken Martin committed
115
  void Initialize(int argc, char* argv[]);
116
117
118

  /**
   * Initialize internal data structure and pass arguments one by one. This is
119
   * convenience method for use from scripting languages where argc and argv
120
121
122
123
124
125
   * are not available.
   */
  void Initialize();
  void ProcessArgument(const char* arg);

  /**
126
   * This method will parse arguments and call appropriate methods.
127
128
129
130
131
132
133
134
135
   */
  int Parse();

  /**
   * This method will add a callback for a specific argument. The arguments to
   * it are argument, argument type, callback method, and call data. The
   * argument help specifies the help string used with this option. The
   * callback and call_data can be skipped.
   */
Andy Cedilnik's avatar
Andy Cedilnik committed
136
137
  void AddCallback(const char* argument, ArgumentTypeEnum type, 
    CallbackType callback, void* call_data, const char* help);
138
139
140

  /**
   * Add handler for argument which is going to set the variable to the
Andy Cedilnik's avatar
Andy Cedilnik committed
141
   * specified value. If the argument is specified, the option is casted to the
142
   * appropriate type.
143
   */
144
145
146
147
  void AddArgument(const char* argument, ArgumentTypeEnum type,
    bool* variable, const char* help);
  void AddArgument(const char* argument, ArgumentTypeEnum type,
    int* variable, const char* help);
Andy Cedilnik's avatar
Andy Cedilnik committed
148
149
150
151
152
  void AddArgument(const char* argument, ArgumentTypeEnum type, 
    double* variable, const char* help);
  void AddArgument(const char* argument, ArgumentTypeEnum type, 
    char** variable, const char* help);
  void AddArgument(const char* argument, ArgumentTypeEnum type,
Brad King's avatar
Brad King committed
153
    std::string* variable, const char* help);
Andy Cedilnik's avatar
Andy Cedilnik committed
154

155
156
157
  /**
   * Add handler for argument which is going to set the variable to the
   * specified value. If the argument is specified, the option is casted to the
158
   * appropriate type. This will handle the multi argument values.
159
160
   */
  void AddArgument(const char* argument, ArgumentTypeEnum type,
Brad King's avatar
Brad King committed
161
    std::vector<bool>* variable, const char* help);
162
  void AddArgument(const char* argument, ArgumentTypeEnum type,
Brad King's avatar
Brad King committed
163
    std::vector<int>* variable, const char* help);
164
  void AddArgument(const char* argument, ArgumentTypeEnum type, 
Brad King's avatar
Brad King committed
165
    std::vector<double>* variable, const char* help);
166
  void AddArgument(const char* argument, ArgumentTypeEnum type, 
Brad King's avatar
Brad King committed
167
    std::vector<char*>* variable, const char* help);
168
  void AddArgument(const char* argument, ArgumentTypeEnum type,
Brad King's avatar
Brad King committed
169
    std::vector<std::string>* variable, const char* help);
170

Andy Cedilnik's avatar
Andy Cedilnik committed
171
172
173
174
175
  /**
   * Add handler for boolean argument. The argument does not take any option
   * and if it is specified, the value of the variable is true/1, otherwise it
   * is false/0.
   */
176
177
178
179
180
181
182
183
184
  void AddBooleanArgument(const char* argument,
    bool* variable, const char* help);
  void AddBooleanArgument(const char* argument,
    int* variable, const char* help);
  void AddBooleanArgument(const char* argument,
    double* variable, const char* help);
  void AddBooleanArgument(const char* argument,
    char** variable, const char* help);
  void AddBooleanArgument(const char* argument,
Brad King's avatar
Brad King committed
185
    std::string* variable, const char* help);
186

187
188
189
190
191
192
193
194
195
196
197
  /**
   * Set the callbacks for error handling.
   */
  void SetClientData(void* client_data);
  void SetUnknownArgumentCallback(ErrorCallbackType callback);

  /**
   * Get remaining arguments. It allocates space for argv, so you have to call
   * delete[] on it.
   */
  void GetRemainingArguments(int* argc, char*** argv);
198
  void DeleteRemainingArguments(int argc, char*** argv);
199

200
201
202
203
204
205
206
207
  /**
   * If StoreUnusedArguments is set to true, then all unknown arguments will be
   * stored and the user can access the modified argc, argv without known
   * arguments.
   */
  void StoreUnusedArguments(bool val) { this->StoreUnusedArgumentsFlag = val; }
  void GetUnusedArguments(int* argc, char*** argv);

208
209
210
211
212
213
214
215
  /**
   * Return string containing help. If the argument is specified, only return
   * help for that argument.
   */
  const char* GetHelp() { return this->Help.c_str(); }
  const char* GetHelp(const char* arg);

  /**
Andy Cedilnik's avatar
Andy Cedilnik committed
216
217
   * Get / Set the help line length. This length is used when generating the
   * help page. Default length is 80.
218
   */
219
  void SetLineLength(unsigned int);
220
221
  unsigned int GetLineLength();

Andy Cedilnik's avatar
Andy Cedilnik committed
222
  /**
Andy Cedilnik's avatar
Andy Cedilnik committed
223
224
   * Get the executable name (argv0). This is only available when using
   * Initialize with argc/argv.
Andy Cedilnik's avatar
Andy Cedilnik committed
225
226
227
   */
  const char* GetArgv0();

228
229
230
231
232
233
  /**
   * Get index of the last argument parsed. This is the last argument that was
   * parsed ok in the original argc/argv list.
   */
  unsigned int GetLastArgument();

234
235
236
protected:
  void GenerateHelp();

Andy Cedilnik's avatar
Andy Cedilnik committed
237
238
239
240
  //! This is internal method that registers variable with argument
  void AddArgument(const char* argument, ArgumentTypeEnum type,
    VariableTypeEnum vtype, void* variable, const char* help);

Brad King's avatar
Brad King committed
241
242
  bool GetMatchedArguments(std::vector<std::string>* matches,
    const std::string& arg);
243
244
245
246
247
248

  //! Populate individual variables
  bool PopulateVariable(CommandLineArgumentsCallbackStructure* cs,
    const char* value);

  //! Populate individual variables of type ...
Brad King's avatar
Brad King committed
249
250
251
252
253
254
255
256
257
258
  void PopulateVariable(bool* variable, const std::string& value);
  void PopulateVariable(int* variable, const std::string& value);
  void PopulateVariable(double* variable, const std::string& value);
  void PopulateVariable(char** variable, const std::string& value);
  void PopulateVariable(std::string* variable, const std::string& value);
  void PopulateVariable(std::vector<bool>* variable, const std::string& value);
  void PopulateVariable(std::vector<int>* variable, const std::string& value);
  void PopulateVariable(std::vector<double>* variable, const std::string& value);
  void PopulateVariable(std::vector<char*>* variable, const std::string& value);
  void PopulateVariable(std::vector<std::string>* variable, const std::string& value);
259

260
261
  typedef CommandLineArgumentsInternal Internal;
  Internal* Internals;
Brad King's avatar
Brad King committed
262
  std::string Help;
263
264

  unsigned int LineLength;
265
266

  bool StoreUnusedArgumentsFlag;
267
268
269
270
271
272
273
274
275
276
};

} // namespace @KWSYS_NAMESPACE@

#endif