Flutter Engine
The Flutter Engine
Classes | Static Public Member Functions | Friends | List of all members
CommandLineFlags Class Reference

#include <CommandLineFlags.h>

Classes

class  StringArray
 

Static Public Member Functions

static void SetUsage (const char *usage)
 
static void PrintUsage ()
 
static void Parse (int argc, const char *const *argv)
 
static bool ShouldSkip (const SkTDArray< const char * > &strings, const char *name)
 
static bool ShouldSkip (const StringArray &strings, const char *name)
 

Friends

class SkFlagInfo
 

Detailed Description

Definition at line 104 of file CommandLineFlags.h.

Member Function Documentation

◆ Parse()

void CommandLineFlags::Parse ( int  argc,
const char *const *  argv 
)
static

Call at the beginning of main to parse flags created by DEFINE_x, above. Must only be called once.

Definition at line 215 of file CommandLineFlags.cpp.

215 {
216 // Only allow calling this function once.
217 static bool gOnce;
218 if (gOnce) {
219 SkDebugf("Parse should only be called once at the beginning of main!\n");
220 SkASSERT(false);
221 return;
222 }
223 gOnce = true;
224
225 bool helpPrinted = false;
226 bool flagsPrinted = false;
227 // Loop over argv, starting with 1, since the first is just the name of the program.
228 for (int i = 1; i < argc; i++) {
229 if (0 == strcmp("-h", argv[i]) || 0 == strcmp("--help", argv[i])) {
230 // Print help message.
231 SkTDArray<const char*> helpFlags;
232 for (int j = i + 1; j < argc; j++) {
233 if (SkStrStartsWith(argv[j], '-')) {
234 break;
235 }
236 helpFlags.append(1, &argv[j]);
237 }
238 if (0 == helpFlags.size()) {
239 // Only print general help message if help for specific flags is not requested.
240 SkDebugf("%s\n%s\n", argv[0], gUsage.c_str());
241 }
242 if (!flagsPrinted) {
243 SkDebugf("Flags:\n");
244 flagsPrinted = true;
245 }
246 if (0 == helpFlags.size()) {
247 // If no flags followed --help, print them all
248 SkTDArray<SkFlagInfo*> allFlags;
249 for (SkFlagInfo* flag = CommandLineFlags::gHead; flag; flag = flag->next()) {
250 allFlags.push_back(flag);
251 }
252 SkTQSort(allFlags.begin(), allFlags.end(), CompareFlagsByName());
253 for (SkFlagInfo* flag : allFlags) {
255 if (flag->extendedHelp().size() > 0) {
256 SkDebugf(" Use '--help %s' for more information.\n",
257 flag->name().c_str());
258 }
259 }
260 } else {
261 for (SkFlagInfo* flag = CommandLineFlags::gHead; flag; flag = flag->next()) {
262 for (int k = 0; k < helpFlags.size(); k++) {
263 if (flag->name().equals(helpFlags[k]) ||
264 flag->shortName().equals(helpFlags[k])) {
266 helpFlags.remove(k);
267 break;
268 }
269 }
270 }
271 }
272 if (helpFlags.size() > 0) {
273 SkDebugf("Requested help for unrecognized flags:\n");
274 for (int k = 0; k < helpFlags.size(); k++) {
275 SkDebugf(" --%s\n", helpFlags[k]);
276 }
277 }
278 helpPrinted = true;
279 }
280 if (!helpPrinted) {
281 SkFlagInfo* matchedFlag = nullptr;
282 SkFlagInfo* flag = gHead;
283 int startI = i;
284 while (flag != nullptr) {
285 if (flag->match(argv[startI])) {
286 i = startI;
287 if (matchedFlag) {
288 // Don't redefine the same flag with different types.
289 SkASSERT(matchedFlag->getFlagType() == flag->getFlagType());
290 } else {
291 matchedFlag = flag;
292 }
293 switch (flag->getFlagType()) {
295 // Can be handled by match, above, but can also be set by the next
296 // string.
297 if (i + 1 < argc && !SkStrStartsWith(argv[i + 1], '-')) {
298 i++;
299 bool value;
300 if (parse_bool_arg(argv[i], &value)) {
301 flag->setBool(value);
302 }
303 }
304 break;
306 flag->resetStrings();
307 // Add all arguments until another flag is reached.
308 while (i + 1 < argc) {
309 char* end = nullptr;
310 // Negative numbers aren't flags.
311 ignore_result(strtod(argv[i + 1], &end));
312 if (end == argv[i + 1] && SkStrStartsWith(argv[i + 1], '-')) {
313 break;
314 }
315 i++;
316 flag->append(argv[i]);
317 }
318 break;
320 i++;
321 flag->setInt(atoi(argv[i]));
322 break;
324 i++;
325 flag->setDouble(atof(argv[i]));
326 break;
327 default: SkDEBUGFAIL("Invalid flag type");
328 }
329 }
330 flag = flag->next();
331 }
332 if (!matchedFlag) {
333#if defined(SK_BUILD_FOR_MAC)
334 if (SkStrStartsWith(argv[i], "NSDocumentRevisions") ||
335 SkStrStartsWith(argv[i], "-NSDocumentRevisions")) {
336 i++; // skip YES
337 } else
338#endif
339 SkDebugf("Got unknown flag '%s'. Exiting.\n", argv[i]);
340 exit(-1);
341 }
342 }
343 }
344 // Since all of the flags have been set, release the memory used by each
345 // flag. FLAGS_x can still be used after this.
346 SkFlagInfo* flag = gHead;
347 gHead = nullptr;
348 while (flag != nullptr) {
349 SkFlagInfo* next = flag->next();
350 delete flag;
351 flag = next;
352 }
353 if (helpPrinted) {
354 exit(0);
355 }
356}
static bool parse_bool_arg(const char *string, bool *result)
static void print_extended_help_for_flag(const SkFlagInfo *flag)
static void ignore_result(const T &)
static void print_help_for_flag(const SkFlagInfo *flag)
static float next(float f)
#define SkDEBUGFAIL(message)
Definition: SkAssert.h:118
#define SkASSERT(cond)
Definition: SkAssert.h:116
void SK_SPI SkDebugf(const char format[],...) SK_PRINTF_LIKE(1
static bool SkStrStartsWith(const char string[], const char prefixStr[])
Definition: SkString.h:26
void SkTQSort(T *begin, T *end, const C &lessThan)
Definition: SkTSort.h:194
FlagTypes getFlagType() const
const char * c_str() const
Definition: SkString.h:133
T * end()
Definition: SkTDArray.h:152
int size() const
Definition: SkTDArray.h:138
void push_back(const T &v)
Definition: SkTDArray.h:219
T * begin()
Definition: SkTDArray.h:150
T * append()
Definition: SkTDArray.h:191
void remove(int index, int count=1)
Definition: SkTDArray.h:210
FlutterSemanticsFlag flag
glong glong end
uint8_t value
char ** argv
Definition: library.h:9
exit(kErrorExitCode)

◆ PrintUsage()

void CommandLineFlags::PrintUsage ( )
static

Call this to display the help message. Should be called after SetUsage.

Definition at line 145 of file CommandLineFlags.cpp.

145{ SkDebugf("%s", gUsage.c_str()); }

◆ SetUsage()

void CommandLineFlags::SetUsage ( const char *  usage)
static

Call to set the help message to be displayed. Should be called before Parse.

Definition at line 143 of file CommandLineFlags.cpp.

143{ gUsage.set(usage); }
void set(const SkString &src)
Definition: SkString.h:186
static void usage(char *argv0)

◆ ShouldSkip() [1/2]

bool CommandLineFlags::ShouldSkip ( const SkTDArray< const char * > &  strings,
const char *  name 
)
static

Definition at line 394 of file CommandLineFlags.cpp.

394 {
395 return ShouldSkipImpl(strings, name);
396}
DEF_SWITCHES_START aot vmservice shared library name
Definition: switches.h:32

◆ ShouldSkip() [2/2]

bool CommandLineFlags::ShouldSkip ( const StringArray strings,
const char *  name 
)
static

Definition at line 397 of file CommandLineFlags.cpp.

397 {
398 return ShouldSkipImpl(strings, name);
399}

Friends And Related Function Documentation

◆ SkFlagInfo

friend class SkFlagInfo
friend

Definition at line 212 of file CommandLineFlags.h.


The documentation for this class was generated from the following files: