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_COMMAND_LINE_FLAGS_H | 8 #ifndef SK_COMMAND_LINE_FLAGS_H |
9 #define SK_COMMAND_LINE_FLAGS_H | 9 #define SK_COMMAND_LINE_FLAGS_H |
10 | 10 |
11 #include "SkString.h" | 11 #include "SkString.h" |
12 #include "SkTDArray.h" | 12 #include "SkTArray.h" |
13 | 13 |
14 /** | 14 /** |
15 * Including this file (and compiling SkCommandLineFlags.cpp) provides command
line | 15 * Including this file (and compiling SkCommandLineFlags.cpp) provides command
line |
16 * parsing. In order to use it, use the following macros in global | 16 * parsing. In order to use it, use the following macros in global |
17 * namespace: | 17 * namespace: |
18 * | 18 * |
19 * DEFINE_bool(name, defaultValue, helpString); | 19 * DEFINE_bool(name, defaultValue, helpString); |
20 * DEFINE_string(name, defaultValue, helpString); | 20 * DEFINE_string(name, defaultValue, helpString); |
21 * DEFINE_int32(name, defaultValue, helpString); | 21 * DEFINE_int32(name, defaultValue, helpString); |
22 * DEFINE_double(name, defaultValue, helpString); | 22 * DEFINE_double(name, defaultValue, helpString); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
61 * | 61 * |
62 * These flags can be set by specifying, for example, "--integer 7" and | 62 * These flags can be set by specifying, for example, "--integer 7" and |
63 * "--real 3.14" on the command line. | 63 * "--real 3.14" on the command line. |
64 * | 64 * |
65 * Unlike the others, the line | 65 * Unlike the others, the line |
66 * | 66 * |
67 * DEFINE_string(args, .., ..); | 67 * DEFINE_string(args, .., ..); |
68 * | 68 * |
69 * creates an array: | 69 * creates an array: |
70 * | 70 * |
71 * SkTDArray<const char*> FLAGS_args; | 71 * SkCommandLineFlags::StringArray FLAGS_args; |
72 * | 72 * |
73 * If the default value is the empty string, FLAGS_args will default to a size | 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 | 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 | 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. | 76 * a string that begins with '-') will be entries in the array. |
77 * | 77 * |
78 * Any flag can be referenced from another file after using the following: | 78 * Any flag can be referenced from another file after using the following: |
79 * | 79 * |
80 * DECLARE_x(name); | 80 * DECLARE_x(name); |
81 * | 81 * |
(...skipping 19 matching lines...) Expand all Loading... |
101 * Parse. | 101 * Parse. |
102 */ | 102 */ |
103 static void SetUsage(const char* usage); | 103 static void SetUsage(const char* usage); |
104 | 104 |
105 /** | 105 /** |
106 * Call at the beginning of main to parse flags created by DEFINE_x, above. | 106 * Call at the beginning of main to parse flags created by DEFINE_x, above. |
107 * Must only be called once. | 107 * Must only be called once. |
108 */ | 108 */ |
109 static void Parse(int argc, char** argv); | 109 static void Parse(int argc, char** argv); |
110 | 110 |
| 111 /** |
| 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 |
111 private: | 140 private: |
112 static SkFlagInfo* gHead; | 141 static SkFlagInfo* gHead; |
113 static SkString gUsage; | 142 static SkString gUsage; |
114 | 143 |
115 // For access to gHead. | 144 // For access to gHead. |
116 friend class SkFlagInfo; | 145 friend class SkFlagInfo; |
117 }; | 146 }; |
118 | 147 |
119 #define TO_STRING2(s) #s | 148 #define TO_STRING2(s) #s |
120 #define TO_STRING(s) TO_STRING2(s) | 149 #define TO_STRING(s) TO_STRING2(s) |
(...skipping 12 matching lines...) Expand all Loading... |
133 bool FLAGS_##name; \ | 162 bool FLAGS_##name; \ |
134 static bool unused_##name = SkFlagInfo::CreateBoolFlag(TO_STRING(name), \ | 163 static bool unused_##name = SkFlagInfo::CreateBoolFlag(TO_STRING(name), \ |
135 TO_STRING(shortName),\ | 164 TO_STRING(shortName),\ |
136 &FLAGS_##name, \ | 165 &FLAGS_##name, \ |
137 defaultValue, \ | 166 defaultValue, \ |
138 helpString) | 167 helpString) |
139 | 168 |
140 #define DECLARE_bool(name) extern bool FLAGS_##name; | 169 #define DECLARE_bool(name) extern bool FLAGS_##name; |
141 | 170 |
142 #define DEFINE_string(name, defaultValue, helpString) \ | 171 #define DEFINE_string(name, defaultValue, helpString) \ |
143 SkTDArray<const char*> FLAGS_##name; \ | 172 SkCommandLineFlags::StringArray FLAGS_##name; \ |
144 static bool unused_##name = SkFlagInfo::CreateStringFlag(TO_STRING(name), \ | 173 static bool unused_##name = SkFlagInfo::CreateStringFlag(TO_STRING(name), \ |
145 NULL, \ | 174 NULL, \ |
146 &FLAGS_##name, \ | 175 &FLAGS_##name, \ |
147 defaultValue, \ | 176 defaultValue, \ |
148 helpString) | 177 helpString) |
149 | 178 |
150 // string2 allows specifying a short name. There is an assert that shortName | 179 // string2 allows specifying a short name. There is an assert that shortName |
151 // is only 1 character. | 180 // is only 1 character. |
152 #define DEFINE_string2(name, shortName, defaultValue, helpString)
\ | 181 #define DEFINE_string2(name, shortName, defaultValue, helpString)
\ |
153 SkTDArray<const char*> FLAGS_##name;
\ | 182 SkCommandLineFlags::StringArray FLAGS_##name;
\ |
154 static bool unused_##name = SkFlagInfo::CreateStringFlag(TO_STRING(name),
\ | 183 static bool unused_##name = SkFlagInfo::CreateStringFlag(TO_STRING(name),
\ |
155 TO_STRING(shortName),
\ | 184 TO_STRING(shortName),
\ |
156 &FLAGS_##name,
\ | 185 &FLAGS_##name,
\ |
157 defaultValue,
\ | 186 defaultValue,
\ |
158 helpString) | 187 helpString) |
159 | 188 |
160 #define DECLARE_string(name) extern SkTDArray<const char*> FLAGS_##name; | 189 #define DECLARE_string(name) extern SkCommandLineFlags::StringArray FLAGS_##name
; |
161 | 190 |
162 #define DEFINE_int32(name, defaultValue, helpString) \ | 191 #define DEFINE_int32(name, defaultValue, helpString) \ |
163 int32_t FLAGS_##name; \ | 192 int32_t FLAGS_##name; \ |
164 static bool unused_##name = SkFlagInfo::CreateIntFlag(TO_STRING(name), \ | 193 static bool unused_##name = SkFlagInfo::CreateIntFlag(TO_STRING(name), \ |
165 &FLAGS_##name, \ | 194 &FLAGS_##name, \ |
166 defaultValue, \ | 195 defaultValue, \ |
167 helpString) | 196 helpString) |
168 | 197 |
169 #define DECLARE_int32(name) extern int32_t FLAGS_##name; | 198 #define DECLARE_int32(name) extern int32_t FLAGS_##name; |
170 | 199 |
(...skipping 19 matching lines...) Expand all Loading... |
190 // Create flags of the desired type, and append to the list. | 219 // Create flags of the desired type, and append to the list. |
191 static bool CreateBoolFlag(const char* name, const char* shortName, bool* pB
ool, | 220 static bool CreateBoolFlag(const char* name, const char* shortName, bool* pB
ool, |
192 bool defaultValue, const char* helpString) { | 221 bool defaultValue, const char* helpString) { |
193 SkFlagInfo* info = SkNEW_ARGS(SkFlagInfo, (name, shortName, kBool_FlagTy
pe, helpString)); | 222 SkFlagInfo* info = SkNEW_ARGS(SkFlagInfo, (name, shortName, kBool_FlagTy
pe, helpString)); |
194 info->fBoolValue = pBool; | 223 info->fBoolValue = pBool; |
195 *info->fBoolValue = info->fDefaultBool = defaultValue; | 224 *info->fBoolValue = info->fDefaultBool = defaultValue; |
196 return true; | 225 return true; |
197 } | 226 } |
198 | 227 |
199 static bool CreateStringFlag(const char* name, const char* shortName, | 228 static bool CreateStringFlag(const char* name, const char* shortName, |
200 SkTDArray<const char*>* pStrings, | 229 SkCommandLineFlags::StringArray* pStrings, |
201 const char* defaultValue, const char* helpStrin
g) { | 230 const char* defaultValue, const char* helpStrin
g) { |
202 SkFlagInfo* info = SkNEW_ARGS(SkFlagInfo, (name, shortName, kString_Flag
Type, helpString)); | 231 SkFlagInfo* info = SkNEW_ARGS(SkFlagInfo, (name, shortName, kString_Flag
Type, helpString)); |
203 info->fDefaultString.set(defaultValue); | 232 info->fDefaultString.set(defaultValue); |
204 | 233 |
205 info->fStrings = pStrings; | 234 info->fStrings = pStrings; |
206 info->fStrings->reset(); | 235 info->fStrings->reset(); |
207 // If default is "", leave the array empty. | 236 // If default is "", leave the array empty. |
208 if (info->fDefaultString.size() > 0) { | 237 if (info->fDefaultString.size() > 0) { |
209 info->fStrings->append(1, &defaultValue); | 238 info->fStrings->append(defaultValue); |
210 } | 239 } |
211 return true; | 240 return true; |
212 } | 241 } |
213 | 242 |
214 static bool CreateIntFlag(const char* name, int32_t* pInt, | 243 static bool CreateIntFlag(const char* name, int32_t* pInt, |
215 int32_t defaultValue, const char* helpString) { | 244 int32_t defaultValue, const char* helpString) { |
216 SkFlagInfo* info = SkNEW_ARGS(SkFlagInfo, (name, NULL, kInt_FlagType, he
lpString)); | 245 SkFlagInfo* info = SkNEW_ARGS(SkFlagInfo, (name, NULL, kInt_FlagType, he
lpString)); |
217 info->fIntValue = pInt; | 246 info->fIntValue = pInt; |
218 *info->fIntValue = info->fDefaultInt = defaultValue; | 247 *info->fIntValue = info->fDefaultInt = defaultValue; |
219 return true; | 248 return true; |
(...skipping 27 matching lines...) Expand all Loading... |
247 void resetStrings() { | 276 void resetStrings() { |
248 if (kString_FlagType == fFlagType) { | 277 if (kString_FlagType == fFlagType) { |
249 fStrings->reset(); | 278 fStrings->reset(); |
250 } else { | 279 } else { |
251 SkASSERT(!"Can only call resetStrings on kString_FlagType"); | 280 SkASSERT(!"Can only call resetStrings on kString_FlagType"); |
252 } | 281 } |
253 } | 282 } |
254 | 283 |
255 void append(const char* string) { | 284 void append(const char* string) { |
256 if (kString_FlagType == fFlagType) { | 285 if (kString_FlagType == fFlagType) { |
257 fStrings->append(1, &string); | 286 fStrings->append(string); |
258 } else { | 287 } else { |
259 SkASSERT(!"Can only append to kString_FlagType"); | 288 SkASSERT(!"Can only append to kString_FlagType"); |
260 } | 289 } |
261 } | 290 } |
262 | 291 |
263 void setInt(int32_t value) { | 292 void setInt(int32_t value) { |
264 if (kInt_FlagType == fFlagType) { | 293 if (kInt_FlagType == fFlagType) { |
265 *fIntValue = value; | 294 *fIntValue = value; |
266 } else { | 295 } else { |
267 SkASSERT(!"Can only call setInt on kInt_FlagType"); | 296 SkASSERT(!"Can only call setInt on kInt_FlagType"); |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
350 SkString fName; | 379 SkString fName; |
351 SkString fShortName; | 380 SkString fShortName; |
352 FlagTypes fFlagType; | 381 FlagTypes fFlagType; |
353 SkString fHelpString; | 382 SkString fHelpString; |
354 bool* fBoolValue; | 383 bool* fBoolValue; |
355 bool fDefaultBool; | 384 bool fDefaultBool; |
356 int32_t* fIntValue; | 385 int32_t* fIntValue; |
357 int32_t fDefaultInt; | 386 int32_t fDefaultInt; |
358 double* fDoubleValue; | 387 double* fDoubleValue; |
359 double fDefaultDouble; | 388 double fDefaultDouble; |
360 SkTDArray<const char*>* fStrings; | 389 SkCommandLineFlags::StringArray* fStrings; |
361 // Both for the help string and in case fStrings is empty. | 390 // Both for the help string and in case fStrings is empty. |
362 SkString fDefaultString; | 391 SkString fDefaultString; |
363 | 392 |
364 // In order to keep a linked list. | 393 // In order to keep a linked list. |
365 SkFlagInfo* fNext; | 394 SkFlagInfo* fNext; |
366 }; | 395 }; |
367 #endif // SK_COMMAND_LINE_FLAGS_H | 396 #endif // SK_COMMAND_LINE_FLAGS_H |
OLD | NEW |