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 "SkFlags.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) { |
11 for (size_t i = 0; i < len; i++) { | 11 for (size_t i = 0; i < len; i++) { |
12 if (0 == strcmp(target, set[i])) { | 12 if (0 == strcmp(target, set[i])) { |
13 return true; | 13 return true; |
14 } | 14 } |
15 } | 15 } |
16 return false; | 16 return false; |
17 } | 17 } |
18 | 18 |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
72 } | 72 } |
73 } | 73 } |
74 return fName.equals(string) || fShortName.equals(string); | 74 return fName.equals(string) || fShortName.equals(string); |
75 } else { | 75 } else { |
76 // Has no dash | 76 // Has no dash |
77 return false; | 77 return false; |
78 } | 78 } |
79 return false; | 79 return false; |
80 } | 80 } |
81 | 81 |
82 SkFlagInfo* SkFlags::gHead; | 82 SkFlagInfo* SkCommandLineFlags::gHead; |
83 SkString SkFlags::gUsage; | 83 SkString SkCommandLineFlags::gUsage; |
84 | 84 |
85 void SkFlags::SetUsage(const char* usage) { | 85 void SkCommandLineFlags::SetUsage(const char* usage) { |
86 gUsage.set(usage); | 86 gUsage.set(usage); |
87 } | 87 } |
88 | 88 |
89 // Maximum line length for the help message. | 89 // Maximum line length for the help message. |
90 #define LINE_LENGTH 80 | 90 #define LINE_LENGTH 80 |
91 | 91 |
92 static void print_help_for_flag(const SkFlagInfo* flag) { | 92 static void print_help_for_flag(const SkFlagInfo* flag) { |
93 SkDebugf("\t--%s", flag->name().c_str()); | 93 SkDebugf("\t--%s", flag->name().c_str()); |
94 const SkString& shortName = flag->shortName(); | 94 const SkString& shortName = flag->shortName(); |
95 if (shortName.size() > 0) { | 95 if (shortName.size() > 0) { |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 } else { | 132 } else { |
133 // the line break is within the limit. Break there. | 133 // the line break is within the limit. Break there. |
134 lineBreak++; | 134 lineBreak++; |
135 SkDebugf("\t\t%.*s", lineBreak, currLine); | 135 SkDebugf("\t\t%.*s", lineBreak, currLine); |
136 currLine += lineBreak; | 136 currLine += lineBreak; |
137 } | 137 } |
138 } | 138 } |
139 SkDebugf("\n"); | 139 SkDebugf("\n"); |
140 } | 140 } |
141 | 141 |
142 void SkFlags::ParseCommandLine(int argc, char** argv) { | 142 void SkCommandLineFlags::Parse(int argc, char** argv) { |
143 // Only allow calling this function once. | 143 // Only allow calling this function once. |
144 static bool gOnce; | 144 static bool gOnce; |
145 if (gOnce) { | 145 if (gOnce) { |
146 SkDebugf("ParseCommandLine should only be called once at the beginning" | 146 SkDebugf("Parse should only be called once at the beginning" |
147 " of main!\n"); | 147 " of main!\n"); |
148 SkASSERT(false); | 148 SkASSERT(false); |
149 return; | 149 return; |
150 } | 150 } |
151 gOnce = true; | 151 gOnce = true; |
152 | 152 |
153 bool helpPrinted = false; | 153 bool helpPrinted = false; |
154 // Loop over argv, starting with 1, since the first is just the name of the
program. | 154 // Loop over argv, starting with 1, since the first is just the name of the
program. |
155 for (int i = 1; i < argc; i++) { | 155 for (int i = 1; i < argc; i++) { |
156 if (0 == strcmp("-h", argv[i]) || 0 == strcmp("--h", argv[i]) | 156 if (0 == strcmp("-h", argv[i]) || 0 == strcmp("--h", argv[i]) |
157 || 0 == strcmp("-help", argv[i]) || 0 == strcmp("--help", argv[i
])) { | 157 || 0 == strcmp("-help", argv[i]) || 0 == strcmp("--help", argv[i
])) { |
158 // Print help message. | 158 // Print help message. |
159 SkTDArray<const char*> helpFlags; | 159 SkTDArray<const char*> helpFlags; |
160 for (int j = i + 1; j < argc; j++) { | 160 for (int j = i + 1; j < argc; j++) { |
161 if (SkStrStartsWith(argv[j], '-')) { | 161 if (SkStrStartsWith(argv[j], '-')) { |
162 break; | 162 break; |
163 } | 163 } |
164 helpFlags.append(1, &argv[j]); | 164 helpFlags.append(1, &argv[j]); |
165 } | 165 } |
166 if (0 == helpFlags.count()) { | 166 if (0 == helpFlags.count()) { |
167 // Only print general help message if help for specific flags is
not requested. | 167 // Only print general help message if help for specific flags is
not requested. |
168 SkDebugf("%s\n%s\n", argv[0], gUsage.c_str()); | 168 SkDebugf("%s\n%s\n", argv[0], gUsage.c_str()); |
169 } | 169 } |
170 SkDebugf("Flags:\n"); | 170 SkDebugf("Flags:\n"); |
171 SkFlagInfo* flag = SkFlags::gHead; | 171 SkFlagInfo* flag = SkCommandLineFlags::gHead; |
172 while (flag != NULL) { | 172 while (flag != NULL) { |
173 // If no flags followed --help, print them all | 173 // If no flags followed --help, print them all |
174 bool printFlag = 0 == helpFlags.count(); | 174 bool printFlag = 0 == helpFlags.count(); |
175 if (!printFlag) { | 175 if (!printFlag) { |
176 for (int k = 0; k < helpFlags.count(); k++) { | 176 for (int k = 0; k < helpFlags.count(); k++) { |
177 if (flag->name().equals(helpFlags[k]) || | 177 if (flag->name().equals(helpFlags[k]) || |
178 flag->shortName().equals(helpFlags[k])) { | 178 flag->shortName().equals(helpFlags[k])) { |
179 printFlag = true; | 179 printFlag = true; |
180 helpFlags.remove(k); | 180 helpFlags.remove(k); |
181 break; | 181 break; |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
248 gHead = NULL; | 248 gHead = NULL; |
249 while (flag != NULL) { | 249 while (flag != NULL) { |
250 SkFlagInfo* next = flag->next(); | 250 SkFlagInfo* next = flag->next(); |
251 SkDELETE(flag); | 251 SkDELETE(flag); |
252 flag = next; | 252 flag = next; |
253 } | 253 } |
254 if (helpPrinted) { | 254 if (helpPrinted) { |
255 exit(0); | 255 exit(0); |
256 } | 256 } |
257 } | 257 } |
OLD | NEW |