blob: a2d4c97ece0115159dc24d63ec404eb9bcf1860c [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
8#ifndef SK_FLAGS_H
9#define SK_FLAGS_H
10
11#include "SkString.h"
12#include "SkTDArray.h"
13
14/**
15 * Including this file (and compiling SkFlags.cpp) provides command line
16 * parsing. In order to use it, use the following macros in global
17 * namespace:
18 *
19 * DEFINE_bool(name, defaultValue, helpString);
20 * DEFINE_string(name, defaultValue, helpString);
21 * DEFINE_int32(name, defaultValue, helpString);
22 * DEFINE_double(name, defaultValue, helpString);
23 *
24 * Then, in main, call SkFlags::SetUsage() to describe usage and call
25 * SkFlags::ParseCommandLine() to parse the flags. Henceforth, each flag can
26 * be referenced using
27 *
28 * FLAGS_name
29 *
30 * For example, the line
31 *
32 * DEFINE_bool(boolean, false, "The variable boolean does such and such");
33 *
34 * will create the following variable:
35 *
36 * bool FLAGS_boolean;
37 *
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
40 * to false. (Single dashes are also permitted for this and other flags.) The
41 * helpString will be printed if the help flag (-h or -help) is used.
42 *
43 * Similarly, the line
44 *
45 * DEFINE_int32(integer, .., ..);
46 *
47 * will create
48 *
49 * int32_t FLAGS_integer;
50 *
51 * and
52 *
53 * DEFINE_double(real, .., ..);
54 *
55 * will create
56 *
57 * double FLAGS_real;
58 *
59 * These flags can be set by specifying, for example, "--integer 7" and
60 * "--real 3.14" on the command line.
61 *
62 * Unlike the others, the line
63 *
64 * DEFINE_string(args, .., ..);
65 *
66 * creates an array:
67 *
68 * SkTDArray<const char*> FLAGS_args;
69 *
70 * If the default value is the empty string, FLAGS_args will default to a size
71 * of zero. Otherwise it will default to a size of 1 with the default string
72 * as its value. All strings that follow the flag on the command line (until
73 * a string that begins with '-') will be entries in the array.
74 *
75 * Any flag can be referenced from another file after using the following:
76 *
77 * DECLARE_x(name);
78 *
79 * (where 'x' is the type specified in the DEFINE).
80 *
81 * Inspired by gflags (https://code.google.com/p/gflags/). Is not quite as
82 * robust as gflags, but suits our purposes. For example, allows creating
83 * a flag -h or -help which will never be used, since SkFlags handles it.
84 * SkFlags will also allow creating --flag and --noflag. Uses the same input
85 * format as gflags and creates similarly named variables (i.e. FLAGS_name).
86 * Strings are handled differently (resulting variable will be an array of
87 * strings) so that a flag can be followed by multiple parameters.
88 */
89
90
91class SkFlagInfo;
92
93class SkFlags {
94
95public:
96 /**
97 * Call to set the help message to be displayed. Should be called before
98 * ParseCommandLine.
99 */
100 static void SetUsage(const char* usage);
101
102 /**
103 * Call at the beginning of main to parse flags created by DEFINE_x, above.
104 * Must only be called once.
105 */
106 static void ParseCommandLine(int argc, char** argv);
107
108private:
109 static SkFlagInfo* gHead;
110 static SkString gUsage;
111
112 // For access to gHead.
113 friend class SkFlagInfo;
114};
115
116#define TO_STRING2(s) #s
117#define TO_STRING(s) TO_STRING2(s)
118
119#define DEFINE_bool(name, defaultValue, helpString) \
120bool FLAGS_##name; \
121static bool unused_##name = SkFlagInfo::CreateBoolFlag(TO_STRING(name), \
scroggo@google.com09fd4d22013-03-20 14:20:18 +0000122 NULL, \
123 &FLAGS_##name, \
124 defaultValue, \
125 helpString)
126
127// bool 2 allows specifying a short name. No check is done to ensure that shortName
128// is actually shorter than name.
129#define DEFINE_bool2(name, shortName, defaultValue, helpString) \
130bool FLAGS_##name; \
131static bool unused_##name = SkFlagInfo::CreateBoolFlag(TO_STRING(name), \
132 TO_STRING(shortName),\
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000133 &FLAGS_##name, \
134 defaultValue, \
135 helpString)
136
137#define DECLARE_bool(name) extern bool FLAGS_##name;
138
139#define DEFINE_string(name, defaultValue, helpString) \
140SkTDArray<const char*> FLAGS_##name; \
141static bool unused_##name = SkFlagInfo::CreateStringFlag(TO_STRING(name), \
scroggo@google.com09fd4d22013-03-20 14:20:18 +0000142 NULL, \
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000143 &FLAGS_##name, \
144 defaultValue, \
145 helpString)
146
scroggo@google.com09fd4d22013-03-20 14:20:18 +0000147// string2 allows specifying a short name. No check is done to ensure that shortName
148// is actually shorter than name.
149#define DEFINE_string2(name, shortName, defaultValue, helpString) \
150SkTDArray<const char*> FLAGS_##name; \
151static bool unused_##name = SkFlagInfo::CreateStringFlag(TO_STRING(name), \
152 TO_STRING(shortName), \
153 &FLAGS_##name, \
154 defaultValue, \
155 helpString)
156
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000157#define DECLARE_string(name) extern SkTDArray<const char*> FLAGS_##name;
158
159#define DEFINE_int32(name, defaultValue, helpString) \
160int32_t FLAGS_##name; \
161static bool unused_##name = SkFlagInfo::CreateIntFlag(TO_STRING(name), \
162 &FLAGS_##name, \
163 defaultValue, \
164 helpString)
165
166#define DECLARE_int32(name) extern int32_t FLAGS_##name;
167
168#define DEFINE_double(name, defaultValue, helpString) \
169double FLAGS_##name; \
170static bool unused_##name = SkFlagInfo::CreateDoubleFlag(TO_STRING(name), \
171 &FLAGS_##name, \
172 defaultValue, \
173 helpString)
174
175#define DECLARE_double(name) extern double FLAGS_##name;
176
177class SkFlagInfo {
178
179public:
180 enum FlagTypes {
181 kBool_FlagType,
182 kString_FlagType,
183 kInt_FlagType,
184 kDouble_FlagType,
185 };
186
187 // Create flags of the desired type, and append to the list.
scroggo@google.com09fd4d22013-03-20 14:20:18 +0000188 static bool CreateBoolFlag(const char* name, const char* shortName, bool* pBool,
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000189 bool defaultValue, const char* helpString) {
190 SkFlagInfo* info = SkNEW_ARGS(SkFlagInfo, (name, kBool_FlagType, helpString));
scroggo@google.com09fd4d22013-03-20 14:20:18 +0000191 info->fShortName.set(shortName);
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000192 info->fBoolValue = pBool;
193 *info->fBoolValue = info->fDefaultBool = defaultValue;
194 return true;
195 }
196
scroggo@google.com09fd4d22013-03-20 14:20:18 +0000197 static bool CreateStringFlag(const char* name, const char* shortName,
198 SkTDArray<const char*>* pStrings,
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000199 const char* defaultValue, const char* helpString) {
200 SkFlagInfo* info = SkNEW_ARGS(SkFlagInfo, (name, kString_FlagType, helpString));
scroggo@google.com09fd4d22013-03-20 14:20:18 +0000201 info->fShortName.set(shortName);
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000202 info->fDefaultString.set(defaultValue);
skia.committer@gmail.com075b0892013-03-05 07:09:08 +0000203
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000204 info->fStrings = pStrings;
205 info->fStrings->reset();
206 // If default is "", leave the array empty.
207 if (info->fDefaultString.size() > 0) {
208 info->fStrings->append(1, &defaultValue);
209 }
210 return true;
211 }
212
213 static bool CreateIntFlag(const char* name, int32_t* pInt,
214 int32_t defaultValue, const char* helpString) {
215 SkFlagInfo* info = SkNEW_ARGS(SkFlagInfo, (name, kInt_FlagType, helpString));
216 info->fIntValue = pInt;
217 *info->fIntValue = info->fDefaultInt = defaultValue;
218 return true;
219 }
220
221 static bool CreateDoubleFlag(const char* name, double* pDouble,
222 double defaultValue, const char* helpString) {
223 SkFlagInfo* info = SkNEW_ARGS(SkFlagInfo, (name, kDouble_FlagType, helpString));
224 info->fDoubleValue = pDouble;
225 *info->fDoubleValue = info->fDefaultDouble = defaultValue;
226 return true;
227 }
228
229 /**
230 * Returns true if the string matches this flag. For a bool, also sets the
231 * value, since a bool is specified as true or false by --name or --noname.
232 */
233 bool match(const char* string) {
scroggo@google.com09fd4d22013-03-20 14:20:18 +0000234 if (SkStrStartsWith(string, '-') && strlen(string) > 1) {
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000235 string++;
236 // Allow one or two dashes
scroggo@google.com09fd4d22013-03-20 14:20:18 +0000237 if (SkStrStartsWith(string, '-') && strlen(string) > 1) {
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000238 string++;
239 }
240 if (kBool_FlagType == fFlagType) {
241 // In this case, go ahead and set the value.
scroggo@google.com09fd4d22013-03-20 14:20:18 +0000242 if (fName.equals(string) || fShortName.equals(string)) {
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000243 *fBoolValue = true;
244 return true;
245 }
scroggo@google.com09fd4d22013-03-20 14:20:18 +0000246 if (SkStrStartsWith(string, "no") && strlen(string) > 2) {
247 string += 2;
248 if (fName.equals(string) || fShortName.equals(string)) {
249 *fBoolValue = false;
250 return true;
251 }
252 return false;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000253 }
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000254 }
scroggo@google.com09fd4d22013-03-20 14:20:18 +0000255 return fName.equals(string) || fShortName.equals(string);
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000256 } else {
257 // Has no dash
258 return false;
259 }
260 return false;
261 }
262
263 FlagTypes getFlagType() const { return fFlagType; }
264
265 void resetStrings() {
266 if (kString_FlagType == fFlagType) {
267 fStrings->reset();
268 } else {
269 SkASSERT(!"Can only call resetStrings on kString_FlagType");
270 }
271 }
272
273 void append(const char* string) {
274 if (kString_FlagType == fFlagType) {
275 fStrings->append(1, &string);
276 } else {
277 SkASSERT(!"Can only append to kString_FlagType");
278 }
279 }
280
281 void setInt(int32_t value) {
282 if (kInt_FlagType == fFlagType) {
283 *fIntValue = value;
284 } else {
285 SkASSERT(!"Can only call setInt on kInt_FlagType");
286 }
287 }
288
289 void setDouble(double value) {
290 if (kDouble_FlagType == fFlagType) {
291 *fDoubleValue = value;
292 } else {
293 SkASSERT(!"Can only call setDouble on kDouble_FlagType");
294 }
295 }
296
297 SkFlagInfo* next() { return fNext; }
298
299 const SkString& name() const { return fName; }
300
scroggo@google.com8366df02013-03-20 19:50:41 +0000301 const SkString& shortName() const { return fShortName; }
302
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000303 const SkString& help() const { return fHelpString; }
304
305 SkString defaultValue() const {
306 SkString result;
307 switch (fFlagType) {
308 case SkFlagInfo::kBool_FlagType:
309 result.printf("%s", fDefaultBool ? "true" : "false");
310 break;
311 case SkFlagInfo::kString_FlagType:
312 return fDefaultString;
313 case SkFlagInfo::kInt_FlagType:
314 result.printf("%i", fDefaultInt);
315 break;
316 case SkFlagInfo::kDouble_FlagType:
317 result.printf("%2.2f", fDefaultDouble);
318 break;
319 default:
320 SkASSERT(!"Invalid flag type");
321 }
322 return result;
323 }
324
325 SkString typeAsString() const {
326 switch (fFlagType) {
327 case SkFlagInfo::kBool_FlagType:
328 return SkString("bool");
329 case SkFlagInfo::kString_FlagType:
330 return SkString("string");
331 case SkFlagInfo::kInt_FlagType:
332 return SkString("int");
333 case SkFlagInfo::kDouble_FlagType:
334 return SkString("double");
335 default:
336 SkASSERT(!"Invalid flag type");
337 return SkString();
338 }
339 }
340
341private:
342 SkFlagInfo(const char* name, FlagTypes type, const char* helpString)
343 : fName(name)
344 , fFlagType(type)
345 , fHelpString(helpString)
346 , fBoolValue(NULL)
347 , fDefaultBool(false)
348 , fIntValue(NULL)
349 , fDefaultInt(0)
350 , fDoubleValue(NULL)
351 , fDefaultDouble(0)
352 , fStrings(NULL) {
353 fNext = SkFlags::gHead;
354 SkFlags::gHead = this;
355 }
356 // Name of the flag, without initial dashes
357 SkString fName;
scroggo@google.com09fd4d22013-03-20 14:20:18 +0000358 SkString fShortName;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000359 FlagTypes fFlagType;
360 SkString fHelpString;
361 bool* fBoolValue;
362 bool fDefaultBool;
363 int32_t* fIntValue;
364 int32_t fDefaultInt;
365 double* fDoubleValue;
366 double fDefaultDouble;
367 SkTDArray<const char*>* fStrings;
368 // Both for the help string and in case fStrings is empty.
369 SkString fDefaultString;
370
371 // In order to keep a linked list.
372 SkFlagInfo* fNext;
373};
374#endif // SK_FLAGS_H