Example #1
0
void ParameterParser::Help() {
  LineBreaker leftColumn(FLAG_WIDTH);
  LineBreaker rightColumn(DOC_WIDTH);
  std::string left;
  for (size_t groupIndex = 0; groupIndex < groupNameFlagIndexMap.size();
       ++groupIndex) {
    std::string k;
    std::vector<unsigned int> v;
    groupNameFlagIndexMap.at(groupIndex, &k, &v);

    // print group header
    fprintf(stderr, "%s\n", k.c_str());

    for (size_t i = 0; i < v.size(); i++) {
      int idx = v[i];
      const std::string& flag = flagVec[idx];
      FlagInfo& fi = flagInfoMap[idx];

      if (fi.isLongParam) {
        left = "--";
        left += flag;
      } else {
        left = "-";
        left += flag;
      }
      left += ":";

      leftColumn.setContent(left);
      rightColumn.setContent(fi.doc);
      printTwoColumn(stderr, leftColumn, rightColumn, " ");
    }
  }
}
Example #2
0
void TOpts::PrintUsage(const TStringBuf& program, IOutputStream& osIn, const NColorizer::TColors& colors) const {
    TStringStream os;

    if (!Title.empty())
        os << Title << "\n\n";

    PrintCmdLine(program, os, colors);

    TVector<TString> leftColumn(Opts_.size());
    TVector<size_t> leftColumnSizes(leftColumn.size());
    size_t leftWidth = 0;
    size_t requiredOptionsCount = 0;
    NColorizer::TColors disabledColors(false);

    for (size_t i = 0; i < Opts_.size(); i++) {
        const TOpt* opt = Opts_[i].Get();
        if (opt->IsHidden())
            continue;
        leftColumn[i] = FormatOption(opt, colors);
        const size_t leftColumnSize = colors.IsTTY() ? FormatOption(opt, disabledColors).size() : leftColumn[i].size();
        leftColumnSizes[i] = leftColumnSize;
        leftWidth = Max(leftWidth, leftColumnSize);
        if (opt->IsRequired())
            requiredOptionsCount++;
    }

    const size_t kMaxLeftWidth = 25;
    leftWidth = Min(leftWidth, kMaxLeftWidth);
    const TString maxPadding(kMaxLeftWidth, ' ');
    const TString leftPadding(leftWidth, ' ');

    for (size_t sectionId = 0; sectionId <= 1; sectionId++) {
        bool requiredOptionsSection = (sectionId == 0);

        if (requiredOptionsSection) {
            if (requiredOptionsCount == 0)
                continue;
            os << Endl << colors.BoldColor() << "Required parameters" << colors.OldColor() << ":" << Endl;
        } else {
            if (requiredOptionsCount == Opts_.size())
                continue;
            if (requiredOptionsCount == 0)
                os << Endl << colors.BoldColor() << "Options" << colors.OldColor() << ":" << Endl;
            else
                os << Endl << colors.BoldColor() << "Optional parameters" << colors.OldColor() << ":" << Endl;  // optional options would be a tautology
        }

        for (size_t i = 0; i < Opts_.size(); i++) {
            const TOpt* opt = Opts_[i].Get();

            if (opt->IsHidden())
                continue;
            if (opt->IsRequired() != requiredOptionsSection)
                continue;

            if (leftColumnSizes[i] > leftWidth && !opt->GetHelp().empty()) {
                os <<
                SPad << leftColumn[i] << Endl <<
                SPad << maxPadding << ' ';
            } else {
                os << SPad << leftColumn[i] << ' ';
                if (leftColumnSizes[i] < leftWidth)
                    os << TStringBuf(~leftPadding, leftWidth - leftColumnSizes[i]);
            }

            bool multiLineHelp = false;
            if (!opt->GetHelp().empty()) {
                TVector<TStringBuf> helpLines;
                Split(opt->GetHelp(), "\n", helpLines);
                multiLineHelp = (helpLines.size() > 1);
                os << helpLines[0];
                for (size_t j = 1; j < helpLines.size(); ++j) {
                    if (helpLines[j].empty())
                        continue;
                    os << Endl << SPad << leftPadding << ' ' << helpLines[j];
                }
            }

            if (opt->HasDefaultValue()) {
                TString quotedDef = QuoteForHelp(opt->GetDefaultValue());
                if (multiLineHelp)
                    os << Endl << SPad << leftPadding << " Default: " << colors.CyanColor() << quotedDef << colors.OldColor();
                else if (opt->GetHelp().empty())
                    os << "Default: " << colors.CyanColor() << quotedDef << colors.OldColor();
                else
                    os << " (default: " << colors.CyanColor() << quotedDef << colors.OldColor() << ")";
            }

            os << Endl;
        }
    }
    PrintFreeArgsDesc(os, colors);
    osIn << os.Str();
}
Example #3
0
void ParameterParser::Status() {
  fprintf(stderr,
          "The following parameters are available.  Ones with \"[]\" are in "
          "effect:\n");
  fprintf(stderr, "\n");
  fprintf(stderr, "Available Options\n");
  /*
    Format illustration:
    Individual Filter : --indvDepthMin [], --indvDepthMax [], --indvQualMin []
    <- group name widt -><- SEP -><----------- flag name width
    ------------------------->
    GROUP_WIDTH                               FLAG_WIDTH
  */

  LineBreaker leftColumn(FLAG_WIDTH);
  LineBreaker rightColumn(DOC_WIDTH);
  rightColumn.setSeparator(",");
  std::string left;
  std::string right;
  std::string rightItem;

  std::vector<std::string> effectiveOptionVector;
  std::string effectiveOption;
  for (size_t groupIndex = 0; groupIndex < groupNameFlagIndexMap.size();
       ++groupIndex) {
    std::string k;
    std::vector<unsigned int> v;
    groupNameFlagIndexMap.at(groupIndex, &k, &v);

    // process group header
    left = k;
    left += ":";
    right.clear();

    for (size_t i = 0; i < v.size();
         i++) {  // loop flags under the same param group
      effectiveOption.clear();
      int idx = v[i];
      const std::string& flag = flagVec[idx];
      FlagInfo& fi = flagInfoMap[idx];
      void* data = fi.data;

      if (fi.isLongParam) {
        rightItem += " --";
        rightItem += flag;
        if (fi.pt != BOOL_TYPE || fi.isParsed) {
          rightItem += " [";
        }
      } else {
        rightItem += " -";
        rightItem += flag;
        if (fi.pt != BOOL_TYPE || fi.isParsed) {
          rightItem += " [";
        }
      }
      if (fi.isParsed) {
        effectiveOption = "--";
        effectiveOption += flag;
        switch (fi.pt) {
          case BOOL_TYPE:
            if (*(bool*)data) {
              rightItem += "true";
            } else {
              rightItem += "false";
            }
            break;
          case INT_TYPE:
            rightItem += toString(*(int*)data);
            effectiveOption += " ";
            effectiveOption += toString(*(int*)data);
            break;
          case DOUBLE_TYPE:
            rightItem += toString(*(double*)data);
            effectiveOption += " ";
            effectiveOption += toString(*(double*)data);
            break;
          case STRING_TYPE:
            rightItem += *(std::string*)data;
            effectiveOption += " ";
            effectiveOption += *(std::string*)data;
            break;
          default:
            fprintf(
                stderr,
                "ERROR: Bug in Argumetn parsing, report to [email protected]");
            return;
        }
        effectiveOptionVector.push_back(effectiveOption);
      }
      if (fi.pt != BOOL_TYPE || fi.isParsed) {
        rightItem += "]";
      }

      right += rightItem;
      right += ",";
      rightItem.clear();
    }  // end loop per param group
    // fprintf(stderr, "\n");
    leftColumn.setContent(left);
    rightColumn.setContent(right);
    printTwoColumn(stderr, leftColumn, rightColumn, "");
  }

  fprintf(stderr, "\nEffective Options\n");
  for (size_t i = 0; i != effectiveOptionVector.size(); ++i) {
    fprintf(stderr, "    %s\n", effectiveOptionVector[i].c_str());
  }
  fprintf(stderr, "\n");
}
void TOpts::PrintUsage(const TStringBuf& program, TOutputStream& os) const {
    if (!Title.empty())
        os << Title << "\n\n";

    PrintCmdLine(program, os);

    yvector<Stroka> leftColumn(Opts_.size());
    size_t leftWidth = 0;
    size_t requiredOptionsCount = 0;

    for (size_t i = 0; i < Opts_.size(); i++) {
        const TOpt* opt = Opts_[i].Get();
        if (opt->IsHidden())
            continue;
        leftColumn[i] = FormatOption(opt);
        leftWidth = Max(leftWidth, leftColumn[i].size());
        if (opt->IsRequired())
            requiredOptionsCount++;
    }

    const size_t kMaxLeftWidth = 25;
    leftWidth = Min(leftWidth, kMaxLeftWidth);
    const Stroka maxPadding(kMaxLeftWidth, ' ');
    const Stroka leftPadding(leftWidth, ' ');

    for (size_t sectionId = 0; sectionId <= 1; sectionId++) {
        bool requiredOptionsSection = (sectionId == 0);

        if (requiredOptionsSection) {
            if (requiredOptionsCount == 0)
                continue;
            os << Endl << "Required parameters:" << Endl;
        } else {
            if (requiredOptionsCount == Opts_.size())
                continue;
            if (requiredOptionsCount == 0)
                os << Endl << "Options:" << Endl;
            else
                os << Endl << "Optional parameters:" << Endl;  // optional options would be a tautology
        }

        for (size_t i = 0; i < Opts_.size(); i++) {
            const TOpt* opt = Opts_[i].Get();

            if (opt->IsHidden())
                continue;
            if (opt->IsRequired() != requiredOptionsSection)
                continue;

            if (leftColumn[i].size() > leftWidth && !opt->Help_.empty())
                os <<
                    SPad << leftColumn[i] << Endl <<
                    SPad << maxPadding << ' ';
            else
                os << SPad << RightPad(leftColumn[i], leftWidth, ' ') << ' ';

            bool multiLineHelp = false;
            if (!opt->Help_.empty()) {
                yvector<TStringBuf> helpLines;
                Split(opt->Help_, "\n", helpLines);
                multiLineHelp = (helpLines.size() > 1);
                os << helpLines[0];
                for (size_t j = 1; j < helpLines.size(); ++j) {
                    if (helpLines[j].empty())
                        continue;
                    os << Endl << SPad << leftPadding << ' ' << helpLines[j];
                }
            }

            if (opt->HasDefaultValue()) {
                Stroka quotedDef = QuoteForHelp(opt->GetDefaultValue());
                if (multiLineHelp)
                    os << Endl << SPad << leftPadding << " Default: " << quotedDef;
                else if (opt->Help_.empty())
                    os << "Default: " << quotedDef;
                else
                    os << " (default: " << quotedDef << ")";
            }

            os << Endl;
        }
    }
    PrintFreeArgsDesc(os);
}