blob: bf4a41e70d80e4d17fe73ec8078b945f9cc3c847 [file] [log] [blame]
scroggo@google.com161e1ba2013-03-04 16:41:06 +00001/*
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.comd9ba9a02013-03-21 19:43:15 +00008#ifndef SK_COMMAND_LINE_FLAGS_H
9#define SK_COMMAND_LINE_FLAGS_H
scroggo@google.com161e1ba2013-03-04 16:41:06 +000010
bungemanbf521ff2016-02-17 13:13:44 -080011#include "../private/SkTArray.h"
bungemana7e9f052016-02-18 08:53:33 -080012#include "../private/SkTDArray.h"
scroggo@google.com161e1ba2013-03-04 16:41:06 +000013#include "SkString.h"
scroggo@google.com161e1ba2013-03-04 16:41:06 +000014
15/**
scroggo@google.comd9ba9a02013-03-21 19:43:15 +000016 * Including this file (and compiling SkCommandLineFlags.cpp) provides command line
scroggo@google.com161e1ba2013-03-04 16:41:06 +000017 * 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.comd9ba9a02013-03-21 19:43:15 +000025 * Then, in main, call SkCommandLineFlags::SetUsage() to describe usage and call
26 * SkCommandLineFlags::Parse() to parse the flags. Henceforth, each flag can
scroggo@google.com161e1ba2013-03-04 16:41:06 +000027 * 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.com5dc4ca12013-03-21 13:10:59 +000041 * 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.com5dc4ca12013-03-21 13:10:59 +000045 * The helpString will be printed if the help flag (-h or -help) is used.
scroggo@google.com161e1ba2013-03-04 16:41:06 +000046 *
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 *
Michael Ludwig3ad86d02018-09-28 16:00:38 -040063 * and
64 *
65 * DEFINE_uint32(unsigned, ...);
66 *
67 * will create
68 *
69 * uint32_t FLAGS_unsigned;
70 *
scroggo@google.com161e1ba2013-03-04 16:41:06 +000071 * These flags can be set by specifying, for example, "--integer 7" and
Michael Ludwig3ad86d02018-09-28 16:00:38 -040072 * "--real 3.14" on the command line. Unsigned integers are parsed from the
73 * command line using strtoul() so will detect the base (0 for octal, and
74 * 0x or 0X for hex, otherwise assumes decimal).
scroggo@google.com161e1ba2013-03-04 16:41:06 +000075 *
76 * Unlike the others, the line
77 *
78 * DEFINE_string(args, .., ..);
79 *
80 * creates an array:
81 *
scroggo@google.comb7dbf632013-04-23 15:38:09 +000082 * SkCommandLineFlags::StringArray FLAGS_args;
scroggo@google.com161e1ba2013-03-04 16:41:06 +000083 *
84 * If the default value is the empty string, FLAGS_args will default to a size
85 * of zero. Otherwise it will default to a size of 1 with the default string
86 * as its value. All strings that follow the flag on the command line (until
87 * a string that begins with '-') will be entries in the array.
88 *
kkinnunen3e980c32015-12-23 01:33:00 -080089 * DEFINE_extended_string(args, .., .., extendedHelpString);
90 *
91 * creates a similar string array flag as DEFINE_string. The flag will have extended help text
92 * (extendedHelpString) that can the user can see with '--help <args>' flag.
93 *
scroggo@google.com161e1ba2013-03-04 16:41:06 +000094 * Any flag can be referenced from another file after using the following:
95 *
96 * DECLARE_x(name);
97 *
98 * (where 'x' is the type specified in the DEFINE).
99 *
100 * Inspired by gflags (https://code.google.com/p/gflags/). Is not quite as
101 * robust as gflags, but suits our purposes. For example, allows creating
scroggo@google.comd9ba9a02013-03-21 19:43:15 +0000102 * a flag -h or -help which will never be used, since SkCommandLineFlags handles it.
103 * SkCommandLineFlags will also allow creating --flag and --noflag. Uses the same input
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000104 * format as gflags and creates similarly named variables (i.e. FLAGS_name).
105 * Strings are handled differently (resulting variable will be an array of
106 * strings) so that a flag can be followed by multiple parameters.
107 */
108
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000109class SkFlagInfo;
110
scroggo@google.comd9ba9a02013-03-21 19:43:15 +0000111class SkCommandLineFlags {
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000112
113public:
114 /**
115 * Call to set the help message to be displayed. Should be called before
scroggo@google.comd9ba9a02013-03-21 19:43:15 +0000116 * Parse.
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000117 */
118 static void SetUsage(const char* usage);
119
120 /**
msarett3478f752016-02-12 14:47:09 -0800121 * Call this to display the help message. Should be called after SetUsage.
122 */
123 static void PrintUsage();
124
125 /**
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000126 * Call at the beginning of main to parse flags created by DEFINE_x, above.
127 * Must only be called once.
128 */
Cary Clark7265ea32017-09-14 12:12:32 -0400129 static void Parse(int argc, const char* const * argv);
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000130
scroggo@google.comb7dbf632013-04-23 15:38:09 +0000131 /**
132 * Custom class for holding the arguments for a string flag.
133 * Publicly only has accessors so the strings cannot be modified.
134 */
135 class StringArray {
136 public:
kkinnunen3e980c32015-12-23 01:33:00 -0800137 StringArray() { }
138 explicit StringArray(const SkTArray<SkString>& strings)
139 : fStrings(strings) {
140 }
scroggo@google.comb7dbf632013-04-23 15:38:09 +0000141 const char* operator[](int i) const {
142 SkASSERT(i >= 0 && i < fStrings.count());
143 return fStrings[i].c_str();
144 }
145
146 int count() const {
147 return fStrings.count();
148 }
149
150 bool isEmpty() const { return this->count() == 0; }
151
epoger@google.comdefc4872013-09-19 06:18:27 +0000152 /**
153 * Returns true iff string is equal to one of the strings in this array.
154 */
155 bool contains(const char* string) const {
156 for (int i = 0; i < fStrings.count(); i++) {
157 if (fStrings[i].equals(string)) {
158 return true;
159 }
160 }
161 return false;
162 }
163
caryclark936b7342014-07-11 12:14:51 -0700164 void set(int i, const char* str) {
165 fStrings[i].set(str);
166 }
167
Florin Malita094ccde2017-12-30 12:27:00 -0500168 const SkString* begin() const { return fStrings.begin(); }
169 const SkString* end() const { return fStrings.end(); }
170
scroggo@google.comb7dbf632013-04-23 15:38:09 +0000171 private:
172 void reset() { fStrings.reset(); }
173
174 void append(const char* string) {
175 fStrings.push_back().set(string);
176 }
177
scroggo@google.com58104a92013-04-24 19:25:26 +0000178 void append(const char* string, size_t length) {
179 fStrings.push_back().set(string, length);
180 }
181
scroggo@google.comb7dbf632013-04-23 15:38:09 +0000182 SkTArray<SkString> fStrings;
183
184 friend class SkFlagInfo;
185 };
186
commit-bot@chromium.orga6f37e72013-08-30 15:52:46 +0000187 /* Takes a list of the form [~][^]match[$]
188 ~ causes a matching test to always be skipped
189 ^ requires the start of the test to match
190 $ requires the end of the test to match
191 ^ and $ requires an exact match
192 If a test does not match any list entry, it is skipped unless some list entry starts with ~
193 */
194 static bool ShouldSkip(const SkTDArray<const char*>& strings, const char* name);
195 static bool ShouldSkip(const StringArray& strings, const char* name);
196
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000197private:
198 static SkFlagInfo* gHead;
199 static SkString gUsage;
200
201 // For access to gHead.
202 friend class SkFlagInfo;
203};
204
205#define TO_STRING2(s) #s
206#define TO_STRING(s) TO_STRING2(s)
207
208#define DEFINE_bool(name, defaultValue, helpString) \
209bool FLAGS_##name; \
fmalita523beb72015-06-10 10:46:50 -0700210SK_UNUSED static bool unused_##name = SkFlagInfo::CreateBoolFlag(TO_STRING(name), \
halcanary96fcdcc2015-08-27 07:41:13 -0700211 nullptr, \
fmalita523beb72015-06-10 10:46:50 -0700212 &FLAGS_##name, \
213 defaultValue, \
214 helpString)
scroggo@google.com09fd4d22013-03-20 14:20:18 +0000215
216// bool 2 allows specifying a short name. No check is done to ensure that shortName
217// is actually shorter than name.
218#define DEFINE_bool2(name, shortName, defaultValue, helpString) \
219bool FLAGS_##name; \
fmalita523beb72015-06-10 10:46:50 -0700220SK_UNUSED static bool unused_##name = SkFlagInfo::CreateBoolFlag(TO_STRING(name), \
221 TO_STRING(shortName),\
222 &FLAGS_##name, \
223 defaultValue, \
224 helpString)
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000225
226#define DECLARE_bool(name) extern bool FLAGS_##name;
227
228#define DEFINE_string(name, defaultValue, helpString) \
scroggo@google.comb7dbf632013-04-23 15:38:09 +0000229SkCommandLineFlags::StringArray FLAGS_##name; \
fmalita523beb72015-06-10 10:46:50 -0700230SK_UNUSED static bool unused_##name = SkFlagInfo::CreateStringFlag(TO_STRING(name), \
halcanary96fcdcc2015-08-27 07:41:13 -0700231 nullptr, \
fmalita523beb72015-06-10 10:46:50 -0700232 &FLAGS_##name, \
233 defaultValue, \
kkinnunen3e980c32015-12-23 01:33:00 -0800234 helpString, nullptr)
235#define DEFINE_extended_string(name, defaultValue, helpString, extendedHelpString) \
236SkCommandLineFlags::StringArray FLAGS_##name; \
237SK_UNUSED static bool unused_##name = SkFlagInfo::CreateStringFlag(TO_STRING(name), \
238 nullptr, \
239 &FLAGS_##name, \
240 defaultValue, \
241 helpString, \
242 extendedHelpString)
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000243
scroggo@google.com604e0c22013-04-09 21:25:46 +0000244// string2 allows specifying a short name. There is an assert that shortName
245// is only 1 character.
scroggo@google.com09fd4d22013-03-20 14:20:18 +0000246#define DEFINE_string2(name, shortName, defaultValue, helpString) \
scroggo@google.comb7dbf632013-04-23 15:38:09 +0000247SkCommandLineFlags::StringArray FLAGS_##name; \
fmalita523beb72015-06-10 10:46:50 -0700248SK_UNUSED static bool unused_##name = SkFlagInfo::CreateStringFlag(TO_STRING(name), \
249 TO_STRING(shortName), \
250 &FLAGS_##name, \
251 defaultValue, \
kkinnunen3e980c32015-12-23 01:33:00 -0800252 helpString, nullptr)
scroggo@google.com09fd4d22013-03-20 14:20:18 +0000253
scroggo@google.comb7dbf632013-04-23 15:38:09 +0000254#define DECLARE_string(name) extern SkCommandLineFlags::StringArray FLAGS_##name;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000255
robertphillips5ce341f2015-09-18 09:04:43 -0700256
257
258
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000259#define DEFINE_int32(name, defaultValue, helpString) \
260int32_t FLAGS_##name; \
fmalita523beb72015-06-10 10:46:50 -0700261SK_UNUSED static bool unused_##name = SkFlagInfo::CreateIntFlag(TO_STRING(name), \
262 &FLAGS_##name, \
263 defaultValue, \
264 helpString)
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000265
robertphillips5ce341f2015-09-18 09:04:43 -0700266#define DEFINE_int32_2(name, shortName, defaultValue, helpString) \
267int32_t FLAGS_##name; \
268SK_UNUSED static bool unused_##name = SkFlagInfo::CreateIntFlag(TO_STRING(name), \
269 TO_STRING(shortName), \
270 &FLAGS_##name, \
271 defaultValue, \
272 helpString)
273
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000274#define DECLARE_int32(name) extern int32_t FLAGS_##name;
275
Michael Ludwig3ad86d02018-09-28 16:00:38 -0400276
277#define DEFINE_uint32(name, defaultValue, helpString) \
278uint32_t FLAGS_##name; \
279SK_UNUSED static bool unused_##name = SkFlagInfo::CreateUintFlag(TO_STRING(name), \
280 &FLAGS_##name, \
281 defaultValue, \
282 helpString)
283
284#define DEFINE_uint32_2(name, shortName, defaultValue, helpString) \
285uint32_t FLAGS_##name; \
286SK_UNUSED static bool unused_##name = SkFlagInfo::CreateUintFlag(TO_STRING(name), \
287 TO_STRING(shortName),\
288 &FLAGS_##name, \
289 defaultValue, \
290 helpString)
291
292#define DECLARE_uint32(name) extern uint32_t FLAGS_##name;
293
294
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000295#define DEFINE_double(name, defaultValue, helpString) \
296double FLAGS_##name; \
fmalita523beb72015-06-10 10:46:50 -0700297SK_UNUSED static bool unused_##name = SkFlagInfo::CreateDoubleFlag(TO_STRING(name), \
298 &FLAGS_##name, \
299 defaultValue, \
300 helpString)
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000301
302#define DECLARE_double(name) extern double FLAGS_##name;
303
304class SkFlagInfo {
305
306public:
307 enum FlagTypes {
308 kBool_FlagType,
309 kString_FlagType,
310 kInt_FlagType,
Michael Ludwig3ad86d02018-09-28 16:00:38 -0400311 kUint_FlagType,
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000312 kDouble_FlagType,
313 };
314
scroggo@google.com58104a92013-04-24 19:25:26 +0000315 /**
316 * Each Create<Type>Flag function creates an SkFlagInfo of the specified type. The SkFlagInfo
317 * object is appended to a list, which is deleted when SkCommandLineFlags::Parse is called.
318 * Therefore, each call should be made before the call to ::Parse. They are not intended
319 * to be called directly. Instead, use the macros described above.
320 * @param name Long version (at least 2 characters) of the name of the flag. This name can
321 * be referenced on the command line as "--name" to set the value of this flag.
322 * @param shortName Short version (one character) of the name of the flag. This name can
323 * be referenced on the command line as "-shortName" to set the value of this flag.
324 * @param p<Type> Pointer to a global variable which holds the value set by SkCommandLineFlags.
325 * @param defaultValue The default value of this flag. The variable pointed to by p<Type> will
326 * be set to this value initially. This is also displayed as part of the help output.
327 * @param helpString Explanation of what this flag changes in the program.
328 */
scroggo@google.com09fd4d22013-03-20 14:20:18 +0000329 static bool CreateBoolFlag(const char* name, const char* shortName, bool* pBool,
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000330 bool defaultValue, const char* helpString) {
kkinnunen3e980c32015-12-23 01:33:00 -0800331 SkFlagInfo* info = new SkFlagInfo(name, shortName, kBool_FlagType, helpString, nullptr);
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000332 info->fBoolValue = pBool;
333 *info->fBoolValue = info->fDefaultBool = defaultValue;
334 return true;
335 }
336
scroggo@google.com58104a92013-04-24 19:25:26 +0000337 /**
338 * See comments for CreateBoolFlag.
339 * @param pStrings Unlike the others, this is a pointer to an array of values.
340 * @param defaultValue Thise default will be parsed so that strings separated by spaces
341 * will be added to pStrings.
342 */
scroggo@google.com09fd4d22013-03-20 14:20:18 +0000343 static bool CreateStringFlag(const char* name, const char* shortName,
scroggo@google.comb7dbf632013-04-23 15:38:09 +0000344 SkCommandLineFlags::StringArray* pStrings,
kkinnunen3e980c32015-12-23 01:33:00 -0800345 const char* defaultValue, const char* helpString,
346 const char* extendedHelpString);
skia.committer@gmail.com075b0892013-03-05 07:09:08 +0000347
scroggo@google.com58104a92013-04-24 19:25:26 +0000348 /**
349 * See comments for CreateBoolFlag.
350 */
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000351 static bool CreateIntFlag(const char* name, int32_t* pInt,
352 int32_t defaultValue, const char* helpString) {
kkinnunen3e980c32015-12-23 01:33:00 -0800353 SkFlagInfo* info = new SkFlagInfo(name, nullptr, kInt_FlagType, helpString, nullptr);
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000354 info->fIntValue = pInt;
355 *info->fIntValue = info->fDefaultInt = defaultValue;
356 return true;
357 }
358
robertphillips5ce341f2015-09-18 09:04:43 -0700359 static bool CreateIntFlag(const char* name, const char* shortName, int32_t* pInt,
360 int32_t defaultValue, const char* helpString) {
kkinnunen3e980c32015-12-23 01:33:00 -0800361 SkFlagInfo* info = new SkFlagInfo(name, shortName, kInt_FlagType, helpString, nullptr);
robertphillips5ce341f2015-09-18 09:04:43 -0700362 info->fIntValue = pInt;
363 *info->fIntValue = info->fDefaultInt = defaultValue;
364 return true;
365 }
366
scroggo@google.com58104a92013-04-24 19:25:26 +0000367 /**
368 * See comments for CreateBoolFlag.
369 */
Michael Ludwig3ad86d02018-09-28 16:00:38 -0400370 static bool CreateUintFlag(const char* name, uint32_t* pUint,
371 uint32_t defaultValue, const char* helpString) {
372 SkFlagInfo* info = new SkFlagInfo(name, nullptr, kUint_FlagType, helpString, nullptr);
373 info->fUintValue = pUint;
374 *info->fUintValue = info->fDefaultUint = defaultValue;
375 return true;
376 }
377
378 static bool CreateUintFlag(const char* name, const char* shortName, uint32_t* pUint,
379 uint32_t defaultValue, const char* helpString) {
380 SkFlagInfo* info = new SkFlagInfo(name, shortName, kUint_FlagType, helpString, nullptr);
381 info->fUintValue = pUint;
382 *info->fUintValue = info->fDefaultUint = defaultValue;
383 return true;
384 }
385
386 /**
387 * See comments for CreateBoolFlag.
388 */
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000389 static bool CreateDoubleFlag(const char* name, double* pDouble,
390 double defaultValue, const char* helpString) {
kkinnunen3e980c32015-12-23 01:33:00 -0800391 SkFlagInfo* info = new SkFlagInfo(name, nullptr, kDouble_FlagType, helpString, nullptr);
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000392 info->fDoubleValue = pDouble;
393 *info->fDoubleValue = info->fDefaultDouble = defaultValue;
394 return true;
395 }
396
397 /**
scroggo@google.com5dc4ca12013-03-21 13:10:59 +0000398 * Returns true if the string matches this flag.
399 * For a boolean flag, also sets the value, since a boolean flag can be set in a number of ways
400 * without looking at the following string:
401 * --name
402 * --noname
403 * --name=true
404 * --name=false
405 * --name=1
406 * --name=0
407 * --name=TRUE
408 * --name=FALSE
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000409 */
scroggo@google.com5dc4ca12013-03-21 13:10:59 +0000410 bool match(const char* string);
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000411
412 FlagTypes getFlagType() const { return fFlagType; }
413
414 void resetStrings() {
415 if (kString_FlagType == fFlagType) {
416 fStrings->reset();
417 } else {
mtklein@google.com330313a2013-08-22 15:37:26 +0000418 SkDEBUGFAIL("Can only call resetStrings on kString_FlagType");
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000419 }
420 }
421
422 void append(const char* string) {
423 if (kString_FlagType == fFlagType) {
scroggo@google.comb7dbf632013-04-23 15:38:09 +0000424 fStrings->append(string);
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000425 } else {
mtklein@google.com330313a2013-08-22 15:37:26 +0000426 SkDEBUGFAIL("Can only append to kString_FlagType");
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000427 }
428 }
429
430 void setInt(int32_t value) {
431 if (kInt_FlagType == fFlagType) {
432 *fIntValue = value;
433 } else {
mtklein@google.com330313a2013-08-22 15:37:26 +0000434 SkDEBUGFAIL("Can only call setInt on kInt_FlagType");
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000435 }
436 }
437
Michael Ludwig3ad86d02018-09-28 16:00:38 -0400438 void setUint(uint32_t value) {
439 if (kUint_FlagType == fFlagType) {
440 *fUintValue = value;
441 } else {
442 SkDEBUGFAIL("Can only call setUint on kUint_FlagType");
443 }
444 }
445
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000446 void setDouble(double value) {
447 if (kDouble_FlagType == fFlagType) {
448 *fDoubleValue = value;
449 } else {
mtklein@google.com330313a2013-08-22 15:37:26 +0000450 SkDEBUGFAIL("Can only call setDouble on kDouble_FlagType");
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000451 }
452 }
453
scroggo@google.com5dc4ca12013-03-21 13:10:59 +0000454 void setBool(bool value) {
455 if (kBool_FlagType == fFlagType) {
456 *fBoolValue = value;
457 } else {
mtklein@google.com330313a2013-08-22 15:37:26 +0000458 SkDEBUGFAIL("Can only call setBool on kBool_FlagType");
scroggo@google.com5dc4ca12013-03-21 13:10:59 +0000459 }
460 }
461
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000462 SkFlagInfo* next() { return fNext; }
463
464 const SkString& name() const { return fName; }
465
scroggo@google.com8366df02013-03-20 19:50:41 +0000466 const SkString& shortName() const { return fShortName; }
467
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000468 const SkString& help() const { return fHelpString; }
kkinnunen3e980c32015-12-23 01:33:00 -0800469 const SkString& extendedHelp() const { return fExtendedHelpString; }
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000470
471 SkString defaultValue() const {
472 SkString result;
473 switch (fFlagType) {
474 case SkFlagInfo::kBool_FlagType:
475 result.printf("%s", fDefaultBool ? "true" : "false");
476 break;
477 case SkFlagInfo::kString_FlagType:
478 return fDefaultString;
479 case SkFlagInfo::kInt_FlagType:
480 result.printf("%i", fDefaultInt);
481 break;
Michael Ludwig3ad86d02018-09-28 16:00:38 -0400482 case SkFlagInfo::kUint_FlagType:
483 result.printf("0x%08x", fDefaultUint);
484 break;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000485 case SkFlagInfo::kDouble_FlagType:
486 result.printf("%2.2f", fDefaultDouble);
487 break;
488 default:
mtklein@google.com330313a2013-08-22 15:37:26 +0000489 SkDEBUGFAIL("Invalid flag type");
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000490 }
491 return result;
492 }
493
494 SkString typeAsString() const {
495 switch (fFlagType) {
496 case SkFlagInfo::kBool_FlagType:
497 return SkString("bool");
498 case SkFlagInfo::kString_FlagType:
499 return SkString("string");
500 case SkFlagInfo::kInt_FlagType:
501 return SkString("int");
Michael Ludwig3ad86d02018-09-28 16:00:38 -0400502 case SkFlagInfo::kUint_FlagType:
503 return SkString("uint");
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000504 case SkFlagInfo::kDouble_FlagType:
505 return SkString("double");
506 default:
mtklein@google.com330313a2013-08-22 15:37:26 +0000507 SkDEBUGFAIL("Invalid flag type");
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000508 return SkString();
509 }
510 }
511
512private:
kkinnunen3e980c32015-12-23 01:33:00 -0800513 SkFlagInfo(const char* name, const char* shortName, FlagTypes type, const char* helpString,
514 const char* extendedHelpString)
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000515 : fName(name)
scroggo@google.com604e0c22013-04-09 21:25:46 +0000516 , fShortName(shortName)
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000517 , fFlagType(type)
518 , fHelpString(helpString)
kkinnunen3e980c32015-12-23 01:33:00 -0800519 , fExtendedHelpString(extendedHelpString)
halcanary96fcdcc2015-08-27 07:41:13 -0700520 , fBoolValue(nullptr)
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000521 , fDefaultBool(false)
halcanary96fcdcc2015-08-27 07:41:13 -0700522 , fIntValue(nullptr)
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000523 , fDefaultInt(0)
Michael Ludwig3ad86d02018-09-28 16:00:38 -0400524 , fUintValue(nullptr)
525 , fDefaultUint(0)
halcanary96fcdcc2015-08-27 07:41:13 -0700526 , fDoubleValue(nullptr)
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000527 , fDefaultDouble(0)
halcanary96fcdcc2015-08-27 07:41:13 -0700528 , fStrings(nullptr) {
scroggo@google.comd9ba9a02013-03-21 19:43:15 +0000529 fNext = SkCommandLineFlags::gHead;
530 SkCommandLineFlags::gHead = this;
bsalomon49f085d2014-09-05 13:34:00 -0700531 SkASSERT(name && strlen(name) > 1);
halcanary96fcdcc2015-08-27 07:41:13 -0700532 SkASSERT(nullptr == shortName || 1 == strlen(shortName));
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000533 }
scroggo@google.com58104a92013-04-24 19:25:26 +0000534
535 /**
536 * Set a StringArray to hold the values stored in defaultStrings.
537 * @param array The StringArray to modify.
538 * @param defaultStrings Space separated list of strings that should be inserted into array
539 * individually.
540 */
541 static void SetDefaultStrings(SkCommandLineFlags::StringArray* array,
542 const char* defaultStrings);
543
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000544 // Name of the flag, without initial dashes
545 SkString fName;
scroggo@google.com09fd4d22013-03-20 14:20:18 +0000546 SkString fShortName;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000547 FlagTypes fFlagType;
548 SkString fHelpString;
kkinnunen3e980c32015-12-23 01:33:00 -0800549 SkString fExtendedHelpString;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000550 bool* fBoolValue;
551 bool fDefaultBool;
552 int32_t* fIntValue;
553 int32_t fDefaultInt;
Michael Ludwig3ad86d02018-09-28 16:00:38 -0400554 uint32_t* fUintValue;
555 uint32_t fDefaultUint;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000556 double* fDoubleValue;
557 double fDefaultDouble;
scroggo@google.comb7dbf632013-04-23 15:38:09 +0000558 SkCommandLineFlags::StringArray* fStrings;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000559 // Both for the help string and in case fStrings is empty.
560 SkString fDefaultString;
561
562 // In order to keep a linked list.
563 SkFlagInfo* fNext;
564};
scroggo@google.comd9ba9a02013-03-21 19:43:15 +0000565#endif // SK_COMMAND_LINE_FLAGS_H