From 8366df025a5c3eb562c6bff1ddf63213fbf3f7d6 Mon Sep 17 00:00:00 2001 From: "scroggo@google.com" Date: Wed, 20 Mar 2013 19:50:41 +0000 Subject: Update help in SkFlags. Print the short name in addition to the standard name. Allow an option for getting help for a specific command. Review URL: https://codereview.chromium.org/12411007 git-svn-id: http://skia.googlecode.com/svn/trunk@8273 2bbb7eff-a529-9590-31e7-b0007b416f81 --- tools/SkFlags.cpp | 120 +++++++++++++++++++++++++++++++++++------------------- tools/SkFlags.h | 2 + 2 files changed, 81 insertions(+), 41 deletions(-) (limited to 'tools') diff --git a/tools/SkFlags.cpp b/tools/SkFlags.cpp index e386b429fe..84b5ef297d 100644 --- a/tools/SkFlags.cpp +++ b/tools/SkFlags.cpp @@ -17,6 +17,56 @@ void SkFlags::SetUsage(const char* usage) { // Maximum line length for the help message. #define LINE_LENGTH 80 +static void print_help_for_flag(const SkFlagInfo* flag) { + SkDebugf("\t--%s", flag->name().c_str()); + const SkString& shortName = flag->shortName(); + if (shortName.size() > 0) { + SkDebugf(" or -%s", shortName.c_str()); + } + SkDebugf(":\ttype: %s", flag->typeAsString().c_str()); + if (flag->defaultValue().size() > 0) { + SkDebugf("\tdefault: %s", flag->defaultValue().c_str()); + } + SkDebugf("\n"); + const SkString& help = flag->help(); + size_t length = help.size(); + const char* currLine = help.c_str(); + const char* stop = currLine + length; + while (currLine < stop) { + if (strlen(currLine) < LINE_LENGTH) { + // Only one line length's worth of text left. + SkDebugf("\t\t%s\n", currLine); + break; + } + int lineBreak = SkStrFind(currLine, "\n"); + if (lineBreak < 0 || lineBreak > LINE_LENGTH) { + // No line break within line length. Will need to insert one. + // Find a space before the line break. + int spaceIndex = LINE_LENGTH - 1; + while (spaceIndex > 0 && currLine[spaceIndex] != ' ') { + spaceIndex--; + } + int gap; + if (0 == spaceIndex) { + // No spaces on the entire line. Go ahead and break mid word. + spaceIndex = LINE_LENGTH; + gap = 0; + } else { + // Skip the space on the next line + gap = 1; + } + SkDebugf("\t\t%.*s\n", spaceIndex, currLine); + currLine += spaceIndex + gap; + } else { + // the line break is within the limit. Break there. + lineBreak++; + SkDebugf("\t\t%.*s", lineBreak, currLine); + currLine += lineBreak; + } + } + SkDebugf("\n"); +} + void SkFlags::ParseCommandLine(int argc, char** argv) { // Only allow calling this function once. static bool gOnce; @@ -34,55 +84,43 @@ void SkFlags::ParseCommandLine(int argc, char** argv) { if (0 == strcmp("-h", argv[i]) || 0 == strcmp("--h", argv[i]) || 0 == strcmp("-help", argv[i]) || 0 == strcmp("--help", argv[i])) { // Print help message. - SkDebugf("%s\n%s\n", argv[0], gUsage.c_str()); + SkTDArray helpFlags; + for (int j = i + 1; j < argc; j++) { + if (SkStrStartsWith(argv[j], '-')) { + break; + } + helpFlags.append(1, &argv[j]); + } + if (0 == helpFlags.count()) { + // Only print general help message if help for specific flags is not requested. + SkDebugf("%s\n%s\n", argv[0], gUsage.c_str()); + } SkDebugf("Flags:\n"); SkFlagInfo* flag = SkFlags::gHead; while (flag != NULL) { - SkDebugf("\t--%s:\ttype: %s", flag->name().c_str(), - flag->typeAsString().c_str()); - if (flag->defaultValue().size() > 0) { - SkDebugf("\tdefault: %s", flag->defaultValue().c_str()); - } - SkDebugf("\n"); - const SkString& help = flag->help(); - size_t length = help.size(); - const char* currLine = help.c_str(); - const char* stop = currLine + length; - while (currLine < stop) { - if (strlen(currLine) < LINE_LENGTH) { - // Only one line length's worth of text left. - SkDebugf("\t\t%s\n", currLine); - break; - } - int lineBreak = SkStrFind(currLine, "\n"); - if (lineBreak < 0 || lineBreak > LINE_LENGTH) { - // No line break within line length. Will need to insert one. - // Find a space before the line break. - int spaceIndex = LINE_LENGTH - 1; - while (spaceIndex > 0 && currLine[spaceIndex] != ' ') { - spaceIndex--; - } - int gap; - if (0 == spaceIndex) { - // No spaces on the entire line. Go ahead and break mid word. - spaceIndex = LINE_LENGTH; - gap = 0; - } else { - // Skip the space on the next line - gap = 1; + // If no flags followed --help, print them all + bool printFlag = 0 == helpFlags.count(); + if (!printFlag) { + for (int k = 0; k < helpFlags.count(); k++) { + if (flag->name().equals(helpFlags[k]) || + flag->shortName().equals(helpFlags[k])) { + printFlag = true; + helpFlags.remove(k); + break; } - SkDebugf("\t\t%.*s\n", spaceIndex, currLine); - currLine += spaceIndex + gap; - } else { - // the line break is within the limit. Break there. - lineBreak++; - SkDebugf("\t\t%.*s", lineBreak, currLine); - currLine += lineBreak; } } - SkDebugf("\n"); + if (printFlag) { + print_help_for_flag(flag); + } flag = flag->next(); } + if (helpFlags.count() > 0) { + SkDebugf("Requested help for unrecognized flags:\n"); + for (int k = 0; k < helpFlags.count(); k++) { + SkDebugf("\t--%s\n", helpFlags[k]); + } + } helpPrinted = true; } if (!helpPrinted) { diff --git a/tools/SkFlags.h b/tools/SkFlags.h index 79cc8788a3..a2d4c97ece 100644 --- a/tools/SkFlags.h +++ b/tools/SkFlags.h @@ -298,6 +298,8 @@ public: const SkString& name() const { return fName; } + const SkString& shortName() const { return fShortName; } + const SkString& help() const { return fHelpString; } SkString defaultValue() const { -- cgit v1.2.3