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