blob: 08fd3a2ba5974447489db7f5e18712bbde71e7cc [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
11#include "SkString.h"
scroggo@google.comb7dbf632013-04-23 15:38:09 +000012#include "SkTArray.h"
scroggo@google.com161e1ba2013-03-04 16:41:06 +000013
14/**
scroggo@google.comd9ba9a02013-03-21 19:43:15 +000015 * Including this file (and compiling SkCommandLineFlags.cpp) provides command line
scroggo@google.com161e1ba2013-03-04 16:41:06 +000016 * 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 *
scroggo@google.comd9ba9a02013-03-21 19:43:15 +000024 * Then, in main, call SkCommandLineFlags::SetUsage() to describe usage and call
25 * SkCommandLineFlags::Parse() to parse the flags. Henceforth, each flag can
scroggo@google.com161e1ba2013-03-04 16:41:06 +000026 * 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
scroggo@google.com5dc4ca12013-03-21 13:10:59 +000040 * to false. FLAGS_boolean can also be set using "--boolean=true" or
41 * "--boolean true" (where "true" can be replaced by "false", "TRUE", "FALSE",
42 * "1" or "0").
43 *
scroggo@google.com5dc4ca12013-03-21 13:10:59 +000044 * The helpString will be printed if the help flag (-h or -help) is used.
scroggo@google.com161e1ba2013-03-04 16:41:06 +000045 *
46 * Similarly, the line
47 *
48 * DEFINE_int32(integer, .., ..);
49 *
50 * will create
51 *
52 * int32_t FLAGS_integer;
53 *
54 * and
55 *
56 * DEFINE_double(real, .., ..);
57 *
58 * will create
59 *
60 * double FLAGS_real;
61 *
62 * These flags can be set by specifying, for example, "--integer 7" and
63 * "--real 3.14" on the command line.
64 *
65 * Unlike the others, the line
66 *
67 * DEFINE_string(args, .., ..);
68 *
69 * creates an array:
70 *
scroggo@google.comb7dbf632013-04-23 15:38:09 +000071 * SkCommandLineFlags::StringArray FLAGS_args;
scroggo@google.com161e1ba2013-03-04 16:41:06 +000072 *
73 * If the default value is the empty string, FLAGS_args will default to a size
74 * of zero. Otherwise it will default to a size of 1 with the default string
75 * as its value. All strings that follow the flag on the command line (until
76 * a string that begins with '-') will be entries in the array.
77 *
78 * Any flag can be referenced from another file after using the following:
79 *
80 * DECLARE_x(name);
81 *
82 * (where 'x' is the type specified in the DEFINE).
83 *
84 * Inspired by gflags (https://code.google.com/p/gflags/). Is not quite as
85 * robust as gflags, but suits our purposes. For example, allows creating
scroggo@google.comd9ba9a02013-03-21 19:43:15 +000086 * a flag -h or -help which will never be used, since SkCommandLineFlags handles it.
87 * SkCommandLineFlags will also allow creating --flag and --noflag. Uses the same input
scroggo@google.com161e1ba2013-03-04 16:41:06 +000088 * format as gflags and creates similarly named variables (i.e. FLAGS_name).
89 * Strings are handled differently (resulting variable will be an array of
90 * strings) so that a flag can be followed by multiple parameters.
91 */
92
93
94class SkFlagInfo;
95
scroggo@google.comd9ba9a02013-03-21 19:43:15 +000096class SkCommandLineFlags {
scroggo@google.com161e1ba2013-03-04 16:41:06 +000097
98public:
99 /**
100 * Call to set the help message to be displayed. Should be called before
scroggo@google.comd9ba9a02013-03-21 19:43:15 +0000101 * Parse.
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000102 */
103 static void SetUsage(const char* usage);
104
105 /**
106 * Call at the beginning of main to parse flags created by DEFINE_x, above.
107 * Must only be called once.
108 */
scroggo@google.comd9ba9a02013-03-21 19:43:15 +0000109 static void Parse(int argc, char** argv);
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000110
scroggo@google.comb7dbf632013-04-23 15:38:09 +0000111 /**
112 * Custom class for holding the arguments for a string flag.
113 * Publicly only has accessors so the strings cannot be modified.
114 */
115 class StringArray {
116 public:
117 const char* operator[](int i) const {
118 SkASSERT(i >= 0 && i < fStrings.count());
119 return fStrings[i].c_str();
120 }
121
122 int count() const {
123 return fStrings.count();
124 }
125
126 bool isEmpty() const { return this->count() == 0; }
127
128 private:
129 void reset() { fStrings.reset(); }
130
131 void append(const char* string) {
132 fStrings.push_back().set(string);
133 }
134
135 SkTArray<SkString> fStrings;
136
137 friend class SkFlagInfo;
138 };
139
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000140private:
141 static SkFlagInfo* gHead;
142 static SkString gUsage;
143
144 // For access to gHead.
145 friend class SkFlagInfo;
146};
147
148#define TO_STRING2(s) #s
149#define TO_STRING(s) TO_STRING2(s)
150
151#define DEFINE_bool(name, defaultValue, helpString) \
152bool FLAGS_##name; \
153static bool unused_##name = SkFlagInfo::CreateBoolFlag(TO_STRING(name), \
scroggo@google.com09fd4d22013-03-20 14:20:18 +0000154 NULL, \
155 &FLAGS_##name, \
156 defaultValue, \
157 helpString)
158
159// bool 2 allows specifying a short name. No check is done to ensure that shortName
160// is actually shorter than name.
161#define DEFINE_bool2(name, shortName, defaultValue, helpString) \
162bool FLAGS_##name; \
163static bool unused_##name = SkFlagInfo::CreateBoolFlag(TO_STRING(name), \
164 TO_STRING(shortName),\
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000165 &FLAGS_##name, \
166 defaultValue, \
167 helpString)
168
169#define DECLARE_bool(name) extern bool FLAGS_##name;
170
171#define DEFINE_string(name, defaultValue, helpString) \
scroggo@google.comb7dbf632013-04-23 15:38:09 +0000172SkCommandLineFlags::StringArray FLAGS_##name; \
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000173static bool unused_##name = SkFlagInfo::CreateStringFlag(TO_STRING(name), \
scroggo@google.com09fd4d22013-03-20 14:20:18 +0000174 NULL, \
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000175 &FLAGS_##name, \
176 defaultValue, \
177 helpString)
178
scroggo@google.com604e0c22013-04-09 21:25:46 +0000179// string2 allows specifying a short name. There is an assert that shortName
180// is only 1 character.
scroggo@google.com09fd4d22013-03-20 14:20:18 +0000181#define DEFINE_string2(name, shortName, defaultValue, helpString) \
scroggo@google.comb7dbf632013-04-23 15:38:09 +0000182SkCommandLineFlags::StringArray FLAGS_##name; \
scroggo@google.com09fd4d22013-03-20 14:20:18 +0000183static bool unused_##name = SkFlagInfo::CreateStringFlag(TO_STRING(name), \
184 TO_STRING(shortName), \
185 &FLAGS_##name, \
186 defaultValue, \
187 helpString)
188
scroggo@google.comb7dbf632013-04-23 15:38:09 +0000189#define DECLARE_string(name) extern SkCommandLineFlags::StringArray FLAGS_##name;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000190
191#define DEFINE_int32(name, defaultValue, helpString) \
192int32_t FLAGS_##name; \
193static bool unused_##name = SkFlagInfo::CreateIntFlag(TO_STRING(name), \
194 &FLAGS_##name, \
195 defaultValue, \
196 helpString)
197
198#define DECLARE_int32(name) extern int32_t FLAGS_##name;
199
200#define DEFINE_double(name, defaultValue, helpString) \
201double FLAGS_##name; \
202static bool unused_##name = SkFlagInfo::CreateDoubleFlag(TO_STRING(name), \
203 &FLAGS_##name, \
204 defaultValue, \
205 helpString)
206
207#define DECLARE_double(name) extern double FLAGS_##name;
208
209class SkFlagInfo {
210
211public:
212 enum FlagTypes {
213 kBool_FlagType,
214 kString_FlagType,
215 kInt_FlagType,
216 kDouble_FlagType,
217 };
218
219 // Create flags of the desired type, and append to the list.
scroggo@google.com09fd4d22013-03-20 14:20:18 +0000220 static bool CreateBoolFlag(const char* name, const char* shortName, bool* pBool,
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000221 bool defaultValue, const char* helpString) {
scroggo@google.com604e0c22013-04-09 21:25:46 +0000222 SkFlagInfo* info = SkNEW_ARGS(SkFlagInfo, (name, shortName, kBool_FlagType, helpString));
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000223 info->fBoolValue = pBool;
224 *info->fBoolValue = info->fDefaultBool = defaultValue;
225 return true;
226 }
227
scroggo@google.com09fd4d22013-03-20 14:20:18 +0000228 static bool CreateStringFlag(const char* name, const char* shortName,
scroggo@google.comb7dbf632013-04-23 15:38:09 +0000229 SkCommandLineFlags::StringArray* pStrings,
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000230 const char* defaultValue, const char* helpString) {
scroggo@google.com604e0c22013-04-09 21:25:46 +0000231 SkFlagInfo* info = SkNEW_ARGS(SkFlagInfo, (name, shortName, kString_FlagType, helpString));
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000232 info->fDefaultString.set(defaultValue);
skia.committer@gmail.com075b0892013-03-05 07:09:08 +0000233
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000234 info->fStrings = pStrings;
235 info->fStrings->reset();
236 // If default is "", leave the array empty.
237 if (info->fDefaultString.size() > 0) {
scroggo@google.comb7dbf632013-04-23 15:38:09 +0000238 info->fStrings->append(defaultValue);
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000239 }
240 return true;
241 }
242
243 static bool CreateIntFlag(const char* name, int32_t* pInt,
244 int32_t defaultValue, const char* helpString) {
scroggo@google.com604e0c22013-04-09 21:25:46 +0000245 SkFlagInfo* info = SkNEW_ARGS(SkFlagInfo, (name, NULL, kInt_FlagType, helpString));
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000246 info->fIntValue = pInt;
247 *info->fIntValue = info->fDefaultInt = defaultValue;
248 return true;
249 }
250
251 static bool CreateDoubleFlag(const char* name, double* pDouble,
252 double defaultValue, const char* helpString) {
scroggo@google.com604e0c22013-04-09 21:25:46 +0000253 SkFlagInfo* info = SkNEW_ARGS(SkFlagInfo, (name, NULL, kDouble_FlagType, helpString));
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000254 info->fDoubleValue = pDouble;
255 *info->fDoubleValue = info->fDefaultDouble = defaultValue;
256 return true;
257 }
258
259 /**
scroggo@google.com5dc4ca12013-03-21 13:10:59 +0000260 * Returns true if the string matches this flag.
261 * For a boolean flag, also sets the value, since a boolean flag can be set in a number of ways
262 * without looking at the following string:
263 * --name
264 * --noname
265 * --name=true
266 * --name=false
267 * --name=1
268 * --name=0
269 * --name=TRUE
270 * --name=FALSE
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000271 */
scroggo@google.com5dc4ca12013-03-21 13:10:59 +0000272 bool match(const char* string);
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000273
274 FlagTypes getFlagType() const { return fFlagType; }
275
276 void resetStrings() {
277 if (kString_FlagType == fFlagType) {
278 fStrings->reset();
279 } else {
280 SkASSERT(!"Can only call resetStrings on kString_FlagType");
281 }
282 }
283
284 void append(const char* string) {
285 if (kString_FlagType == fFlagType) {
scroggo@google.comb7dbf632013-04-23 15:38:09 +0000286 fStrings->append(string);
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000287 } else {
288 SkASSERT(!"Can only append to kString_FlagType");
289 }
290 }
291
292 void setInt(int32_t value) {
293 if (kInt_FlagType == fFlagType) {
294 *fIntValue = value;
295 } else {
296 SkASSERT(!"Can only call setInt on kInt_FlagType");
297 }
298 }
299
300 void setDouble(double value) {
301 if (kDouble_FlagType == fFlagType) {
302 *fDoubleValue = value;
303 } else {
304 SkASSERT(!"Can only call setDouble on kDouble_FlagType");
305 }
306 }
307
scroggo@google.com5dc4ca12013-03-21 13:10:59 +0000308 void setBool(bool value) {
309 if (kBool_FlagType == fFlagType) {
310 *fBoolValue = value;
311 } else {
312 SkASSERT(!"Can only call setBool on kBool_FlagType");
313 }
314 }
315
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000316 SkFlagInfo* next() { return fNext; }
317
318 const SkString& name() const { return fName; }
319
scroggo@google.com8366df02013-03-20 19:50:41 +0000320 const SkString& shortName() const { return fShortName; }
321
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000322 const SkString& help() const { return fHelpString; }
323
324 SkString defaultValue() const {
325 SkString result;
326 switch (fFlagType) {
327 case SkFlagInfo::kBool_FlagType:
328 result.printf("%s", fDefaultBool ? "true" : "false");
329 break;
330 case SkFlagInfo::kString_FlagType:
331 return fDefaultString;
332 case SkFlagInfo::kInt_FlagType:
333 result.printf("%i", fDefaultInt);
334 break;
335 case SkFlagInfo::kDouble_FlagType:
336 result.printf("%2.2f", fDefaultDouble);
337 break;
338 default:
339 SkASSERT(!"Invalid flag type");
340 }
341 return result;
342 }
343
344 SkString typeAsString() const {
345 switch (fFlagType) {
346 case SkFlagInfo::kBool_FlagType:
347 return SkString("bool");
348 case SkFlagInfo::kString_FlagType:
349 return SkString("string");
350 case SkFlagInfo::kInt_FlagType:
351 return SkString("int");
352 case SkFlagInfo::kDouble_FlagType:
353 return SkString("double");
354 default:
355 SkASSERT(!"Invalid flag type");
356 return SkString();
357 }
358 }
359
360private:
scroggo@google.com604e0c22013-04-09 21:25:46 +0000361 SkFlagInfo(const char* name, const char* shortName, FlagTypes type, const char* helpString)
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000362 : fName(name)
scroggo@google.com604e0c22013-04-09 21:25:46 +0000363 , fShortName(shortName)
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000364 , fFlagType(type)
365 , fHelpString(helpString)
366 , fBoolValue(NULL)
367 , fDefaultBool(false)
368 , fIntValue(NULL)
369 , fDefaultInt(0)
370 , fDoubleValue(NULL)
371 , fDefaultDouble(0)
372 , fStrings(NULL) {
scroggo@google.comd9ba9a02013-03-21 19:43:15 +0000373 fNext = SkCommandLineFlags::gHead;
374 SkCommandLineFlags::gHead = this;
scroggo@google.com604e0c22013-04-09 21:25:46 +0000375 SkASSERT(NULL != name && strlen(name) > 1);
376 SkASSERT(NULL == shortName || 1 == strlen(shortName));
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000377 }
378 // Name of the flag, without initial dashes
379 SkString fName;
scroggo@google.com09fd4d22013-03-20 14:20:18 +0000380 SkString fShortName;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000381 FlagTypes fFlagType;
382 SkString fHelpString;
383 bool* fBoolValue;
384 bool fDefaultBool;
385 int32_t* fIntValue;
386 int32_t fDefaultInt;
387 double* fDoubleValue;
388 double fDefaultDouble;
scroggo@google.comb7dbf632013-04-23 15:38:09 +0000389 SkCommandLineFlags::StringArray* fStrings;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000390 // Both for the help string and in case fStrings is empty.
391 SkString fDefaultString;
392
393 // In order to keep a linked list.
394 SkFlagInfo* fNext;
395};
scroggo@google.comd9ba9a02013-03-21 19:43:15 +0000396#endif // SK_COMMAND_LINE_FLAGS_H