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