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_FLAGS_H |
9 #define SK_FLAGS_H | 9 #define SK_FLAGS_H |
10 | 10 |
(...skipping 19 matching lines...) Expand all Loading... | |
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 * |
36 * bool FLAGS_boolean; | 36 * bool FLAGS_boolean; |
37 * | 37 * |
38 * which will initially be set to false, and can be set to true by using the | 38 * which will initially be set to false, and can be set to true by using the |
39 * flag "--boolean" on the commandline. "--noboolean" will set FLAGS_boolean | 39 * flag "--boolean" on the commandline. "--noboolean" will set FLAGS_boolean |
40 * to false. (Single dashes are also permitted for this and other flags.) The | 40 * to false. FLAGS_boolean can also be set using "--boolean=true" or |
41 * helpString will be printed if the help flag (-h or -help) is used. | 41 * "--boolean true" (where "true" can be replaced by "false", "TRUE", "FALSE", |
42 * "1" or "0"). | |
43 * | |
44 * Single dashes are also permitted for this and other flags. | |
45 * | |
46 * The helpString will be printed if the help flag (-h or -help) is used. | |
reed1
2013/03/20 20:09:41
do you mean --help or -help?
scroggo
2013/03/20 21:12:05
In reality, I mean --help or -help or -h or --h. F
| |
42 * | 47 * |
43 * Similarly, the line | 48 * Similarly, the line |
44 * | 49 * |
45 * DEFINE_int32(integer, .., ..); | 50 * DEFINE_int32(integer, .., ..); |
46 * | 51 * |
47 * will create | 52 * will create |
48 * | 53 * |
49 * int32_t FLAGS_integer; | 54 * int32_t FLAGS_integer; |
50 * | 55 * |
51 * and | 56 * and |
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
220 | 225 |
221 static bool CreateDoubleFlag(const char* name, double* pDouble, | 226 static bool CreateDoubleFlag(const char* name, double* pDouble, |
222 double defaultValue, const char* helpString) { | 227 double defaultValue, const char* helpString) { |
223 SkFlagInfo* info = SkNEW_ARGS(SkFlagInfo, (name, kDouble_FlagType, helpS tring)); | 228 SkFlagInfo* info = SkNEW_ARGS(SkFlagInfo, (name, kDouble_FlagType, helpS tring)); |
224 info->fDoubleValue = pDouble; | 229 info->fDoubleValue = pDouble; |
225 *info->fDoubleValue = info->fDefaultDouble = defaultValue; | 230 *info->fDoubleValue = info->fDefaultDouble = defaultValue; |
226 return true; | 231 return true; |
227 } | 232 } |
228 | 233 |
229 /** | 234 /** |
230 * Returns true if the string matches this flag. For a bool, also sets the | 235 * Check to see whether string represents a boolean value. |
231 * value, since a bool is specified as true or false by --name or --noname. | 236 * @param string C style string to parse. |
237 * @param result Pointer to a boolean which will be set to the value in the string, if the | |
238 * string represents a boolean. | |
239 * @param boolean True if the string represents a boolean, false otherwise. | |
240 */ | |
241 static bool ParseBoolArg(const char* string, bool* result) { | |
reed1
2013/03/20 20:09:41
Is it cleaner to use a small table of strings for
scroggo
2013/03/20 21:12:05
Done.
| |
242 SkString parameter(string); | |
243 if (parameter.equals("1") || parameter.equals("TRUE") || parameter.equal s("true")) { | |
244 *result = true; | |
245 return true; | |
246 } | |
247 if (parameter.equals("0") || parameter.equals("FALSE") || parameter.equa ls("false")) { | |
248 *result = false; | |
249 return true; | |
250 } | |
251 SkDebugf("Parameter \"%s\" not supported.\n", string); | |
252 return false; | |
253 } | |
254 | |
255 /** | |
256 * Returns true if the string matches this flag. | |
257 * For a boolean flag, also sets the value, since a boolean flag can be set in a number of ways | |
258 * without looking at the following string: | |
259 * --name | |
260 * --noname | |
261 * --name=true | |
262 * --name=false | |
263 * --name=1 | |
264 * --name=0 | |
265 * --name=TRUE | |
266 * --name=FALSE | |
232 */ | 267 */ |
233 bool match(const char* string) { | 268 bool match(const char* string) { |
234 if (SkStrStartsWith(string, '-') && strlen(string) > 1) { | 269 if (SkStrStartsWith(string, '-') && strlen(string) > 1) { |
235 string++; | 270 string++; |
236 // Allow one or two dashes | 271 // Allow one or two dashes |
237 if (SkStrStartsWith(string, '-') && strlen(string) > 1) { | 272 if (SkStrStartsWith(string, '-') && strlen(string) > 1) { |
238 string++; | 273 string++; |
239 } | 274 } |
240 if (kBool_FlagType == fFlagType) { | 275 if (kBool_FlagType == fFlagType) { |
241 // In this case, go ahead and set the value. | 276 // In this case, go ahead and set the value. |
242 if (fName.equals(string) || fShortName.equals(string)) { | 277 if (fName.equals(string) || fShortName.equals(string)) { |
243 *fBoolValue = true; | 278 *fBoolValue = true; |
244 return true; | 279 return true; |
245 } | 280 } |
246 if (SkStrStartsWith(string, "no") && strlen(string) > 2) { | 281 if (SkStrStartsWith(string, "no") && strlen(string) > 2) { |
247 string += 2; | 282 string += 2; |
248 if (fName.equals(string) || fShortName.equals(string)) { | 283 if (fName.equals(string) || fShortName.equals(string)) { |
249 *fBoolValue = false; | 284 *fBoolValue = false; |
250 return true; | 285 return true; |
251 } | 286 } |
252 return false; | 287 return false; |
253 } | 288 } |
289 int equalIndex = SkStrFind(string, "="); | |
290 if (equalIndex > 0) { | |
291 // The string has an equal sign. Check to see if the string matches. | |
292 SkString flag(string, equalIndex); | |
293 if (flag.equals(fName) || flag.equals(fShortName)) { | |
294 // Check to see if the remainder beyond the equal sign i s true or false: | |
295 string += equalIndex + 1; | |
296 ParseBoolArg(string, fBoolValue); | |
297 return true; | |
298 } | |
299 } | |
254 } | 300 } |
255 return fName.equals(string) || fShortName.equals(string); | 301 return fName.equals(string) || fShortName.equals(string); |
256 } else { | 302 } else { |
257 // Has no dash | 303 // Has no dash |
258 return false; | 304 return false; |
259 } | 305 } |
260 return false; | 306 return false; |
261 } | 307 } |
262 | 308 |
263 FlagTypes getFlagType() const { return fFlagType; } | 309 FlagTypes getFlagType() const { return fFlagType; } |
(...skipping 23 matching lines...) Expand all Loading... | |
287 } | 333 } |
288 | 334 |
289 void setDouble(double value) { | 335 void setDouble(double value) { |
290 if (kDouble_FlagType == fFlagType) { | 336 if (kDouble_FlagType == fFlagType) { |
291 *fDoubleValue = value; | 337 *fDoubleValue = value; |
292 } else { | 338 } else { |
293 SkASSERT(!"Can only call setDouble on kDouble_FlagType"); | 339 SkASSERT(!"Can only call setDouble on kDouble_FlagType"); |
294 } | 340 } |
295 } | 341 } |
296 | 342 |
343 void setBool(bool value) { | |
344 if (kBool_FlagType == fFlagType) { | |
345 *fBoolValue = value; | |
346 } else { | |
347 SkASSERT(!"Can only call setBool on kBool_FlagType"); | |
348 } | |
349 } | |
350 | |
297 SkFlagInfo* next() { return fNext; } | 351 SkFlagInfo* next() { return fNext; } |
298 | 352 |
299 const SkString& name() const { return fName; } | 353 const SkString& name() const { return fName; } |
300 | 354 |
301 const SkString& help() const { return fHelpString; } | 355 const SkString& help() const { return fHelpString; } |
302 | 356 |
303 SkString defaultValue() const { | 357 SkString defaultValue() const { |
304 SkString result; | 358 SkString result; |
305 switch (fFlagType) { | 359 switch (fFlagType) { |
306 case SkFlagInfo::kBool_FlagType: | 360 case SkFlagInfo::kBool_FlagType: |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
363 double* fDoubleValue; | 417 double* fDoubleValue; |
364 double fDefaultDouble; | 418 double fDefaultDouble; |
365 SkTDArray<const char*>* fStrings; | 419 SkTDArray<const char*>* fStrings; |
366 // Both for the help string and in case fStrings is empty. | 420 // Both for the help string and in case fStrings is empty. |
367 SkString fDefaultString; | 421 SkString fDefaultString; |
368 | 422 |
369 // In order to keep a linked list. | 423 // In order to keep a linked list. |
370 SkFlagInfo* fNext; | 424 SkFlagInfo* fNext; |
371 }; | 425 }; |
372 #endif // SK_FLAGS_H | 426 #endif // SK_FLAGS_H |
OLD | NEW |