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 #include "SkCommandLineFlags.h" | 8 #include "SkCommandLineFlags.h" |
9 #include "SkTDArray.h" | 9 #include "SkTDArray.h" |
10 #include "SkTSort.h" | |
10 | 11 |
11 DEFINE_bool(undefok, false, "Silently ignore unknown flags instead of crashing." ); | 12 DEFINE_bool(undefok, false, "Silently ignore unknown flags instead of crashing." ); |
12 | 13 |
13 bool SkFlagInfo::CreateStringFlag(const char* name, const char* shortName, | 14 bool SkFlagInfo::CreateStringFlag(const char* name, const char* shortName, |
14 SkCommandLineFlags::StringArray* pStrings, | 15 SkCommandLineFlags::StringArray* pStrings, |
15 const char* defaultValue, const char* helpStri ng) { | 16 const char* defaultValue, const char* helpStri ng) { |
16 SkFlagInfo* info = SkNEW_ARGS(SkFlagInfo, (name, shortName, kString_FlagType , helpString)); | 17 SkFlagInfo* info = SkNEW_ARGS(SkFlagInfo, (name, shortName, kString_FlagType , helpString)); |
17 info->fDefaultString.set(defaultValue); | 18 info->fDefaultString.set(defaultValue); |
18 | 19 |
19 info->fStrings = pStrings; | 20 info->fStrings = pStrings; |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
184 } else { | 185 } else { |
185 // the line break is within the limit. Break there. | 186 // the line break is within the limit. Break there. |
186 lineBreak++; | 187 lineBreak++; |
187 SkDebugf("\t\t%.*s", lineBreak, currLine); | 188 SkDebugf("\t\t%.*s", lineBreak, currLine); |
188 currLine += lineBreak; | 189 currLine += lineBreak; |
189 } | 190 } |
190 } | 191 } |
191 SkDebugf("\n"); | 192 SkDebugf("\n"); |
192 } | 193 } |
193 | 194 |
195 namespace { | |
196 struct CompareFlagsByName { | |
197 bool operator()(SkFlagInfo* a, SkFlagInfo* b) const { | |
198 return strcmp(a->name().c_str(), b->name().c_str()) < 0; | |
199 } | |
200 }; | |
201 } // namespace | |
202 | |
194 void SkCommandLineFlags::Parse(int argc, char** argv) { | 203 void SkCommandLineFlags::Parse(int argc, char** argv) { |
195 // Only allow calling this function once. | 204 // Only allow calling this function once. |
196 static bool gOnce; | 205 static bool gOnce; |
197 if (gOnce) { | 206 if (gOnce) { |
198 SkDebugf("Parse should only be called once at the beginning of main!\n") ; | 207 SkDebugf("Parse should only be called once at the beginning of main!\n") ; |
199 SkASSERT(false); | 208 SkASSERT(false); |
200 return; | 209 return; |
201 } | 210 } |
202 gOnce = true; | 211 gOnce = true; |
203 | 212 |
204 bool helpPrinted = false; | 213 bool helpPrinted = false; |
205 // Loop over argv, starting with 1, since the first is just the name of the program. | 214 // Loop over argv, starting with 1, since the first is just the name of the program. |
206 for (int i = 1; i < argc; i++) { | 215 for (int i = 1; i < argc; i++) { |
207 if (0 == strcmp("-h", argv[i]) || 0 == strcmp("--help", argv[i])) { | 216 if (0 == strcmp("-h", argv[i]) || 0 == strcmp("--help", argv[i])) { |
208 // Print help message. | 217 // Print help message. |
209 SkTDArray<const char*> helpFlags; | 218 SkTDArray<const char*> helpFlags; |
210 for (int j = i + 1; j < argc; j++) { | 219 for (int j = i + 1; j < argc; j++) { |
211 if (SkStrStartsWith(argv[j], '-')) { | 220 if (SkStrStartsWith(argv[j], '-')) { |
212 break; | 221 break; |
213 } | 222 } |
214 helpFlags.append(1, &argv[j]); | 223 helpFlags.append(1, &argv[j]); |
215 } | 224 } |
216 if (0 == helpFlags.count()) { | 225 if (0 == helpFlags.count()) { |
217 // Only print general help message if help for specific flags is not requested. | 226 // Only print general help message if help for specific flags is not requested. |
218 SkDebugf("%s\n%s\n", argv[0], gUsage.c_str()); | 227 SkDebugf("%s\n%s\n", argv[0], gUsage.c_str()); |
219 } | 228 } |
220 SkDebugf("Flags:\n"); | 229 SkDebugf("Flags:\n"); |
221 SkFlagInfo* flag = SkCommandLineFlags::gHead; | 230 |
222 while (flag != NULL) { | 231 if (0 == helpFlags.count()) { |
223 // If no flags followed --help, print them all | 232 // If no flags followed --help, print them all |
224 bool printFlag = 0 == helpFlags.count(); | 233 SkTDArray<SkFlagInfo*> allFlags; |
225 if (!printFlag) { | 234 for (SkFlagInfo* flag = SkCommandLineFlags::gHead; flag; |
235 flag = flag->next()) { | |
236 allFlags.push(flag); | |
237 } | |
238 CompareFlagsByName c; | |
239 SkTQSort(&allFlags[0], &allFlags[allFlags.count() - 1], c); | |
mtklein
2015/01/18 15:53:10
SkTQSort(&allFlags[0], &allFlags[allFlags.count()-
| |
240 for (int i = 0; i < allFlags.count(); ++i) { | |
241 print_help_for_flag(allFlags[i]); | |
242 } | |
243 } else { | |
244 SkFlagInfo* flag = SkCommandLineFlags::gHead; | |
245 while (flag != NULL) { | |
mtklein
2015/01/18 15:53:10
For consistency, switch this to the for-loop above
| |
226 for (int k = 0; k < helpFlags.count(); k++) { | 246 for (int k = 0; k < helpFlags.count(); k++) { |
227 if (flag->name().equals(helpFlags[k]) || | 247 if (flag->name().equals(helpFlags[k]) || |
228 flag->shortName().equals(helpFlags[k])) { | 248 flag->shortName().equals(helpFlags[k])) { |
229 printFlag = true; | 249 print_help_for_flag(flag); |
230 helpFlags.remove(k); | 250 helpFlags.remove(k); |
231 break; | 251 break; |
232 } | 252 } |
233 } | 253 } |
254 flag = flag->next(); | |
234 } | 255 } |
235 if (printFlag) { | |
236 print_help_for_flag(flag); | |
237 } | |
238 flag = flag->next(); | |
239 } | 256 } |
240 if (helpFlags.count() > 0) { | 257 if (helpFlags.count() > 0) { |
241 SkDebugf("Requested help for unrecognized flags:\n"); | 258 SkDebugf("Requested help for unrecognized flags:\n"); |
242 for (int k = 0; k < helpFlags.count(); k++) { | 259 for (int k = 0; k < helpFlags.count(); k++) { |
243 SkDebugf("\t--%s\n", helpFlags[k]); | 260 SkDebugf("\t--%s\n", helpFlags[k]); |
244 } | 261 } |
245 } | 262 } |
246 helpPrinted = true; | 263 helpPrinted = true; |
247 } | 264 } |
248 if (!helpPrinted) { | 265 if (!helpPrinted) { |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
343 } | 360 } |
344 | 361 |
345 } // namespace | 362 } // namespace |
346 | 363 |
347 bool SkCommandLineFlags::ShouldSkip(const SkTDArray<const char*>& strings, const char* name) { | 364 bool SkCommandLineFlags::ShouldSkip(const SkTDArray<const char*>& strings, const char* name) { |
348 return ShouldSkipImpl(strings, name); | 365 return ShouldSkipImpl(strings, name); |
349 } | 366 } |
350 bool SkCommandLineFlags::ShouldSkip(const StringArray& strings, const char* name ) { | 367 bool SkCommandLineFlags::ShouldSkip(const StringArray& strings, const char* name ) { |
351 return ShouldSkipImpl(strings, name); | 368 return ShouldSkipImpl(strings, name); |
352 } | 369 } |
OLD | NEW |