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 18 matching lines...) Expand all Loading... |
112 * Must only be called once. | 107 * Must only be called once. |
113 */ | 108 */ |
114 static void Parse(int argc, char** argv); | 109 static void Parse(int argc, char** argv); |
115 | 110 |
116 /** | 111 /** |
117 * Custom class for holding the arguments for a string flag. | 112 * Custom class for holding the arguments for a string flag. |
118 * Publicly only has accessors so the strings cannot be modified. | 113 * Publicly only has accessors so the strings cannot be modified. |
119 */ | 114 */ |
120 class StringArray { | 115 class StringArray { |
121 public: | 116 public: |
122 StringArray() { } | |
123 explicit StringArray(const SkTArray<SkString>& strings) | |
124 : fStrings(strings) { | |
125 } | |
126 const char* operator[](int i) const { | 117 const char* operator[](int i) const { |
127 SkASSERT(i >= 0 && i < fStrings.count()); | 118 SkASSERT(i >= 0 && i < fStrings.count()); |
128 return fStrings[i].c_str(); | 119 return fStrings[i].c_str(); |
129 } | 120 } |
130 | 121 |
131 int count() const { | 122 int count() const { |
132 return fStrings.count(); | 123 return fStrings.count(); |
133 } | 124 } |
134 | 125 |
135 bool isEmpty() const { return this->count() == 0; } | 126 bool isEmpty() const { return this->count() == 0; } |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
206 helpString) | 197 helpString) |
207 | 198 |
208 #define DECLARE_bool(name) extern bool FLAGS_##name; | 199 #define DECLARE_bool(name) extern bool FLAGS_##name; |
209 | 200 |
210 #define DEFINE_string(name, defaultValue, helpString) \ | 201 #define DEFINE_string(name, defaultValue, helpString) \ |
211 SkCommandLineFlags::StringArray FLAGS_##name; \ | 202 SkCommandLineFlags::StringArray FLAGS_##name; \ |
212 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), \ |
213 nullptr,
\ | 204 nullptr,
\ |
214 &FLAGS_##name
, \ | 205 &FLAGS_##name
, \ |
215 defaultValue,
\ | 206 defaultValue,
\ |
216 helpString, n
ullptr) | 207 helpString) |
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) | |
225 | 208 |
226 // 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 |
227 // is only 1 character. | 210 // is only 1 character. |
228 #define DEFINE_string2(name, shortName, defaultValue, helpString)
\ | 211 #define DEFINE_string2(name, shortName, defaultValue, helpString)
\ |
229 SkCommandLineFlags::StringArray FLAGS_##name;
\ | 212 SkCommandLineFlags::StringArray FLAGS_##name;
\ |
230 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), \ |
231 TO_STRING(sho
rtName), \ | 214 TO_STRING(sho
rtName), \ |
232 &FLAGS_##name
, \ | 215 &FLAGS_##name
, \ |
233 defaultValue,
\ | 216 defaultValue,
\ |
234 helpString, n
ullptr) | 217 helpString) |
235 | 218 |
236 #define DECLARE_string(name) extern SkCommandLineFlags::StringArray FLAGS_##name
; | 219 #define DECLARE_string(name) extern SkCommandLineFlags::StringArray FLAGS_##name
; |
237 | 220 |
238 | 221 |
239 | 222 |
240 | 223 |
241 #define DEFINE_int32(name, defaultValue, helpString) \ | 224 #define DEFINE_int32(name, defaultValue, helpString) \ |
242 int32_t FLAGS_##name; \ | 225 int32_t FLAGS_##name; \ |
243 SK_UNUSED static bool unused_##name = SkFlagInfo::CreateIntFlag(TO_STRING(name),
\ | 226 SK_UNUSED static bool unused_##name = SkFlagInfo::CreateIntFlag(TO_STRING(name),
\ |
244 &FLAGS_##name,
\ | 227 &FLAGS_##name,
\ |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
283 * 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. |
284 * @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 |
285 * 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. |
286 * @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. |
287 * @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 |
288 * 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. |
289 * @param helpString Explanation of what this flag changes in the program. | 272 * @param helpString Explanation of what this flag changes in the program. |
290 */ | 273 */ |
291 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, |
292 bool defaultValue, const char* helpString) { | 275 bool defaultValue, const char* helpString) { |
293 SkFlagInfo* info = new SkFlagInfo(name, shortName, kBool_FlagType, helpS
tring, nullptr); | 276 SkFlagInfo* info = new SkFlagInfo(name, shortName, kBool_FlagType, helpS
tring); |
294 info->fBoolValue = pBool; | 277 info->fBoolValue = pBool; |
295 *info->fBoolValue = info->fDefaultBool = defaultValue; | 278 *info->fBoolValue = info->fDefaultBool = defaultValue; |
296 return true; | 279 return true; |
297 } | 280 } |
298 | 281 |
299 /** | 282 /** |
300 * See comments for CreateBoolFlag. | 283 * See comments for CreateBoolFlag. |
301 * @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. |
302 * @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 |
303 * will be added to pStrings. | 286 * will be added to pStrings. |
304 */ | 287 */ |
305 static bool CreateStringFlag(const char* name, const char* shortName, | 288 static bool CreateStringFlag(const char* name, const char* shortName, |
306 SkCommandLineFlags::StringArray* pStrings, | 289 SkCommandLineFlags::StringArray* pStrings, |
307 const char* defaultValue, const char* helpStrin
g, | 290 const char* defaultValue, const char* helpStrin
g); |
308 const char* extendedHelpString); | |
309 | 291 |
310 /** | 292 /** |
311 * See comments for CreateBoolFlag. | 293 * See comments for CreateBoolFlag. |
312 */ | 294 */ |
313 static bool CreateIntFlag(const char* name, int32_t* pInt, | 295 static bool CreateIntFlag(const char* name, int32_t* pInt, |
314 int32_t defaultValue, const char* helpString) { | 296 int32_t defaultValue, const char* helpString) { |
315 SkFlagInfo* info = new SkFlagInfo(name, nullptr, kInt_FlagType, helpStri
ng, nullptr); | 297 SkFlagInfo* info = new SkFlagInfo(name, nullptr, kInt_FlagType, helpStri
ng); |
316 info->fIntValue = pInt; | 298 info->fIntValue = pInt; |
317 *info->fIntValue = info->fDefaultInt = defaultValue; | 299 *info->fIntValue = info->fDefaultInt = defaultValue; |
318 return true; | 300 return true; |
319 } | 301 } |
320 | 302 |
321 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, |
322 int32_t defaultValue, const char* helpString) { | 304 int32_t defaultValue, const char* helpString) { |
323 SkFlagInfo* info = new SkFlagInfo(name, shortName, kInt_FlagType, helpSt
ring, nullptr); | 305 SkFlagInfo* info = new SkFlagInfo(name, shortName, kInt_FlagType, helpSt
ring); |
324 info->fIntValue = pInt; | 306 info->fIntValue = pInt; |
325 *info->fIntValue = info->fDefaultInt = defaultValue; | 307 *info->fIntValue = info->fDefaultInt = defaultValue; |
326 return true; | 308 return true; |
327 } | 309 } |
328 | 310 |
329 /** | 311 /** |
330 * See comments for CreateBoolFlag. | 312 * See comments for CreateBoolFlag. |
331 */ | 313 */ |
332 static bool CreateDoubleFlag(const char* name, double* pDouble, | 314 static bool CreateDoubleFlag(const char* name, double* pDouble, |
333 double defaultValue, const char* helpString) { | 315 double defaultValue, const char* helpString) { |
334 SkFlagInfo* info = new SkFlagInfo(name, nullptr, kDouble_FlagType, helpS
tring, nullptr); | 316 SkFlagInfo* info = new SkFlagInfo(name, nullptr, kDouble_FlagType, helpS
tring); |
335 info->fDoubleValue = pDouble; | 317 info->fDoubleValue = pDouble; |
336 *info->fDoubleValue = info->fDefaultDouble = defaultValue; | 318 *info->fDoubleValue = info->fDefaultDouble = defaultValue; |
337 return true; | 319 return true; |
338 } | 320 } |
339 | 321 |
340 /** | 322 /** |
341 * Returns true if the string matches this flag. | 323 * Returns true if the string matches this flag. |
342 * 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 |
343 * without looking at the following string: | 325 * without looking at the following string: |
344 * --name | 326 * --name |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
394 } | 376 } |
395 } | 377 } |
396 | 378 |
397 SkFlagInfo* next() { return fNext; } | 379 SkFlagInfo* next() { return fNext; } |
398 | 380 |
399 const SkString& name() const { return fName; } | 381 const SkString& name() const { return fName; } |
400 | 382 |
401 const SkString& shortName() const { return fShortName; } | 383 const SkString& shortName() const { return fShortName; } |
402 | 384 |
403 const SkString& help() const { return fHelpString; } | 385 const SkString& help() const { return fHelpString; } |
404 const SkString& extendedHelp() const { return fExtendedHelpString; } | |
405 | 386 |
406 SkString defaultValue() const { | 387 SkString defaultValue() const { |
407 SkString result; | 388 SkString result; |
408 switch (fFlagType) { | 389 switch (fFlagType) { |
409 case SkFlagInfo::kBool_FlagType: | 390 case SkFlagInfo::kBool_FlagType: |
410 result.printf("%s", fDefaultBool ? "true" : "false"); | 391 result.printf("%s", fDefaultBool ? "true" : "false"); |
411 break; | 392 break; |
412 case SkFlagInfo::kString_FlagType: | 393 case SkFlagInfo::kString_FlagType: |
413 return fDefaultString; | 394 return fDefaultString; |
414 case SkFlagInfo::kInt_FlagType: | 395 case SkFlagInfo::kInt_FlagType: |
(...skipping 18 matching lines...) Expand all Loading... |
433 return SkString("int"); | 414 return SkString("int"); |
434 case SkFlagInfo::kDouble_FlagType: | 415 case SkFlagInfo::kDouble_FlagType: |
435 return SkString("double"); | 416 return SkString("double"); |
436 default: | 417 default: |
437 SkDEBUGFAIL("Invalid flag type"); | 418 SkDEBUGFAIL("Invalid flag type"); |
438 return SkString(); | 419 return SkString(); |
439 } | 420 } |
440 } | 421 } |
441 | 422 |
442 private: | 423 private: |
443 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) |
444 const char* extendedHelpString) | |
445 : fName(name) | 425 : fName(name) |
446 , fShortName(shortName) | 426 , fShortName(shortName) |
447 , fFlagType(type) | 427 , fFlagType(type) |
448 , fHelpString(helpString) | 428 , fHelpString(helpString) |
449 , fExtendedHelpString(extendedHelpString) | |
450 , fBoolValue(nullptr) | 429 , fBoolValue(nullptr) |
451 , fDefaultBool(false) | 430 , fDefaultBool(false) |
452 , fIntValue(nullptr) | 431 , fIntValue(nullptr) |
453 , fDefaultInt(0) | 432 , fDefaultInt(0) |
454 , fDoubleValue(nullptr) | 433 , fDoubleValue(nullptr) |
455 , fDefaultDouble(0) | 434 , fDefaultDouble(0) |
456 , fStrings(nullptr) { | 435 , fStrings(nullptr) { |
457 fNext = SkCommandLineFlags::gHead; | 436 fNext = SkCommandLineFlags::gHead; |
458 SkCommandLineFlags::gHead = this; | 437 SkCommandLineFlags::gHead = this; |
459 SkASSERT(name && strlen(name) > 1); | 438 SkASSERT(name && strlen(name) > 1); |
460 SkASSERT(nullptr == shortName || 1 == strlen(shortName)); | 439 SkASSERT(nullptr == shortName || 1 == strlen(shortName)); |
461 } | 440 } |
462 | 441 |
463 /** | 442 /** |
464 * Set a StringArray to hold the values stored in defaultStrings. | 443 * Set a StringArray to hold the values stored in defaultStrings. |
465 * @param array The StringArray to modify. | 444 * @param array The StringArray to modify. |
466 * @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 |
467 * individually. | 446 * individually. |
468 */ | 447 */ |
469 static void SetDefaultStrings(SkCommandLineFlags::StringArray* array, | 448 static void SetDefaultStrings(SkCommandLineFlags::StringArray* array, |
470 const char* defaultStrings); | 449 const char* defaultStrings); |
471 | 450 |
472 // Name of the flag, without initial dashes | 451 // Name of the flag, without initial dashes |
473 SkString fName; | 452 SkString fName; |
474 SkString fShortName; | 453 SkString fShortName; |
475 FlagTypes fFlagType; | 454 FlagTypes fFlagType; |
476 SkString fHelpString; | 455 SkString fHelpString; |
477 SkString fExtendedHelpString; | |
478 bool* fBoolValue; | 456 bool* fBoolValue; |
479 bool fDefaultBool; | 457 bool fDefaultBool; |
480 int32_t* fIntValue; | 458 int32_t* fIntValue; |
481 int32_t fDefaultInt; | 459 int32_t fDefaultInt; |
482 double* fDoubleValue; | 460 double* fDoubleValue; |
483 double fDefaultDouble; | 461 double fDefaultDouble; |
484 SkCommandLineFlags::StringArray* fStrings; | 462 SkCommandLineFlags::StringArray* fStrings; |
485 // Both for the help string and in case fStrings is empty. | 463 // Both for the help string and in case fStrings is empty. |
486 SkString fDefaultString; | 464 SkString fDefaultString; |
487 | 465 |
488 // In order to keep a linked list. | 466 // In order to keep a linked list. |
489 SkFlagInfo* fNext; | 467 SkFlagInfo* fNext; |
490 }; | 468 }; |
491 #endif // SK_COMMAND_LINE_FLAGS_H | 469 #endif // SK_COMMAND_LINE_FLAGS_H |
OLD | NEW |