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 #include "SkTSort.h" |
11 | 11 |
12 #include <stdlib.h> | 12 #include <stdlib.h> |
13 | 13 |
14 #if defined(GOOGLE3) && (defined(SK_BUILD_FOR_ANDROID) || defined(SK_BUILD_FOR_I
OS)) | 14 #if defined(GOOGLE3) && (defined(SK_BUILD_FOR_ANDROID) || defined(SK_BUILD_FOR_I
OS)) |
15 // I don't know why, but this is defined by //base only for non-Linux. | 15 // I don't know why, but this is defined by //base only for non-Linux. |
16 DECLARE_bool(undefok) | 16 DECLARE_bool(undefok) |
17 #else | 17 #else |
18 DEFINE_bool(undefok, false, "Silently ignore unknown flags instead of crashi
ng."); | 18 DEFINE_bool(undefok, false, "Silently ignore unknown flags instead of crashi
ng."); |
19 #endif | 19 #endif |
20 | 20 |
21 template <typename T> static void ignore_result(const T&) {} | 21 template <typename T> static void ignore_result(const T&) {} |
22 | 22 |
23 bool SkFlagInfo::CreateStringFlag(const char* name, const char* shortName, | 23 bool SkFlagInfo::CreateStringFlag(const char* name, const char* shortName, |
24 SkCommandLineFlags::StringArray* pStrings, | 24 SkCommandLineFlags::StringArray* pStrings, |
25 const char* defaultValue, const char* helpStri
ng, | 25 const char* defaultValue, const char* helpStri
ng) { |
26 const char* extendedHelpString) { | 26 SkFlagInfo* info = new SkFlagInfo(name, shortName, kString_FlagType, helpStr
ing); |
27 SkFlagInfo* info = new SkFlagInfo(name, shortName, kString_FlagType, helpStr
ing, | |
28 extendedHelpString); | |
29 info->fDefaultString.set(defaultValue); | 27 info->fDefaultString.set(defaultValue); |
30 | 28 |
31 info->fStrings = pStrings; | 29 info->fStrings = pStrings; |
32 SetDefaultStrings(pStrings, defaultValue); | 30 SetDefaultStrings(pStrings, defaultValue); |
33 return true; | 31 return true; |
34 } | 32 } |
35 | 33 |
36 void SkFlagInfo::SetDefaultStrings(SkCommandLineFlags::StringArray* pStrings, | 34 void SkFlagInfo::SetDefaultStrings(SkCommandLineFlags::StringArray* pStrings, |
37 const char* defaultValue) { | 35 const char* defaultValue) { |
38 pStrings->reset(); | 36 pStrings->reset(); |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
146 SkFlagInfo* SkCommandLineFlags::gHead; | 144 SkFlagInfo* SkCommandLineFlags::gHead; |
147 SkString SkCommandLineFlags::gUsage; | 145 SkString SkCommandLineFlags::gUsage; |
148 | 146 |
149 void SkCommandLineFlags::SetUsage(const char* usage) { | 147 void SkCommandLineFlags::SetUsage(const char* usage) { |
150 gUsage.set(usage); | 148 gUsage.set(usage); |
151 } | 149 } |
152 | 150 |
153 // Maximum line length for the help message. | 151 // Maximum line length for the help message. |
154 #define LINE_LENGTH 72 | 152 #define LINE_LENGTH 72 |
155 | 153 |
156 static void print_indented(const SkString& text) { | 154 static void print_help_for_flag(const SkFlagInfo* flag) { |
157 size_t length = text.size(); | 155 SkDebugf(" --%s", flag->name().c_str()); |
158 const char* currLine = text.c_str(); | 156 const SkString& shortName = flag->shortName(); |
| 157 if (shortName.size() > 0) { |
| 158 SkDebugf(" or -%s", shortName.c_str()); |
| 159 } |
| 160 SkDebugf(":\ttype: %s", flag->typeAsString().c_str()); |
| 161 if (flag->defaultValue().size() > 0) { |
| 162 SkDebugf("\tdefault: %s", flag->defaultValue().c_str()); |
| 163 } |
| 164 SkDebugf("\n"); |
| 165 const SkString& help = flag->help(); |
| 166 size_t length = help.size(); |
| 167 const char* currLine = help.c_str(); |
159 const char* stop = currLine + length; | 168 const char* stop = currLine + length; |
160 while (currLine < stop) { | 169 while (currLine < stop) { |
| 170 if (strlen(currLine) < LINE_LENGTH) { |
| 171 // Only one line length's worth of text left. |
| 172 SkDebugf(" %s\n", currLine); |
| 173 break; |
| 174 } |
161 int lineBreak = SkStrFind(currLine, "\n"); | 175 int lineBreak = SkStrFind(currLine, "\n"); |
162 if (lineBreak < 0) { | 176 if (lineBreak < 0 || lineBreak > LINE_LENGTH) { |
163 lineBreak = static_cast<int>(strlen(currLine)); | |
164 } | |
165 if (lineBreak > LINE_LENGTH) { | |
166 // No line break within line length. Will need to insert one. | 177 // No line break within line length. Will need to insert one. |
167 // Find a space before the line break. | 178 // Find a space before the line break. |
168 int spaceIndex = LINE_LENGTH - 1; | 179 int spaceIndex = LINE_LENGTH - 1; |
169 while (spaceIndex > 0 && currLine[spaceIndex] != ' ') { | 180 while (spaceIndex > 0 && currLine[spaceIndex] != ' ') { |
170 spaceIndex--; | 181 spaceIndex--; |
171 } | 182 } |
172 int gap; | 183 int gap; |
173 if (0 == spaceIndex) { | 184 if (0 == spaceIndex) { |
174 // No spaces on the entire line. Go ahead and break mid word. | 185 // No spaces on the entire line. Go ahead and break mid word. |
175 spaceIndex = LINE_LENGTH; | 186 spaceIndex = LINE_LENGTH; |
176 gap = 0; | 187 gap = 0; |
177 } else { | 188 } else { |
178 // Skip the space on the next line | 189 // Skip the space on the next line |
179 gap = 1; | 190 gap = 1; |
180 } | 191 } |
181 SkDebugf(" %.*s\n", spaceIndex, currLine); | 192 SkDebugf(" %.*s\n", spaceIndex, currLine); |
182 currLine += spaceIndex + gap; | 193 currLine += spaceIndex + gap; |
183 } else { | 194 } else { |
184 // the line break is within the limit. Break there. | 195 // the line break is within the limit. Break there. |
185 lineBreak++; | 196 lineBreak++; |
186 SkDebugf(" %.*s", lineBreak, currLine); | 197 SkDebugf(" %.*s", lineBreak, currLine); |
187 currLine += lineBreak; | 198 currLine += lineBreak; |
188 } | 199 } |
189 } | 200 } |
190 } | |
191 | |
192 static void print_help_for_flag(const SkFlagInfo* flag) { | |
193 SkDebugf(" --%s", flag->name().c_str()); | |
194 const SkString& shortName = flag->shortName(); | |
195 if (shortName.size() > 0) { | |
196 SkDebugf(" or -%s", shortName.c_str()); | |
197 } | |
198 SkDebugf(":\ttype: %s", flag->typeAsString().c_str()); | |
199 if (flag->defaultValue().size() > 0) { | |
200 SkDebugf("\tdefault: %s", flag->defaultValue().c_str()); | |
201 } | |
202 SkDebugf("\n"); | |
203 const SkString& help = flag->help(); | |
204 print_indented(help); | |
205 SkDebugf("\n"); | |
206 } | |
207 static void print_extended_help_for_flag(const SkFlagInfo* flag) { | |
208 print_help_for_flag(flag); | |
209 print_indented(flag->extendedHelp()); | |
210 SkDebugf("\n"); | 201 SkDebugf("\n"); |
211 } | 202 } |
212 | 203 |
213 namespace { | 204 namespace { |
214 struct CompareFlagsByName { | 205 struct CompareFlagsByName { |
215 bool operator()(SkFlagInfo* a, SkFlagInfo* b) const { | 206 bool operator()(SkFlagInfo* a, SkFlagInfo* b) const { |
216 return strcmp(a->name().c_str(), b->name().c_str()) < 0; | 207 return strcmp(a->name().c_str(), b->name().c_str()) < 0; |
217 } | 208 } |
218 }; | 209 }; |
219 } // namespace | 210 } // namespace |
(...skipping 30 matching lines...) Expand all Loading... |
250 // If no flags followed --help, print them all | 241 // If no flags followed --help, print them all |
251 SkTDArray<SkFlagInfo*> allFlags; | 242 SkTDArray<SkFlagInfo*> allFlags; |
252 for (SkFlagInfo* flag = SkCommandLineFlags::gHead; flag; | 243 for (SkFlagInfo* flag = SkCommandLineFlags::gHead; flag; |
253 flag = flag->next()) { | 244 flag = flag->next()) { |
254 allFlags.push(flag); | 245 allFlags.push(flag); |
255 } | 246 } |
256 SkTQSort(&allFlags[0], &allFlags[allFlags.count() - 1], | 247 SkTQSort(&allFlags[0], &allFlags[allFlags.count() - 1], |
257 CompareFlagsByName()); | 248 CompareFlagsByName()); |
258 for (int i = 0; i < allFlags.count(); ++i) { | 249 for (int i = 0; i < allFlags.count(); ++i) { |
259 print_help_for_flag(allFlags[i]); | 250 print_help_for_flag(allFlags[i]); |
260 if (allFlags[i]->extendedHelp().size() > 0) { | |
261 SkDebugf(" Use '--help %s' for more information.\
n", | |
262 allFlags[i]->name().c_str()); | |
263 } | |
264 } | 251 } |
265 } else { | 252 } else { |
266 for (SkFlagInfo* flag = SkCommandLineFlags::gHead; flag; | 253 for (SkFlagInfo* flag = SkCommandLineFlags::gHead; flag; |
267 flag = flag->next()) { | 254 flag = flag->next()) { |
268 for (int k = 0; k < helpFlags.count(); k++) { | 255 for (int k = 0; k < helpFlags.count(); k++) { |
269 if (flag->name().equals(helpFlags[k]) || | 256 if (flag->name().equals(helpFlags[k]) || |
270 flag->shortName().equals(helpFlags[k])) { | 257 flag->shortName().equals(helpFlags[k])) { |
271 print_extended_help_for_flag(flag); | 258 print_help_for_flag(flag); |
272 helpFlags.remove(k); | 259 helpFlags.remove(k); |
273 break; | 260 break; |
274 } | 261 } |
275 } | 262 } |
276 } | 263 } |
277 } | 264 } |
278 if (helpFlags.count() > 0) { | 265 if (helpFlags.count() > 0) { |
279 SkDebugf("Requested help for unrecognized flags:\n"); | 266 SkDebugf("Requested help for unrecognized flags:\n"); |
280 for (int k = 0; k < helpFlags.count(); k++) { | 267 for (int k = 0; k < helpFlags.count(); k++) { |
281 SkDebugf(" --%s\n", helpFlags[k]); | 268 SkDebugf(" --%s\n", helpFlags[k]); |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
395 } | 382 } |
396 | 383 |
397 } // namespace | 384 } // namespace |
398 | 385 |
399 bool SkCommandLineFlags::ShouldSkip(const SkTDArray<const char*>& strings, const
char* name) { | 386 bool SkCommandLineFlags::ShouldSkip(const SkTDArray<const char*>& strings, const
char* name) { |
400 return ShouldSkipImpl(strings, name); | 387 return ShouldSkipImpl(strings, name); |
401 } | 388 } |
402 bool SkCommandLineFlags::ShouldSkip(const StringArray& strings, const char* name
) { | 389 bool SkCommandLineFlags::ShouldSkip(const StringArray& strings, const char* name
) { |
403 return ShouldSkipImpl(strings, name); | 390 return ShouldSkipImpl(strings, name); |
404 } | 391 } |
OLD | NEW |