| 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_FLAGS_H | 8 #ifndef SK_COMMAND_LINE_FLAGS_H |
| 9 #define SK_FLAGS_H | 9 #define SK_COMMAND_LINE_FLAGS_H |
| 10 | 10 |
| 11 #include "SkString.h" | 11 #include "SkString.h" |
| 12 #include "SkTDArray.h" | 12 #include "SkTDArray.h" |
| 13 | 13 |
| 14 /** | 14 /** |
| 15 * Including this file (and compiling SkFlags.cpp) provides command line | 15 * Including this file (and compiling SkCommandLineFlags.cpp) provides command
line |
| 16 * parsing. In order to use it, use the following macros in global | 16 * parsing. In order to use it, use the following macros in global |
| 17 * namespace: | 17 * namespace: |
| 18 * | 18 * |
| 19 * DEFINE_bool(name, defaultValue, helpString); | 19 * DEFINE_bool(name, defaultValue, helpString); |
| 20 * DEFINE_string(name, defaultValue, helpString); | 20 * DEFINE_string(name, defaultValue, helpString); |
| 21 * DEFINE_int32(name, defaultValue, helpString); | 21 * DEFINE_int32(name, defaultValue, helpString); |
| 22 * DEFINE_double(name, defaultValue, helpString); | 22 * DEFINE_double(name, defaultValue, helpString); |
| 23 * | 23 * |
| 24 * Then, in main, call SkFlags::SetUsage() to describe usage and call | 24 * Then, in main, call SkCommandLineFlags::SetUsage() to describe usage and cal
l |
| 25 * SkFlags::ParseCommandLine() to parse the flags. Henceforth, each flag can | 25 * SkCommandLineFlags::Parse() to parse the flags. Henceforth, each flag can |
| 26 * be referenced using | 26 * be referenced using |
| 27 * | 27 * |
| 28 * FLAGS_name | 28 * FLAGS_name |
| 29 * | 29 * |
| 30 * For example, the line | 30 * For example, the line |
| 31 * | 31 * |
| 32 * DEFINE_bool(boolean, false, "The variable boolean does such and such"); | 32 * DEFINE_bool(boolean, false, "The variable boolean does such and such"); |
| 33 * | 33 * |
| 34 * will create the following variable: | 34 * will create the following variable: |
| 35 * | 35 * |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 78 * a string that begins with '-') will be entries in the array. | 78 * a string that begins with '-') will be entries in the array. |
| 79 * | 79 * |
| 80 * Any flag can be referenced from another file after using the following: | 80 * Any flag can be referenced from another file after using the following: |
| 81 * | 81 * |
| 82 * DECLARE_x(name); | 82 * DECLARE_x(name); |
| 83 * | 83 * |
| 84 * (where 'x' is the type specified in the DEFINE). | 84 * (where 'x' is the type specified in the DEFINE). |
| 85 * | 85 * |
| 86 * Inspired by gflags (https://code.google.com/p/gflags/). Is not quite as | 86 * Inspired by gflags (https://code.google.com/p/gflags/). Is not quite as |
| 87 * robust as gflags, but suits our purposes. For example, allows creating | 87 * robust as gflags, but suits our purposes. For example, allows creating |
| 88 * a flag -h or -help which will never be used, since SkFlags handles it. | 88 * a flag -h or -help which will never be used, since SkCommandLineFlags handle
s it. |
| 89 * SkFlags will also allow creating --flag and --noflag. Uses the same input | 89 * SkCommandLineFlags will also allow creating --flag and --noflag. Uses the sa
me input |
| 90 * format as gflags and creates similarly named variables (i.e. FLAGS_name). | 90 * format as gflags and creates similarly named variables (i.e. FLAGS_name). |
| 91 * Strings are handled differently (resulting variable will be an array of | 91 * Strings are handled differently (resulting variable will be an array of |
| 92 * strings) so that a flag can be followed by multiple parameters. | 92 * strings) so that a flag can be followed by multiple parameters. |
| 93 */ | 93 */ |
| 94 | 94 |
| 95 | 95 |
| 96 class SkFlagInfo; | 96 class SkFlagInfo; |
| 97 | 97 |
| 98 class SkFlags { | 98 class SkCommandLineFlags { |
| 99 | 99 |
| 100 public: | 100 public: |
| 101 /** | 101 /** |
| 102 * Call to set the help message to be displayed. Should be called before | 102 * Call to set the help message to be displayed. Should be called before |
| 103 * ParseCommandLine. | 103 * Parse. |
| 104 */ | 104 */ |
| 105 static void SetUsage(const char* usage); | 105 static void SetUsage(const char* usage); |
| 106 | 106 |
| 107 /** | 107 /** |
| 108 * Call at the beginning of main to parse flags created by DEFINE_x, above. | 108 * Call at the beginning of main to parse flags created by DEFINE_x, above. |
| 109 * Must only be called once. | 109 * Must only be called once. |
| 110 */ | 110 */ |
| 111 static void ParseCommandLine(int argc, char** argv); | 111 static void Parse(int argc, char** argv); |
| 112 | 112 |
| 113 private: | 113 private: |
| 114 static SkFlagInfo* gHead; | 114 static SkFlagInfo* gHead; |
| 115 static SkString gUsage; | 115 static SkString gUsage; |
| 116 | 116 |
| 117 // For access to gHead. | 117 // For access to gHead. |
| 118 friend class SkFlagInfo; | 118 friend class SkFlagInfo; |
| 119 }; | 119 }; |
| 120 | 120 |
| 121 #define TO_STRING2(s) #s | 121 #define TO_STRING2(s) #s |
| (...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 337 : fName(name) | 337 : fName(name) |
| 338 , fFlagType(type) | 338 , fFlagType(type) |
| 339 , fHelpString(helpString) | 339 , fHelpString(helpString) |
| 340 , fBoolValue(NULL) | 340 , fBoolValue(NULL) |
| 341 , fDefaultBool(false) | 341 , fDefaultBool(false) |
| 342 , fIntValue(NULL) | 342 , fIntValue(NULL) |
| 343 , fDefaultInt(0) | 343 , fDefaultInt(0) |
| 344 , fDoubleValue(NULL) | 344 , fDoubleValue(NULL) |
| 345 , fDefaultDouble(0) | 345 , fDefaultDouble(0) |
| 346 , fStrings(NULL) { | 346 , fStrings(NULL) { |
| 347 fNext = SkFlags::gHead; | 347 fNext = SkCommandLineFlags::gHead; |
| 348 SkFlags::gHead = this; | 348 SkCommandLineFlags::gHead = this; |
| 349 } | 349 } |
| 350 // Name of the flag, without initial dashes | 350 // Name of the flag, without initial dashes |
| 351 SkString fName; | 351 SkString fName; |
| 352 SkString fShortName; | 352 SkString fShortName; |
| 353 FlagTypes fFlagType; | 353 FlagTypes fFlagType; |
| 354 SkString fHelpString; | 354 SkString fHelpString; |
| 355 bool* fBoolValue; | 355 bool* fBoolValue; |
| 356 bool fDefaultBool; | 356 bool fDefaultBool; |
| 357 int32_t* fIntValue; | 357 int32_t* fIntValue; |
| 358 int32_t fDefaultInt; | 358 int32_t fDefaultInt; |
| 359 double* fDoubleValue; | 359 double* fDoubleValue; |
| 360 double fDefaultDouble; | 360 double fDefaultDouble; |
| 361 SkTDArray<const char*>* fStrings; | 361 SkTDArray<const char*>* fStrings; |
| 362 // Both for the help string and in case fStrings is empty. | 362 // Both for the help string and in case fStrings is empty. |
| 363 SkString fDefaultString; | 363 SkString fDefaultString; |
| 364 | 364 |
| 365 // In order to keep a linked list. | 365 // In order to keep a linked list. |
| 366 SkFlagInfo* fNext; | 366 SkFlagInfo* fNext; |
| 367 }; | 367 }; |
| 368 #endif // SK_FLAGS_H | 368 #endif // SK_COMMAND_LINE_FLAGS_H |
| OLD | NEW |