OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #ifndef SK_COMMAND_LINE_FLAGS_H | 8 #ifndef SK_COMMAND_LINE_FLAGS_H |
9 #define SK_COMMAND_LINE_FLAGS_H | 9 #define SK_COMMAND_LINE_FLAGS_H |
10 | 10 |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
69 * | 69 * |
70 * creates an array: | 70 * creates an array: |
71 * | 71 * |
72 * SkCommandLineFlags::StringArray FLAGS_args; | 72 * SkCommandLineFlags::StringArray FLAGS_args; |
73 * | 73 * |
74 * If the default value is the empty string, FLAGS_args will default to a size | 74 * If the default value is the empty string, FLAGS_args will default to a size |
75 * of zero. Otherwise it will default to a size of 1 with the default string | 75 * of zero. Otherwise it will default to a size of 1 with the default string |
76 * as its value. All strings that follow the flag on the command line (until | 76 * as its value. All strings that follow the flag on the command line (until |
77 * a string that begins with '-') will be entries in the array. | 77 * a string that begins with '-') will be entries in the array. |
78 * | 78 * |
| 79 * DEFINE_extended_string(args, .., .., extendedHelpString); |
| 80 * |
| 81 * creates a similar string array flag as DEFINE_string. The flag will have ext
ended help text |
| 82 * (extendedHelpString) that can the user can see with '--help <args>' flag. |
| 83 * |
79 * Any flag can be referenced from another file after using the following: | 84 * Any flag can be referenced from another file after using the following: |
80 * | 85 * |
81 * DECLARE_x(name); | 86 * DECLARE_x(name); |
82 * | 87 * |
83 * (where 'x' is the type specified in the DEFINE). | 88 * (where 'x' is the type specified in the DEFINE). |
84 * | 89 * |
85 * Inspired by gflags (https://code.google.com/p/gflags/). Is not quite as | 90 * Inspired by gflags (https://code.google.com/p/gflags/). Is not quite as |
86 * robust as gflags, but suits our purposes. For example, allows creating | 91 * robust as gflags, but suits our purposes. For example, allows creating |
87 * a flag -h or -help which will never be used, since SkCommandLineFlags handle
s it. | 92 * a flag -h or -help which will never be used, since SkCommandLineFlags handle
s it. |
88 * SkCommandLineFlags will also allow creating --flag and --noflag. Uses the sa
me input | 93 * SkCommandLineFlags will also allow creating --flag and --noflag. Uses the sa
me input |
(...skipping 18 matching lines...) Expand all Loading... |
107 * Must only be called once. | 112 * Must only be called once. |
108 */ | 113 */ |
109 static void Parse(int argc, char** argv); | 114 static void Parse(int argc, char** argv); |
110 | 115 |
111 /** | 116 /** |
112 * Custom class for holding the arguments for a string flag. | 117 * Custom class for holding the arguments for a string flag. |
113 * Publicly only has accessors so the strings cannot be modified. | 118 * Publicly only has accessors so the strings cannot be modified. |
114 */ | 119 */ |
115 class StringArray { | 120 class StringArray { |
116 public: | 121 public: |
| 122 StringArray() { } |
| 123 explicit StringArray(const SkTArray<SkString>& strings) |
| 124 : fStrings(strings) { |
| 125 } |
117 const char* operator[](int i) const { | 126 const char* operator[](int i) const { |
118 SkASSERT(i >= 0 && i < fStrings.count()); | 127 SkASSERT(i >= 0 && i < fStrings.count()); |
119 return fStrings[i].c_str(); | 128 return fStrings[i].c_str(); |
120 } | 129 } |
121 | 130 |
122 int count() const { | 131 int count() const { |
123 return fStrings.count(); | 132 return fStrings.count(); |
124 } | 133 } |
125 | 134 |
126 bool isEmpty() const { return this->count() == 0; } | 135 bool isEmpty() const { return this->count() == 0; } |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
197 helpString) | 206 helpString) |
198 | 207 |
199 #define DECLARE_bool(name) extern bool FLAGS_##name; | 208 #define DECLARE_bool(name) extern bool FLAGS_##name; |
200 | 209 |
201 #define DEFINE_string(name, defaultValue, helpString) \ | 210 #define DEFINE_string(name, defaultValue, helpString) \ |
202 SkCommandLineFlags::StringArray FLAGS_##name; \ | 211 SkCommandLineFlags::StringArray FLAGS_##name; \ |
203 SK_UNUSED static bool unused_##name = SkFlagInfo::CreateStringFlag(TO_STRING(nam
e), \ | 212 SK_UNUSED static bool unused_##name = SkFlagInfo::CreateStringFlag(TO_STRING(nam
e), \ |
204 nullptr,
\ | 213 nullptr,
\ |
205 &FLAGS_##name
, \ | 214 &FLAGS_##name
, \ |
206 defaultValue,
\ | 215 defaultValue,
\ |
207 helpString) | 216 helpString, n
ullptr) |
| 217 #define DEFINE_extended_string(name, defaultValue, helpString, extendedHelpStrin
g) \ |
| 218 SkCommandLineFlags::StringArray FLAGS_##name;
\ |
| 219 SK_UNUSED static bool unused_##name = SkFlagInfo::CreateStringFlag(TO_STRING(nam
e), \ |
| 220 nullptr, \ |
| 221 &FLAGS_##name
, \ |
| 222 defaultValue,
\ |
| 223 helpString, \ |
| 224 extendedHelpS
tring) |
208 | 225 |
209 // string2 allows specifying a short name. There is an assert that shortName | 226 // string2 allows specifying a short name. There is an assert that shortName |
210 // is only 1 character. | 227 // is only 1 character. |
211 #define DEFINE_string2(name, shortName, defaultValue, helpString)
\ | 228 #define DEFINE_string2(name, shortName, defaultValue, helpString)
\ |
212 SkCommandLineFlags::StringArray FLAGS_##name;
\ | 229 SkCommandLineFlags::StringArray FLAGS_##name;
\ |
213 SK_UNUSED static bool unused_##name = SkFlagInfo::CreateStringFlag(TO_STRING(nam
e), \ | 230 SK_UNUSED static bool unused_##name = SkFlagInfo::CreateStringFlag(TO_STRING(nam
e), \ |
214 TO_STRING(sho
rtName), \ | 231 TO_STRING(sho
rtName), \ |
215 &FLAGS_##name
, \ | 232 &FLAGS_##name
, \ |
216 defaultValue,
\ | 233 defaultValue,
\ |
217 helpString) | 234 helpString, n
ullptr) |
218 | 235 |
219 #define DECLARE_string(name) extern SkCommandLineFlags::StringArray FLAGS_##name
; | 236 #define DECLARE_string(name) extern SkCommandLineFlags::StringArray FLAGS_##name
; |
220 | 237 |
221 | 238 |
222 | 239 |
223 | 240 |
224 #define DEFINE_int32(name, defaultValue, helpString) \ | 241 #define DEFINE_int32(name, defaultValue, helpString) \ |
225 int32_t FLAGS_##name; \ | 242 int32_t FLAGS_##name; \ |
226 SK_UNUSED static bool unused_##name = SkFlagInfo::CreateIntFlag(TO_STRING(name),
\ | 243 SK_UNUSED static bool unused_##name = SkFlagInfo::CreateIntFlag(TO_STRING(name),
\ |
227 &FLAGS_##name,
\ | 244 &FLAGS_##name,
\ |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
266 * be referenced on the command line as "--name" to set the value of th
is flag. | 283 * be referenced on the command line as "--name" to set the value of th
is flag. |
267 * @param shortName Short version (one character) of the name of the flag.
This name can | 284 * @param shortName Short version (one character) of the name of the flag.
This name can |
268 * be referenced on the command line as "-shortName" to set the value o
f this flag. | 285 * be referenced on the command line as "-shortName" to set the value o
f this flag. |
269 * @param p<Type> Pointer to a global variable which holds the value set by
SkCommandLineFlags. | 286 * @param p<Type> Pointer to a global variable which holds the value set by
SkCommandLineFlags. |
270 * @param defaultValue The default value of this flag. The variable pointed
to by p<Type> will | 287 * @param defaultValue The default value of this flag. The variable pointed
to by p<Type> will |
271 * be set to this value initially. This is also displayed as part of th
e help output. | 288 * be set to this value initially. This is also displayed as part of th
e help output. |
272 * @param helpString Explanation of what this flag changes in the program. | 289 * @param helpString Explanation of what this flag changes in the program. |
273 */ | 290 */ |
274 static bool CreateBoolFlag(const char* name, const char* shortName, bool* pB
ool, | 291 static bool CreateBoolFlag(const char* name, const char* shortName, bool* pB
ool, |
275 bool defaultValue, const char* helpString) { | 292 bool defaultValue, const char* helpString) { |
276 SkFlagInfo* info = new SkFlagInfo(name, shortName, kBool_FlagType, helpS
tring); | 293 SkFlagInfo* info = new SkFlagInfo(name, shortName, kBool_FlagType, helpS
tring, nullptr); |
277 info->fBoolValue = pBool; | 294 info->fBoolValue = pBool; |
278 *info->fBoolValue = info->fDefaultBool = defaultValue; | 295 *info->fBoolValue = info->fDefaultBool = defaultValue; |
279 return true; | 296 return true; |
280 } | 297 } |
281 | 298 |
282 /** | 299 /** |
283 * See comments for CreateBoolFlag. | 300 * See comments for CreateBoolFlag. |
284 * @param pStrings Unlike the others, this is a pointer to an array of valu
es. | 301 * @param pStrings Unlike the others, this is a pointer to an array of valu
es. |
285 * @param defaultValue Thise default will be parsed so that strings separat
ed by spaces | 302 * @param defaultValue Thise default will be parsed so that strings separat
ed by spaces |
286 * will be added to pStrings. | 303 * will be added to pStrings. |
287 */ | 304 */ |
288 static bool CreateStringFlag(const char* name, const char* shortName, | 305 static bool CreateStringFlag(const char* name, const char* shortName, |
289 SkCommandLineFlags::StringArray* pStrings, | 306 SkCommandLineFlags::StringArray* pStrings, |
290 const char* defaultValue, const char* helpStrin
g); | 307 const char* defaultValue, const char* helpStrin
g, |
| 308 const char* extendedHelpString); |
291 | 309 |
292 /** | 310 /** |
293 * See comments for CreateBoolFlag. | 311 * See comments for CreateBoolFlag. |
294 */ | 312 */ |
295 static bool CreateIntFlag(const char* name, int32_t* pInt, | 313 static bool CreateIntFlag(const char* name, int32_t* pInt, |
296 int32_t defaultValue, const char* helpString) { | 314 int32_t defaultValue, const char* helpString) { |
297 SkFlagInfo* info = new SkFlagInfo(name, nullptr, kInt_FlagType, helpStri
ng); | 315 SkFlagInfo* info = new SkFlagInfo(name, nullptr, kInt_FlagType, helpStri
ng, nullptr); |
298 info->fIntValue = pInt; | 316 info->fIntValue = pInt; |
299 *info->fIntValue = info->fDefaultInt = defaultValue; | 317 *info->fIntValue = info->fDefaultInt = defaultValue; |
300 return true; | 318 return true; |
301 } | 319 } |
302 | 320 |
303 static bool CreateIntFlag(const char* name, const char* shortName, int32_t*
pInt, | 321 static bool CreateIntFlag(const char* name, const char* shortName, int32_t*
pInt, |
304 int32_t defaultValue, const char* helpString) { | 322 int32_t defaultValue, const char* helpString) { |
305 SkFlagInfo* info = new SkFlagInfo(name, shortName, kInt_FlagType, helpSt
ring); | 323 SkFlagInfo* info = new SkFlagInfo(name, shortName, kInt_FlagType, helpSt
ring, nullptr); |
306 info->fIntValue = pInt; | 324 info->fIntValue = pInt; |
307 *info->fIntValue = info->fDefaultInt = defaultValue; | 325 *info->fIntValue = info->fDefaultInt = defaultValue; |
308 return true; | 326 return true; |
309 } | 327 } |
310 | 328 |
311 /** | 329 /** |
312 * See comments for CreateBoolFlag. | 330 * See comments for CreateBoolFlag. |
313 */ | 331 */ |
314 static bool CreateDoubleFlag(const char* name, double* pDouble, | 332 static bool CreateDoubleFlag(const char* name, double* pDouble, |
315 double defaultValue, const char* helpString) { | 333 double defaultValue, const char* helpString) { |
316 SkFlagInfo* info = new SkFlagInfo(name, nullptr, kDouble_FlagType, helpS
tring); | 334 SkFlagInfo* info = new SkFlagInfo(name, nullptr, kDouble_FlagType, helpS
tring, nullptr); |
317 info->fDoubleValue = pDouble; | 335 info->fDoubleValue = pDouble; |
318 *info->fDoubleValue = info->fDefaultDouble = defaultValue; | 336 *info->fDoubleValue = info->fDefaultDouble = defaultValue; |
319 return true; | 337 return true; |
320 } | 338 } |
321 | 339 |
322 /** | 340 /** |
323 * Returns true if the string matches this flag. | 341 * Returns true if the string matches this flag. |
324 * For a boolean flag, also sets the value, since a boolean flag can be set
in a number of ways | 342 * For a boolean flag, also sets the value, since a boolean flag can be set
in a number of ways |
325 * without looking at the following string: | 343 * without looking at the following string: |
326 * --name | 344 * --name |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
376 } | 394 } |
377 } | 395 } |
378 | 396 |
379 SkFlagInfo* next() { return fNext; } | 397 SkFlagInfo* next() { return fNext; } |
380 | 398 |
381 const SkString& name() const { return fName; } | 399 const SkString& name() const { return fName; } |
382 | 400 |
383 const SkString& shortName() const { return fShortName; } | 401 const SkString& shortName() const { return fShortName; } |
384 | 402 |
385 const SkString& help() const { return fHelpString; } | 403 const SkString& help() const { return fHelpString; } |
| 404 const SkString& extendedHelp() const { return fExtendedHelpString; } |
386 | 405 |
387 SkString defaultValue() const { | 406 SkString defaultValue() const { |
388 SkString result; | 407 SkString result; |
389 switch (fFlagType) { | 408 switch (fFlagType) { |
390 case SkFlagInfo::kBool_FlagType: | 409 case SkFlagInfo::kBool_FlagType: |
391 result.printf("%s", fDefaultBool ? "true" : "false"); | 410 result.printf("%s", fDefaultBool ? "true" : "false"); |
392 break; | 411 break; |
393 case SkFlagInfo::kString_FlagType: | 412 case SkFlagInfo::kString_FlagType: |
394 return fDefaultString; | 413 return fDefaultString; |
395 case SkFlagInfo::kInt_FlagType: | 414 case SkFlagInfo::kInt_FlagType: |
(...skipping 18 matching lines...) Expand all Loading... |
414 return SkString("int"); | 433 return SkString("int"); |
415 case SkFlagInfo::kDouble_FlagType: | 434 case SkFlagInfo::kDouble_FlagType: |
416 return SkString("double"); | 435 return SkString("double"); |
417 default: | 436 default: |
418 SkDEBUGFAIL("Invalid flag type"); | 437 SkDEBUGFAIL("Invalid flag type"); |
419 return SkString(); | 438 return SkString(); |
420 } | 439 } |
421 } | 440 } |
422 | 441 |
423 private: | 442 private: |
424 SkFlagInfo(const char* name, const char* shortName, FlagTypes type, const ch
ar* helpString) | 443 SkFlagInfo(const char* name, const char* shortName, FlagTypes type, const ch
ar* helpString, |
| 444 const char* extendedHelpString) |
425 : fName(name) | 445 : fName(name) |
426 , fShortName(shortName) | 446 , fShortName(shortName) |
427 , fFlagType(type) | 447 , fFlagType(type) |
428 , fHelpString(helpString) | 448 , fHelpString(helpString) |
| 449 , fExtendedHelpString(extendedHelpString) |
429 , fBoolValue(nullptr) | 450 , fBoolValue(nullptr) |
430 , fDefaultBool(false) | 451 , fDefaultBool(false) |
431 , fIntValue(nullptr) | 452 , fIntValue(nullptr) |
432 , fDefaultInt(0) | 453 , fDefaultInt(0) |
433 , fDoubleValue(nullptr) | 454 , fDoubleValue(nullptr) |
434 , fDefaultDouble(0) | 455 , fDefaultDouble(0) |
435 , fStrings(nullptr) { | 456 , fStrings(nullptr) { |
436 fNext = SkCommandLineFlags::gHead; | 457 fNext = SkCommandLineFlags::gHead; |
437 SkCommandLineFlags::gHead = this; | 458 SkCommandLineFlags::gHead = this; |
438 SkASSERT(name && strlen(name) > 1); | 459 SkASSERT(name && strlen(name) > 1); |
439 SkASSERT(nullptr == shortName || 1 == strlen(shortName)); | 460 SkASSERT(nullptr == shortName || 1 == strlen(shortName)); |
440 } | 461 } |
441 | 462 |
442 /** | 463 /** |
443 * Set a StringArray to hold the values stored in defaultStrings. | 464 * Set a StringArray to hold the values stored in defaultStrings. |
444 * @param array The StringArray to modify. | 465 * @param array The StringArray to modify. |
445 * @param defaultStrings Space separated list of strings that should be ins
erted into array | 466 * @param defaultStrings Space separated list of strings that should be ins
erted into array |
446 * individually. | 467 * individually. |
447 */ | 468 */ |
448 static void SetDefaultStrings(SkCommandLineFlags::StringArray* array, | 469 static void SetDefaultStrings(SkCommandLineFlags::StringArray* array, |
449 const char* defaultStrings); | 470 const char* defaultStrings); |
450 | 471 |
451 // Name of the flag, without initial dashes | 472 // Name of the flag, without initial dashes |
452 SkString fName; | 473 SkString fName; |
453 SkString fShortName; | 474 SkString fShortName; |
454 FlagTypes fFlagType; | 475 FlagTypes fFlagType; |
455 SkString fHelpString; | 476 SkString fHelpString; |
| 477 SkString fExtendedHelpString; |
456 bool* fBoolValue; | 478 bool* fBoolValue; |
457 bool fDefaultBool; | 479 bool fDefaultBool; |
458 int32_t* fIntValue; | 480 int32_t* fIntValue; |
459 int32_t fDefaultInt; | 481 int32_t fDefaultInt; |
460 double* fDoubleValue; | 482 double* fDoubleValue; |
461 double fDefaultDouble; | 483 double fDefaultDouble; |
462 SkCommandLineFlags::StringArray* fStrings; | 484 SkCommandLineFlags::StringArray* fStrings; |
463 // Both for the help string and in case fStrings is empty. | 485 // Both for the help string and in case fStrings is empty. |
464 SkString fDefaultString; | 486 SkString fDefaultString; |
465 | 487 |
466 // In order to keep a linked list. | 488 // In order to keep a linked list. |
467 SkFlagInfo* fNext; | 489 SkFlagInfo* fNext; |
468 }; | 490 }; |
469 #endif // SK_COMMAND_LINE_FLAGS_H | 491 #endif // SK_COMMAND_LINE_FLAGS_H |
OLD | NEW |