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 | 9 |
10 static bool string_is_in(const char* target, const char* set[], size_t len) { | 10 static bool string_is_in(const char* target, const char* set[], size_t len) { |
(...skipping 23 matching lines...) Expand all Loading... |
34 *result = false; | 34 *result = false; |
35 return true; | 35 return true; |
36 } | 36 } |
37 SkDebugf("Parameter \"%s\" not supported.\n", string); | 37 SkDebugf("Parameter \"%s\" not supported.\n", string); |
38 return false; | 38 return false; |
39 } | 39 } |
40 | 40 |
41 bool SkFlagInfo::match(const char* string) { | 41 bool SkFlagInfo::match(const char* string) { |
42 if (SkStrStartsWith(string, '-') && strlen(string) > 1) { | 42 if (SkStrStartsWith(string, '-') && strlen(string) > 1) { |
43 string++; | 43 string++; |
44 // Allow one or two dashes | 44 const SkString* compareName; |
45 if (SkStrStartsWith(string, '-') && strlen(string) > 1) { | 45 if (SkStrStartsWith(string, '-') && strlen(string) > 1) { |
46 string++; | 46 string++; |
| 47 // There were two dashes. Compare against full name. |
| 48 compareName = &fName; |
| 49 } else { |
| 50 // One dash. Compare against the short name. |
| 51 compareName = &fShortName; |
47 } | 52 } |
48 if (kBool_FlagType == fFlagType) { | 53 if (kBool_FlagType == fFlagType) { |
49 // In this case, go ahead and set the value. | 54 // In this case, go ahead and set the value. |
50 if (fName.equals(string) || fShortName.equals(string)) { | 55 if (compareName->equals(string)) { |
51 *fBoolValue = true; | 56 *fBoolValue = true; |
52 return true; | 57 return true; |
53 } | 58 } |
54 if (SkStrStartsWith(string, "no") && strlen(string) > 2) { | 59 if (SkStrStartsWith(string, "no") && strlen(string) > 2) { |
55 string += 2; | 60 string += 2; |
56 if (fName.equals(string) || fShortName.equals(string)) { | 61 // Only allow "no" to be prepended to the full name. |
| 62 if (fName.equals(string)) { |
57 *fBoolValue = false; | 63 *fBoolValue = false; |
58 return true; | 64 return true; |
59 } | 65 } |
60 return false; | 66 return false; |
61 } | 67 } |
62 int equalIndex = SkStrFind(string, "="); | 68 int equalIndex = SkStrFind(string, "="); |
63 if (equalIndex > 0) { | 69 if (equalIndex > 0) { |
64 // The string has an equal sign. Check to see if the string matc
hes. | 70 // The string has an equal sign. Check to see if the string matc
hes. |
65 SkString flag(string, equalIndex); | 71 SkString flag(string, equalIndex); |
66 if (flag.equals(fName) || flag.equals(fShortName)) { | 72 if (flag.equals(*compareName)) { |
67 // Check to see if the remainder beyond the equal sign is tr
ue or false: | 73 // Check to see if the remainder beyond the equal sign is tr
ue or false: |
68 string += equalIndex + 1; | 74 string += equalIndex + 1; |
69 parse_bool_arg(string, fBoolValue); | 75 parse_bool_arg(string, fBoolValue); |
70 return true; | 76 return true; |
| 77 } else { |
| 78 return false; |
71 } | 79 } |
72 } | 80 } |
73 } | 81 } |
74 return fName.equals(string) || fShortName.equals(string); | 82 return compareName->equals(string); |
75 } else { | 83 } else { |
76 // Has no dash | 84 // Has no dash |
77 return false; | 85 return false; |
78 } | 86 } |
79 return false; | 87 return false; |
80 } | 88 } |
81 | 89 |
82 SkFlagInfo* SkCommandLineFlags::gHead; | 90 SkFlagInfo* SkCommandLineFlags::gHead; |
83 SkString SkCommandLineFlags::gUsage; | 91 SkString SkCommandLineFlags::gUsage; |
84 | 92 |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
136 currLine += lineBreak; | 144 currLine += lineBreak; |
137 } | 145 } |
138 } | 146 } |
139 SkDebugf("\n"); | 147 SkDebugf("\n"); |
140 } | 148 } |
141 | 149 |
142 void SkCommandLineFlags::Parse(int argc, char** argv) { | 150 void SkCommandLineFlags::Parse(int argc, char** argv) { |
143 // Only allow calling this function once. | 151 // Only allow calling this function once. |
144 static bool gOnce; | 152 static bool gOnce; |
145 if (gOnce) { | 153 if (gOnce) { |
146 SkDebugf("Parse should only be called once at the beginning" | 154 SkDebugf("Parse should only be called once at the beginning of main!\n")
; |
147 " of main!\n"); | |
148 SkASSERT(false); | 155 SkASSERT(false); |
149 return; | 156 return; |
150 } | 157 } |
151 gOnce = true; | 158 gOnce = true; |
152 | 159 |
153 bool helpPrinted = false; | 160 bool helpPrinted = false; |
154 // Loop over argv, starting with 1, since the first is just the name of the
program. | 161 // Loop over argv, starting with 1, since the first is just the name of the
program. |
155 for (int i = 1; i < argc; i++) { | 162 for (int i = 1; i < argc; i++) { |
156 if (0 == strcmp("-h", argv[i]) || 0 == strcmp("--h", argv[i]) | 163 if (0 == strcmp("-h", argv[i]) || 0 == strcmp("--help", argv[i])) { |
157 || 0 == strcmp("-help", argv[i]) || 0 == strcmp("--help", argv[i
])) { | |
158 // Print help message. | 164 // Print help message. |
159 SkTDArray<const char*> helpFlags; | 165 SkTDArray<const char*> helpFlags; |
160 for (int j = i + 1; j < argc; j++) { | 166 for (int j = i + 1; j < argc; j++) { |
161 if (SkStrStartsWith(argv[j], '-')) { | 167 if (SkStrStartsWith(argv[j], '-')) { |
162 break; | 168 break; |
163 } | 169 } |
164 helpFlags.append(1, &argv[j]); | 170 helpFlags.append(1, &argv[j]); |
165 } | 171 } |
166 if (0 == helpFlags.count()) { | 172 if (0 == helpFlags.count()) { |
167 // Only print general help message if help for specific flags is
not requested. | 173 // Only print general help message if help for specific flags is
not requested. |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
248 gHead = NULL; | 254 gHead = NULL; |
249 while (flag != NULL) { | 255 while (flag != NULL) { |
250 SkFlagInfo* next = flag->next(); | 256 SkFlagInfo* next = flag->next(); |
251 SkDELETE(flag); | 257 SkDELETE(flag); |
252 flag = next; | 258 flag = next; |
253 } | 259 } |
254 if (helpPrinted) { | 260 if (helpPrinted) { |
255 exit(0); | 261 exit(0); |
256 } | 262 } |
257 } | 263 } |
OLD | NEW |