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 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
197 helpString) | 202 helpString) |
198 | 203 |
199 #define DECLARE_bool(name) extern bool FLAGS_##name; | 204 #define DECLARE_bool(name) extern bool FLAGS_##name; |
200 | 205 |
201 #define DEFINE_string(name, defaultValue, helpString) \ | 206 #define DEFINE_string(name, defaultValue, helpString) \ |
202 SkCommandLineFlags::StringArray FLAGS_##name; \ | 207 SkCommandLineFlags::StringArray FLAGS_##name; \ |
203 SK_UNUSED static bool unused_##name = SkFlagInfo::CreateStringFlag(TO_STRING(nam
e), \ | 208 SK_UNUSED static bool unused_##name = SkFlagInfo::CreateStringFlag(TO_STRING(nam
e), \ |
204 nullptr,
\ | 209 nullptr,
\ |
205 &FLAGS_##name
, \ | 210 &FLAGS_##name
, \ |
206 defaultValue,
\ | 211 defaultValue,
\ |
207 helpString) | 212 helpString, n
ullptr) |
| 213 #define DEFINE_extended_string(name, defaultValue, helpString, extendedHelpStrin
g) \ |
| 214 SkCommandLineFlags::StringArray FLAGS_##name;
\ |
| 215 SK_UNUSED static bool unused_##name = SkFlagInfo::CreateStringFlag(TO_STRING(nam
e), \ |
| 216 nullptr, \ |
| 217 &FLAGS_##name
, \ |
| 218 defaultValue,
\ |
| 219 helpString, \ |
| 220 extendedHelpS
tring) |
208 | 221 |
209 // string2 allows specifying a short name. There is an assert that shortName | 222 // string2 allows specifying a short name. There is an assert that shortName |
210 // is only 1 character. | 223 // is only 1 character. |
211 #define DEFINE_string2(name, shortName, defaultValue, helpString)
\ | 224 #define DEFINE_string2(name, shortName, defaultValue, helpString)
\ |
212 SkCommandLineFlags::StringArray FLAGS_##name;
\ | 225 SkCommandLineFlags::StringArray FLAGS_##name;
\ |
213 SK_UNUSED static bool unused_##name = SkFlagInfo::CreateStringFlag(TO_STRING(nam
e), \ | 226 SK_UNUSED static bool unused_##name = SkFlagInfo::CreateStringFlag(TO_STRING(nam
e), \ |
214 TO_STRING(sho
rtName), \ | 227 TO_STRING(sho
rtName), \ |
215 &FLAGS_##name
, \ | 228 &FLAGS_##name
, \ |
216 defaultValue,
\ | 229 defaultValue,
\ |
217 helpString) | 230 helpString, n
ullptr) |
218 | 231 |
219 #define DECLARE_string(name) extern SkCommandLineFlags::StringArray FLAGS_##name
; | 232 #define DECLARE_string(name) extern SkCommandLineFlags::StringArray FLAGS_##name
; |
220 | 233 |
221 | 234 |
222 | 235 |
223 | 236 |
224 #define DEFINE_int32(name, defaultValue, helpString) \ | 237 #define DEFINE_int32(name, defaultValue, helpString) \ |
225 int32_t FLAGS_##name; \ | 238 int32_t FLAGS_##name; \ |
226 SK_UNUSED static bool unused_##name = SkFlagInfo::CreateIntFlag(TO_STRING(name),
\ | 239 SK_UNUSED static bool unused_##name = SkFlagInfo::CreateIntFlag(TO_STRING(name),
\ |
227 &FLAGS_##name,
\ | 240 &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. | 279 * 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 | 280 * @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. | 281 * 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. | 282 * @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 | 283 * @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. | 284 * 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. | 285 * @param helpString Explanation of what this flag changes in the program. |
273 */ | 286 */ |
274 static bool CreateBoolFlag(const char* name, const char* shortName, bool* pB
ool, | 287 static bool CreateBoolFlag(const char* name, const char* shortName, bool* pB
ool, |
275 bool defaultValue, const char* helpString) { | 288 bool defaultValue, const char* helpString) { |
276 SkFlagInfo* info = new SkFlagInfo(name, shortName, kBool_FlagType, helpS
tring); | 289 SkFlagInfo* info = new SkFlagInfo(name, shortName, kBool_FlagType, helpS
tring, nullptr); |
277 info->fBoolValue = pBool; | 290 info->fBoolValue = pBool; |
278 *info->fBoolValue = info->fDefaultBool = defaultValue; | 291 *info->fBoolValue = info->fDefaultBool = defaultValue; |
279 return true; | 292 return true; |
280 } | 293 } |
281 | 294 |
282 /** | 295 /** |
283 * See comments for CreateBoolFlag. | 296 * See comments for CreateBoolFlag. |
284 * @param pStrings Unlike the others, this is a pointer to an array of valu
es. | 297 * @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 | 298 * @param defaultValue Thise default will be parsed so that strings separat
ed by spaces |
286 * will be added to pStrings. | 299 * will be added to pStrings. |
287 */ | 300 */ |
288 static bool CreateStringFlag(const char* name, const char* shortName, | 301 static bool CreateStringFlag(const char* name, const char* shortName, |
289 SkCommandLineFlags::StringArray* pStrings, | 302 SkCommandLineFlags::StringArray* pStrings, |
290 const char* defaultValue, const char* helpStrin
g); | 303 const char* defaultValue, const char* helpStrin
g, |
| 304 const char* extendedHelpString); |
291 | 305 |
292 /** | 306 /** |
293 * See comments for CreateBoolFlag. | 307 * See comments for CreateBoolFlag. |
294 */ | 308 */ |
295 static bool CreateIntFlag(const char* name, int32_t* pInt, | 309 static bool CreateIntFlag(const char* name, int32_t* pInt, |
296 int32_t defaultValue, const char* helpString) { | 310 int32_t defaultValue, const char* helpString) { |
297 SkFlagInfo* info = new SkFlagInfo(name, nullptr, kInt_FlagType, helpStri
ng); | 311 SkFlagInfo* info = new SkFlagInfo(name, nullptr, kInt_FlagType, helpStri
ng, nullptr); |
298 info->fIntValue = pInt; | 312 info->fIntValue = pInt; |
299 *info->fIntValue = info->fDefaultInt = defaultValue; | 313 *info->fIntValue = info->fDefaultInt = defaultValue; |
300 return true; | 314 return true; |
301 } | 315 } |
302 | 316 |
303 static bool CreateIntFlag(const char* name, const char* shortName, int32_t*
pInt, | 317 static bool CreateIntFlag(const char* name, const char* shortName, int32_t*
pInt, |
304 int32_t defaultValue, const char* helpString) { | 318 int32_t defaultValue, const char* helpString) { |
305 SkFlagInfo* info = new SkFlagInfo(name, shortName, kInt_FlagType, helpSt
ring); | 319 SkFlagInfo* info = new SkFlagInfo(name, shortName, kInt_FlagType, helpSt
ring, nullptr); |
306 info->fIntValue = pInt; | 320 info->fIntValue = pInt; |
307 *info->fIntValue = info->fDefaultInt = defaultValue; | 321 *info->fIntValue = info->fDefaultInt = defaultValue; |
308 return true; | 322 return true; |
309 } | 323 } |
310 | 324 |
311 /** | 325 /** |
312 * See comments for CreateBoolFlag. | 326 * See comments for CreateBoolFlag. |
313 */ | 327 */ |
314 static bool CreateDoubleFlag(const char* name, double* pDouble, | 328 static bool CreateDoubleFlag(const char* name, double* pDouble, |
315 double defaultValue, const char* helpString) { | 329 double defaultValue, const char* helpString) { |
316 SkFlagInfo* info = new SkFlagInfo(name, nullptr, kDouble_FlagType, helpS
tring); | 330 SkFlagInfo* info = new SkFlagInfo(name, nullptr, kDouble_FlagType, helpS
tring, nullptr); |
317 info->fDoubleValue = pDouble; | 331 info->fDoubleValue = pDouble; |
318 *info->fDoubleValue = info->fDefaultDouble = defaultValue; | 332 *info->fDoubleValue = info->fDefaultDouble = defaultValue; |
319 return true; | 333 return true; |
320 } | 334 } |
321 | 335 |
322 /** | 336 /** |
323 * Returns true if the string matches this flag. | 337 * 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 | 338 * 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: | 339 * without looking at the following string: |
326 * --name | 340 * --name |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
376 } | 390 } |
377 } | 391 } |
378 | 392 |
379 SkFlagInfo* next() { return fNext; } | 393 SkFlagInfo* next() { return fNext; } |
380 | 394 |
381 const SkString& name() const { return fName; } | 395 const SkString& name() const { return fName; } |
382 | 396 |
383 const SkString& shortName() const { return fShortName; } | 397 const SkString& shortName() const { return fShortName; } |
384 | 398 |
385 const SkString& help() const { return fHelpString; } | 399 const SkString& help() const { return fHelpString; } |
| 400 const SkString& extendedHelp() const { return fExtendedHelpString; } |
386 | 401 |
387 SkString defaultValue() const { | 402 SkString defaultValue() const { |
388 SkString result; | 403 SkString result; |
389 switch (fFlagType) { | 404 switch (fFlagType) { |
390 case SkFlagInfo::kBool_FlagType: | 405 case SkFlagInfo::kBool_FlagType: |
391 result.printf("%s", fDefaultBool ? "true" : "false"); | 406 result.printf("%s", fDefaultBool ? "true" : "false"); |
392 break; | 407 break; |
393 case SkFlagInfo::kString_FlagType: | 408 case SkFlagInfo::kString_FlagType: |
394 return fDefaultString; | 409 return fDefaultString; |
395 case SkFlagInfo::kInt_FlagType: | 410 case SkFlagInfo::kInt_FlagType: |
(...skipping 18 matching lines...) Expand all Loading... |
414 return SkString("int"); | 429 return SkString("int"); |
415 case SkFlagInfo::kDouble_FlagType: | 430 case SkFlagInfo::kDouble_FlagType: |
416 return SkString("double"); | 431 return SkString("double"); |
417 default: | 432 default: |
418 SkDEBUGFAIL("Invalid flag type"); | 433 SkDEBUGFAIL("Invalid flag type"); |
419 return SkString(); | 434 return SkString(); |
420 } | 435 } |
421 } | 436 } |
422 | 437 |
423 private: | 438 private: |
424 SkFlagInfo(const char* name, const char* shortName, FlagTypes type, const ch
ar* helpString) | 439 SkFlagInfo(const char* name, const char* shortName, FlagTypes type, const ch
ar* helpString, |
| 440 const char* extendedHelpString) |
425 : fName(name) | 441 : fName(name) |
426 , fShortName(shortName) | 442 , fShortName(shortName) |
427 , fFlagType(type) | 443 , fFlagType(type) |
428 , fHelpString(helpString) | 444 , fHelpString(helpString) |
| 445 , fExtendedHelpString(extendedHelpString) |
429 , fBoolValue(nullptr) | 446 , fBoolValue(nullptr) |
430 , fDefaultBool(false) | 447 , fDefaultBool(false) |
431 , fIntValue(nullptr) | 448 , fIntValue(nullptr) |
432 , fDefaultInt(0) | 449 , fDefaultInt(0) |
433 , fDoubleValue(nullptr) | 450 , fDoubleValue(nullptr) |
434 , fDefaultDouble(0) | 451 , fDefaultDouble(0) |
435 , fStrings(nullptr) { | 452 , fStrings(nullptr) { |
436 fNext = SkCommandLineFlags::gHead; | 453 fNext = SkCommandLineFlags::gHead; |
437 SkCommandLineFlags::gHead = this; | 454 SkCommandLineFlags::gHead = this; |
438 SkASSERT(name && strlen(name) > 1); | 455 SkASSERT(name && strlen(name) > 1); |
439 SkASSERT(nullptr == shortName || 1 == strlen(shortName)); | 456 SkASSERT(nullptr == shortName || 1 == strlen(shortName)); |
440 } | 457 } |
441 | 458 |
442 /** | 459 /** |
443 * Set a StringArray to hold the values stored in defaultStrings. | 460 * Set a StringArray to hold the values stored in defaultStrings. |
444 * @param array The StringArray to modify. | 461 * @param array The StringArray to modify. |
445 * @param defaultStrings Space separated list of strings that should be ins
erted into array | 462 * @param defaultStrings Space separated list of strings that should be ins
erted into array |
446 * individually. | 463 * individually. |
447 */ | 464 */ |
448 static void SetDefaultStrings(SkCommandLineFlags::StringArray* array, | 465 static void SetDefaultStrings(SkCommandLineFlags::StringArray* array, |
449 const char* defaultStrings); | 466 const char* defaultStrings); |
450 | 467 |
451 // Name of the flag, without initial dashes | 468 // Name of the flag, without initial dashes |
452 SkString fName; | 469 SkString fName; |
453 SkString fShortName; | 470 SkString fShortName; |
454 FlagTypes fFlagType; | 471 FlagTypes fFlagType; |
455 SkString fHelpString; | 472 SkString fHelpString; |
| 473 SkString fExtendedHelpString; |
456 bool* fBoolValue; | 474 bool* fBoolValue; |
457 bool fDefaultBool; | 475 bool fDefaultBool; |
458 int32_t* fIntValue; | 476 int32_t* fIntValue; |
459 int32_t fDefaultInt; | 477 int32_t fDefaultInt; |
460 double* fDoubleValue; | 478 double* fDoubleValue; |
461 double fDefaultDouble; | 479 double fDefaultDouble; |
462 SkCommandLineFlags::StringArray* fStrings; | 480 SkCommandLineFlags::StringArray* fStrings; |
463 // Both for the help string and in case fStrings is empty. | 481 // Both for the help string and in case fStrings is empty. |
464 SkString fDefaultString; | 482 SkString fDefaultString; |
465 | 483 |
466 // In order to keep a linked list. | 484 // In order to keep a linked list. |
467 SkFlagInfo* fNext; | 485 SkFlagInfo* fNext; |
468 }; | 486 }; |
469 #endif // SK_COMMAND_LINE_FLAGS_H | 487 #endif // SK_COMMAND_LINE_FLAGS_H |
OLD | NEW |