scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
scroggo@google.com | d9ba9a0 | 2013-03-21 19:43:15 +0000 | [diff] [blame] | 8 | #ifndef SK_COMMAND_LINE_FLAGS_H |
| 9 | #define SK_COMMAND_LINE_FLAGS_H |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 10 | |
bungeman | bf521ff | 2016-02-17 13:13:44 -0800 | [diff] [blame] | 11 | #include "../private/SkTArray.h" |
bungeman | a7e9f05 | 2016-02-18 08:53:33 -0800 | [diff] [blame] | 12 | #include "../private/SkTDArray.h" |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 13 | #include "SkString.h" |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 14 | |
| 15 | /** |
scroggo@google.com | d9ba9a0 | 2013-03-21 19:43:15 +0000 | [diff] [blame] | 16 | * Including this file (and compiling SkCommandLineFlags.cpp) provides command line |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 17 | * parsing. In order to use it, use the following macros in global |
| 18 | * namespace: |
| 19 | * |
| 20 | * DEFINE_bool(name, defaultValue, helpString); |
| 21 | * DEFINE_string(name, defaultValue, helpString); |
| 22 | * DEFINE_int32(name, defaultValue, helpString); |
| 23 | * DEFINE_double(name, defaultValue, helpString); |
| 24 | * |
scroggo@google.com | d9ba9a0 | 2013-03-21 19:43:15 +0000 | [diff] [blame] | 25 | * Then, in main, call SkCommandLineFlags::SetUsage() to describe usage and call |
| 26 | * SkCommandLineFlags::Parse() to parse the flags. Henceforth, each flag can |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 27 | * be referenced using |
| 28 | * |
| 29 | * FLAGS_name |
| 30 | * |
| 31 | * For example, the line |
| 32 | * |
| 33 | * DEFINE_bool(boolean, false, "The variable boolean does such and such"); |
| 34 | * |
| 35 | * will create the following variable: |
| 36 | * |
| 37 | * bool FLAGS_boolean; |
| 38 | * |
| 39 | * which will initially be set to false, and can be set to true by using the |
| 40 | * flag "--boolean" on the commandline. "--noboolean" will set FLAGS_boolean |
scroggo@google.com | 5dc4ca1 | 2013-03-21 13:10:59 +0000 | [diff] [blame] | 41 | * to false. FLAGS_boolean can also be set using "--boolean=true" or |
| 42 | * "--boolean true" (where "true" can be replaced by "false", "TRUE", "FALSE", |
| 43 | * "1" or "0"). |
| 44 | * |
scroggo@google.com | 5dc4ca1 | 2013-03-21 13:10:59 +0000 | [diff] [blame] | 45 | * The helpString will be printed if the help flag (-h or -help) is used. |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 46 | * |
| 47 | * Similarly, the line |
| 48 | * |
| 49 | * DEFINE_int32(integer, .., ..); |
| 50 | * |
| 51 | * will create |
| 52 | * |
| 53 | * int32_t FLAGS_integer; |
| 54 | * |
| 55 | * and |
| 56 | * |
| 57 | * DEFINE_double(real, .., ..); |
| 58 | * |
| 59 | * will create |
| 60 | * |
| 61 | * double FLAGS_real; |
| 62 | * |
| 63 | * These flags can be set by specifying, for example, "--integer 7" and |
| 64 | * "--real 3.14" on the command line. |
| 65 | * |
| 66 | * Unlike the others, the line |
| 67 | * |
| 68 | * DEFINE_string(args, .., ..); |
| 69 | * |
| 70 | * creates an array: |
| 71 | * |
scroggo@google.com | b7dbf63 | 2013-04-23 15:38:09 +0000 | [diff] [blame] | 72 | * SkCommandLineFlags::StringArray FLAGS_args; |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 73 | * |
| 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 |
| 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. |
| 78 | * |
kkinnunen | 3e980c3 | 2015-12-23 01:33:00 -0800 | [diff] [blame] | 79 | * DEFINE_extended_string(args, .., .., extendedHelpString); |
| 80 | * |
| 81 | * creates a similar string array flag as DEFINE_string. The flag will have extended help text |
| 82 | * (extendedHelpString) that can the user can see with '--help <args>' flag. |
| 83 | * |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 84 | * Any flag can be referenced from another file after using the following: |
| 85 | * |
| 86 | * DECLARE_x(name); |
| 87 | * |
| 88 | * (where 'x' is the type specified in the DEFINE). |
| 89 | * |
| 90 | * 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 |
scroggo@google.com | d9ba9a0 | 2013-03-21 19:43:15 +0000 | [diff] [blame] | 92 | * a flag -h or -help which will never be used, since SkCommandLineFlags handles it. |
| 93 | * SkCommandLineFlags will also allow creating --flag and --noflag. Uses the same input |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 94 | * format as gflags and creates similarly named variables (i.e. FLAGS_name). |
| 95 | * Strings are handled differently (resulting variable will be an array of |
| 96 | * strings) so that a flag can be followed by multiple parameters. |
| 97 | */ |
| 98 | |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 99 | class SkFlagInfo; |
| 100 | |
scroggo@google.com | d9ba9a0 | 2013-03-21 19:43:15 +0000 | [diff] [blame] | 101 | class SkCommandLineFlags { |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 102 | |
| 103 | public: |
| 104 | /** |
| 105 | * Call to set the help message to be displayed. Should be called before |
scroggo@google.com | d9ba9a0 | 2013-03-21 19:43:15 +0000 | [diff] [blame] | 106 | * Parse. |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 107 | */ |
| 108 | static void SetUsage(const char* usage); |
| 109 | |
| 110 | /** |
msarett | 3478f75 | 2016-02-12 14:47:09 -0800 | [diff] [blame] | 111 | * Call this to display the help message. Should be called after SetUsage. |
| 112 | */ |
| 113 | static void PrintUsage(); |
| 114 | |
| 115 | /** |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 116 | * Call at the beginning of main to parse flags created by DEFINE_x, above. |
| 117 | * Must only be called once. |
| 118 | */ |
scroggo@google.com | d9ba9a0 | 2013-03-21 19:43:15 +0000 | [diff] [blame] | 119 | static void Parse(int argc, char** argv); |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 120 | |
scroggo@google.com | b7dbf63 | 2013-04-23 15:38:09 +0000 | [diff] [blame] | 121 | /** |
| 122 | * Custom class for holding the arguments for a string flag. |
| 123 | * Publicly only has accessors so the strings cannot be modified. |
| 124 | */ |
| 125 | class StringArray { |
| 126 | public: |
kkinnunen | 3e980c3 | 2015-12-23 01:33:00 -0800 | [diff] [blame] | 127 | StringArray() { } |
| 128 | explicit StringArray(const SkTArray<SkString>& strings) |
| 129 | : fStrings(strings) { |
| 130 | } |
scroggo@google.com | b7dbf63 | 2013-04-23 15:38:09 +0000 | [diff] [blame] | 131 | const char* operator[](int i) const { |
| 132 | SkASSERT(i >= 0 && i < fStrings.count()); |
| 133 | return fStrings[i].c_str(); |
| 134 | } |
| 135 | |
| 136 | int count() const { |
| 137 | return fStrings.count(); |
| 138 | } |
| 139 | |
| 140 | bool isEmpty() const { return this->count() == 0; } |
| 141 | |
epoger@google.com | defc487 | 2013-09-19 06:18:27 +0000 | [diff] [blame] | 142 | /** |
| 143 | * Returns true iff string is equal to one of the strings in this array. |
| 144 | */ |
| 145 | bool contains(const char* string) const { |
| 146 | for (int i = 0; i < fStrings.count(); i++) { |
| 147 | if (fStrings[i].equals(string)) { |
| 148 | return true; |
| 149 | } |
| 150 | } |
| 151 | return false; |
| 152 | } |
| 153 | |
caryclark | 936b734 | 2014-07-11 12:14:51 -0700 | [diff] [blame] | 154 | void set(int i, const char* str) { |
| 155 | fStrings[i].set(str); |
| 156 | } |
| 157 | |
scroggo@google.com | b7dbf63 | 2013-04-23 15:38:09 +0000 | [diff] [blame] | 158 | private: |
| 159 | void reset() { fStrings.reset(); } |
| 160 | |
| 161 | void append(const char* string) { |
| 162 | fStrings.push_back().set(string); |
| 163 | } |
| 164 | |
scroggo@google.com | 58104a9 | 2013-04-24 19:25:26 +0000 | [diff] [blame] | 165 | void append(const char* string, size_t length) { |
| 166 | fStrings.push_back().set(string, length); |
| 167 | } |
| 168 | |
scroggo@google.com | b7dbf63 | 2013-04-23 15:38:09 +0000 | [diff] [blame] | 169 | SkTArray<SkString> fStrings; |
| 170 | |
| 171 | friend class SkFlagInfo; |
| 172 | }; |
| 173 | |
commit-bot@chromium.org | a6f37e7 | 2013-08-30 15:52:46 +0000 | [diff] [blame] | 174 | /* Takes a list of the form [~][^]match[$] |
| 175 | ~ causes a matching test to always be skipped |
| 176 | ^ requires the start of the test to match |
| 177 | $ requires the end of the test to match |
| 178 | ^ and $ requires an exact match |
| 179 | If a test does not match any list entry, it is skipped unless some list entry starts with ~ |
| 180 | */ |
| 181 | static bool ShouldSkip(const SkTDArray<const char*>& strings, const char* name); |
| 182 | static bool ShouldSkip(const StringArray& strings, const char* name); |
| 183 | |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 184 | private: |
| 185 | static SkFlagInfo* gHead; |
| 186 | static SkString gUsage; |
| 187 | |
| 188 | // For access to gHead. |
| 189 | friend class SkFlagInfo; |
| 190 | }; |
| 191 | |
| 192 | #define TO_STRING2(s) #s |
| 193 | #define TO_STRING(s) TO_STRING2(s) |
| 194 | |
| 195 | #define DEFINE_bool(name, defaultValue, helpString) \ |
| 196 | bool FLAGS_##name; \ |
fmalita | 523beb7 | 2015-06-10 10:46:50 -0700 | [diff] [blame] | 197 | SK_UNUSED static bool unused_##name = SkFlagInfo::CreateBoolFlag(TO_STRING(name), \ |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 198 | nullptr, \ |
fmalita | 523beb7 | 2015-06-10 10:46:50 -0700 | [diff] [blame] | 199 | &FLAGS_##name, \ |
| 200 | defaultValue, \ |
| 201 | helpString) |
scroggo@google.com | 09fd4d2 | 2013-03-20 14:20:18 +0000 | [diff] [blame] | 202 | |
| 203 | // bool 2 allows specifying a short name. No check is done to ensure that shortName |
| 204 | // is actually shorter than name. |
| 205 | #define DEFINE_bool2(name, shortName, defaultValue, helpString) \ |
| 206 | bool FLAGS_##name; \ |
fmalita | 523beb7 | 2015-06-10 10:46:50 -0700 | [diff] [blame] | 207 | SK_UNUSED static bool unused_##name = SkFlagInfo::CreateBoolFlag(TO_STRING(name), \ |
| 208 | TO_STRING(shortName),\ |
| 209 | &FLAGS_##name, \ |
| 210 | defaultValue, \ |
| 211 | helpString) |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 212 | |
| 213 | #define DECLARE_bool(name) extern bool FLAGS_##name; |
| 214 | |
| 215 | #define DEFINE_string(name, defaultValue, helpString) \ |
scroggo@google.com | b7dbf63 | 2013-04-23 15:38:09 +0000 | [diff] [blame] | 216 | SkCommandLineFlags::StringArray FLAGS_##name; \ |
fmalita | 523beb7 | 2015-06-10 10:46:50 -0700 | [diff] [blame] | 217 | SK_UNUSED static bool unused_##name = SkFlagInfo::CreateStringFlag(TO_STRING(name), \ |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 218 | nullptr, \ |
fmalita | 523beb7 | 2015-06-10 10:46:50 -0700 | [diff] [blame] | 219 | &FLAGS_##name, \ |
| 220 | defaultValue, \ |
kkinnunen | 3e980c3 | 2015-12-23 01:33:00 -0800 | [diff] [blame] | 221 | helpString, nullptr) |
| 222 | #define DEFINE_extended_string(name, defaultValue, helpString, extendedHelpString) \ |
| 223 | SkCommandLineFlags::StringArray FLAGS_##name; \ |
| 224 | SK_UNUSED static bool unused_##name = SkFlagInfo::CreateStringFlag(TO_STRING(name), \ |
| 225 | nullptr, \ |
| 226 | &FLAGS_##name, \ |
| 227 | defaultValue, \ |
| 228 | helpString, \ |
| 229 | extendedHelpString) |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 230 | |
scroggo@google.com | 604e0c2 | 2013-04-09 21:25:46 +0000 | [diff] [blame] | 231 | // string2 allows specifying a short name. There is an assert that shortName |
| 232 | // is only 1 character. |
scroggo@google.com | 09fd4d2 | 2013-03-20 14:20:18 +0000 | [diff] [blame] | 233 | #define DEFINE_string2(name, shortName, defaultValue, helpString) \ |
scroggo@google.com | b7dbf63 | 2013-04-23 15:38:09 +0000 | [diff] [blame] | 234 | SkCommandLineFlags::StringArray FLAGS_##name; \ |
fmalita | 523beb7 | 2015-06-10 10:46:50 -0700 | [diff] [blame] | 235 | SK_UNUSED static bool unused_##name = SkFlagInfo::CreateStringFlag(TO_STRING(name), \ |
| 236 | TO_STRING(shortName), \ |
| 237 | &FLAGS_##name, \ |
| 238 | defaultValue, \ |
kkinnunen | 3e980c3 | 2015-12-23 01:33:00 -0800 | [diff] [blame] | 239 | helpString, nullptr) |
scroggo@google.com | 09fd4d2 | 2013-03-20 14:20:18 +0000 | [diff] [blame] | 240 | |
scroggo@google.com | b7dbf63 | 2013-04-23 15:38:09 +0000 | [diff] [blame] | 241 | #define DECLARE_string(name) extern SkCommandLineFlags::StringArray FLAGS_##name; |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 242 | |
robertphillips | 5ce341f | 2015-09-18 09:04:43 -0700 | [diff] [blame] | 243 | |
| 244 | |
| 245 | |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 246 | #define DEFINE_int32(name, defaultValue, helpString) \ |
| 247 | int32_t FLAGS_##name; \ |
fmalita | 523beb7 | 2015-06-10 10:46:50 -0700 | [diff] [blame] | 248 | SK_UNUSED static bool unused_##name = SkFlagInfo::CreateIntFlag(TO_STRING(name), \ |
| 249 | &FLAGS_##name, \ |
| 250 | defaultValue, \ |
| 251 | helpString) |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 252 | |
robertphillips | 5ce341f | 2015-09-18 09:04:43 -0700 | [diff] [blame] | 253 | #define DEFINE_int32_2(name, shortName, defaultValue, helpString) \ |
| 254 | int32_t FLAGS_##name; \ |
| 255 | SK_UNUSED static bool unused_##name = SkFlagInfo::CreateIntFlag(TO_STRING(name), \ |
| 256 | TO_STRING(shortName), \ |
| 257 | &FLAGS_##name, \ |
| 258 | defaultValue, \ |
| 259 | helpString) |
| 260 | |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 261 | #define DECLARE_int32(name) extern int32_t FLAGS_##name; |
| 262 | |
| 263 | #define DEFINE_double(name, defaultValue, helpString) \ |
| 264 | double FLAGS_##name; \ |
fmalita | 523beb7 | 2015-06-10 10:46:50 -0700 | [diff] [blame] | 265 | SK_UNUSED static bool unused_##name = SkFlagInfo::CreateDoubleFlag(TO_STRING(name), \ |
| 266 | &FLAGS_##name, \ |
| 267 | defaultValue, \ |
| 268 | helpString) |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 269 | |
| 270 | #define DECLARE_double(name) extern double FLAGS_##name; |
| 271 | |
| 272 | class SkFlagInfo { |
| 273 | |
| 274 | public: |
| 275 | enum FlagTypes { |
| 276 | kBool_FlagType, |
| 277 | kString_FlagType, |
| 278 | kInt_FlagType, |
| 279 | kDouble_FlagType, |
| 280 | }; |
| 281 | |
scroggo@google.com | 58104a9 | 2013-04-24 19:25:26 +0000 | [diff] [blame] | 282 | /** |
| 283 | * Each Create<Type>Flag function creates an SkFlagInfo of the specified type. The SkFlagInfo |
| 284 | * object is appended to a list, which is deleted when SkCommandLineFlags::Parse is called. |
| 285 | * Therefore, each call should be made before the call to ::Parse. They are not intended |
| 286 | * to be called directly. Instead, use the macros described above. |
| 287 | * @param name Long version (at least 2 characters) of the name of the flag. This name can |
| 288 | * be referenced on the command line as "--name" to set the value of this flag. |
| 289 | * @param shortName Short version (one character) of the name of the flag. This name can |
| 290 | * be referenced on the command line as "-shortName" to set the value of this flag. |
| 291 | * @param p<Type> Pointer to a global variable which holds the value set by SkCommandLineFlags. |
| 292 | * @param defaultValue The default value of this flag. The variable pointed to by p<Type> will |
| 293 | * be set to this value initially. This is also displayed as part of the help output. |
| 294 | * @param helpString Explanation of what this flag changes in the program. |
| 295 | */ |
scroggo@google.com | 09fd4d2 | 2013-03-20 14:20:18 +0000 | [diff] [blame] | 296 | static bool CreateBoolFlag(const char* name, const char* shortName, bool* pBool, |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 297 | bool defaultValue, const char* helpString) { |
kkinnunen | 3e980c3 | 2015-12-23 01:33:00 -0800 | [diff] [blame] | 298 | SkFlagInfo* info = new SkFlagInfo(name, shortName, kBool_FlagType, helpString, nullptr); |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 299 | info->fBoolValue = pBool; |
| 300 | *info->fBoolValue = info->fDefaultBool = defaultValue; |
| 301 | return true; |
| 302 | } |
| 303 | |
scroggo@google.com | 58104a9 | 2013-04-24 19:25:26 +0000 | [diff] [blame] | 304 | /** |
| 305 | * See comments for CreateBoolFlag. |
| 306 | * @param pStrings Unlike the others, this is a pointer to an array of values. |
| 307 | * @param defaultValue Thise default will be parsed so that strings separated by spaces |
| 308 | * will be added to pStrings. |
| 309 | */ |
scroggo@google.com | 09fd4d2 | 2013-03-20 14:20:18 +0000 | [diff] [blame] | 310 | static bool CreateStringFlag(const char* name, const char* shortName, |
scroggo@google.com | b7dbf63 | 2013-04-23 15:38:09 +0000 | [diff] [blame] | 311 | SkCommandLineFlags::StringArray* pStrings, |
kkinnunen | 3e980c3 | 2015-12-23 01:33:00 -0800 | [diff] [blame] | 312 | const char* defaultValue, const char* helpString, |
| 313 | const char* extendedHelpString); |
skia.committer@gmail.com | 075b089 | 2013-03-05 07:09:08 +0000 | [diff] [blame] | 314 | |
scroggo@google.com | 58104a9 | 2013-04-24 19:25:26 +0000 | [diff] [blame] | 315 | /** |
| 316 | * See comments for CreateBoolFlag. |
| 317 | */ |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 318 | static bool CreateIntFlag(const char* name, int32_t* pInt, |
| 319 | int32_t defaultValue, const char* helpString) { |
kkinnunen | 3e980c3 | 2015-12-23 01:33:00 -0800 | [diff] [blame] | 320 | SkFlagInfo* info = new SkFlagInfo(name, nullptr, kInt_FlagType, helpString, nullptr); |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 321 | info->fIntValue = pInt; |
| 322 | *info->fIntValue = info->fDefaultInt = defaultValue; |
| 323 | return true; |
| 324 | } |
| 325 | |
robertphillips | 5ce341f | 2015-09-18 09:04:43 -0700 | [diff] [blame] | 326 | static bool CreateIntFlag(const char* name, const char* shortName, int32_t* pInt, |
| 327 | int32_t defaultValue, const char* helpString) { |
kkinnunen | 3e980c3 | 2015-12-23 01:33:00 -0800 | [diff] [blame] | 328 | SkFlagInfo* info = new SkFlagInfo(name, shortName, kInt_FlagType, helpString, nullptr); |
robertphillips | 5ce341f | 2015-09-18 09:04:43 -0700 | [diff] [blame] | 329 | info->fIntValue = pInt; |
| 330 | *info->fIntValue = info->fDefaultInt = defaultValue; |
| 331 | return true; |
| 332 | } |
| 333 | |
scroggo@google.com | 58104a9 | 2013-04-24 19:25:26 +0000 | [diff] [blame] | 334 | /** |
| 335 | * See comments for CreateBoolFlag. |
| 336 | */ |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 337 | static bool CreateDoubleFlag(const char* name, double* pDouble, |
| 338 | double defaultValue, const char* helpString) { |
kkinnunen | 3e980c3 | 2015-12-23 01:33:00 -0800 | [diff] [blame] | 339 | SkFlagInfo* info = new SkFlagInfo(name, nullptr, kDouble_FlagType, helpString, nullptr); |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 340 | info->fDoubleValue = pDouble; |
| 341 | *info->fDoubleValue = info->fDefaultDouble = defaultValue; |
| 342 | return true; |
| 343 | } |
| 344 | |
| 345 | /** |
scroggo@google.com | 5dc4ca1 | 2013-03-21 13:10:59 +0000 | [diff] [blame] | 346 | * Returns true if the string matches this flag. |
| 347 | * For a boolean flag, also sets the value, since a boolean flag can be set in a number of ways |
| 348 | * without looking at the following string: |
| 349 | * --name |
| 350 | * --noname |
| 351 | * --name=true |
| 352 | * --name=false |
| 353 | * --name=1 |
| 354 | * --name=0 |
| 355 | * --name=TRUE |
| 356 | * --name=FALSE |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 357 | */ |
scroggo@google.com | 5dc4ca1 | 2013-03-21 13:10:59 +0000 | [diff] [blame] | 358 | bool match(const char* string); |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 359 | |
| 360 | FlagTypes getFlagType() const { return fFlagType; } |
| 361 | |
| 362 | void resetStrings() { |
| 363 | if (kString_FlagType == fFlagType) { |
| 364 | fStrings->reset(); |
| 365 | } else { |
mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame] | 366 | SkDEBUGFAIL("Can only call resetStrings on kString_FlagType"); |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 367 | } |
| 368 | } |
| 369 | |
| 370 | void append(const char* string) { |
| 371 | if (kString_FlagType == fFlagType) { |
scroggo@google.com | b7dbf63 | 2013-04-23 15:38:09 +0000 | [diff] [blame] | 372 | fStrings->append(string); |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 373 | } else { |
mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame] | 374 | SkDEBUGFAIL("Can only append to kString_FlagType"); |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 375 | } |
| 376 | } |
| 377 | |
| 378 | void setInt(int32_t value) { |
| 379 | if (kInt_FlagType == fFlagType) { |
| 380 | *fIntValue = value; |
| 381 | } else { |
mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame] | 382 | SkDEBUGFAIL("Can only call setInt on kInt_FlagType"); |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 383 | } |
| 384 | } |
| 385 | |
| 386 | void setDouble(double value) { |
| 387 | if (kDouble_FlagType == fFlagType) { |
| 388 | *fDoubleValue = value; |
| 389 | } else { |
mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame] | 390 | SkDEBUGFAIL("Can only call setDouble on kDouble_FlagType"); |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 391 | } |
| 392 | } |
| 393 | |
scroggo@google.com | 5dc4ca1 | 2013-03-21 13:10:59 +0000 | [diff] [blame] | 394 | void setBool(bool value) { |
| 395 | if (kBool_FlagType == fFlagType) { |
| 396 | *fBoolValue = value; |
| 397 | } else { |
mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame] | 398 | SkDEBUGFAIL("Can only call setBool on kBool_FlagType"); |
scroggo@google.com | 5dc4ca1 | 2013-03-21 13:10:59 +0000 | [diff] [blame] | 399 | } |
| 400 | } |
| 401 | |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 402 | SkFlagInfo* next() { return fNext; } |
| 403 | |
| 404 | const SkString& name() const { return fName; } |
| 405 | |
scroggo@google.com | 8366df0 | 2013-03-20 19:50:41 +0000 | [diff] [blame] | 406 | const SkString& shortName() const { return fShortName; } |
| 407 | |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 408 | const SkString& help() const { return fHelpString; } |
kkinnunen | 3e980c3 | 2015-12-23 01:33:00 -0800 | [diff] [blame] | 409 | const SkString& extendedHelp() const { return fExtendedHelpString; } |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 410 | |
| 411 | SkString defaultValue() const { |
| 412 | SkString result; |
| 413 | switch (fFlagType) { |
| 414 | case SkFlagInfo::kBool_FlagType: |
| 415 | result.printf("%s", fDefaultBool ? "true" : "false"); |
| 416 | break; |
| 417 | case SkFlagInfo::kString_FlagType: |
| 418 | return fDefaultString; |
| 419 | case SkFlagInfo::kInt_FlagType: |
| 420 | result.printf("%i", fDefaultInt); |
| 421 | break; |
| 422 | case SkFlagInfo::kDouble_FlagType: |
| 423 | result.printf("%2.2f", fDefaultDouble); |
| 424 | break; |
| 425 | default: |
mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame] | 426 | SkDEBUGFAIL("Invalid flag type"); |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 427 | } |
| 428 | return result; |
| 429 | } |
| 430 | |
| 431 | SkString typeAsString() const { |
| 432 | switch (fFlagType) { |
| 433 | case SkFlagInfo::kBool_FlagType: |
| 434 | return SkString("bool"); |
| 435 | case SkFlagInfo::kString_FlagType: |
| 436 | return SkString("string"); |
| 437 | case SkFlagInfo::kInt_FlagType: |
| 438 | return SkString("int"); |
| 439 | case SkFlagInfo::kDouble_FlagType: |
| 440 | return SkString("double"); |
| 441 | default: |
mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame] | 442 | SkDEBUGFAIL("Invalid flag type"); |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 443 | return SkString(); |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | private: |
kkinnunen | 3e980c3 | 2015-12-23 01:33:00 -0800 | [diff] [blame] | 448 | SkFlagInfo(const char* name, const char* shortName, FlagTypes type, const char* helpString, |
| 449 | const char* extendedHelpString) |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 450 | : fName(name) |
scroggo@google.com | 604e0c2 | 2013-04-09 21:25:46 +0000 | [diff] [blame] | 451 | , fShortName(shortName) |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 452 | , fFlagType(type) |
| 453 | , fHelpString(helpString) |
kkinnunen | 3e980c3 | 2015-12-23 01:33:00 -0800 | [diff] [blame] | 454 | , fExtendedHelpString(extendedHelpString) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 455 | , fBoolValue(nullptr) |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 456 | , fDefaultBool(false) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 457 | , fIntValue(nullptr) |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 458 | , fDefaultInt(0) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 459 | , fDoubleValue(nullptr) |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 460 | , fDefaultDouble(0) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 461 | , fStrings(nullptr) { |
scroggo@google.com | d9ba9a0 | 2013-03-21 19:43:15 +0000 | [diff] [blame] | 462 | fNext = SkCommandLineFlags::gHead; |
| 463 | SkCommandLineFlags::gHead = this; |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 464 | SkASSERT(name && strlen(name) > 1); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 465 | SkASSERT(nullptr == shortName || 1 == strlen(shortName)); |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 466 | } |
scroggo@google.com | 58104a9 | 2013-04-24 19:25:26 +0000 | [diff] [blame] | 467 | |
| 468 | /** |
| 469 | * Set a StringArray to hold the values stored in defaultStrings. |
| 470 | * @param array The StringArray to modify. |
| 471 | * @param defaultStrings Space separated list of strings that should be inserted into array |
| 472 | * individually. |
| 473 | */ |
| 474 | static void SetDefaultStrings(SkCommandLineFlags::StringArray* array, |
| 475 | const char* defaultStrings); |
| 476 | |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 477 | // Name of the flag, without initial dashes |
| 478 | SkString fName; |
scroggo@google.com | 09fd4d2 | 2013-03-20 14:20:18 +0000 | [diff] [blame] | 479 | SkString fShortName; |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 480 | FlagTypes fFlagType; |
| 481 | SkString fHelpString; |
kkinnunen | 3e980c3 | 2015-12-23 01:33:00 -0800 | [diff] [blame] | 482 | SkString fExtendedHelpString; |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 483 | bool* fBoolValue; |
| 484 | bool fDefaultBool; |
| 485 | int32_t* fIntValue; |
| 486 | int32_t fDefaultInt; |
| 487 | double* fDoubleValue; |
| 488 | double fDefaultDouble; |
scroggo@google.com | b7dbf63 | 2013-04-23 15:38:09 +0000 | [diff] [blame] | 489 | SkCommandLineFlags::StringArray* fStrings; |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 490 | // Both for the help string and in case fStrings is empty. |
| 491 | SkString fDefaultString; |
| 492 | |
| 493 | // In order to keep a linked list. |
| 494 | SkFlagInfo* fNext; |
| 495 | }; |
scroggo@google.com | d9ba9a0 | 2013-03-21 19:43:15 +0000 | [diff] [blame] | 496 | #endif // SK_COMMAND_LINE_FLAGS_H |