예제 #1
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();
}
예제 #2
0
//------------------------------------------------------------------------
bool FormatLoad::processFormat(Format &format)
{
  QStringList hfields = lines[currentLine++].split("\t");
  if (hfields.size() < 5) {
    return false;
  }
  QString htmlPage = lines[currentLine++];
  htmlPage.replace(QRegExp("^[\\s]*"), "");
  htmlPage.replace(QRegExp("[\\s]$"), "");

  QRegExp regex("^option");
  QList <FormatOption> optionList;
  while (currentLine <lines.size() && regex.indexIn(lines[currentLine]) == 0) {
    QStringList ofields = lines[currentLine].split("\t");
    if (ofields.size() < 9) {
      return false;
    }
    QString name        = ofields[2];
    QString description = ofields[3];
    QString optionType  = ofields[4];
    QString optionDef   = ofields[5];
    QString optionMin   = ofields[6];
    QString optionMax   = ofields[7];
    QString optionHtml  = ofields[8];
    FormatOption::optionType type = FormatOption::OPTbool;
    if (optionType == "boolean")
      type = FormatOption::OPTbool;
    else if (optionType == "string")
      type = FormatOption::OPTstring;
    else if (optionType == "integer") {
      type = (optionMax != "" && optionMin != "") ? FormatOption::OPTboundedInt : FormatOption::OPTint;
      if (optionMax == "")
	optionMax = "2147483647";
      if (optionMin == "")
	optionMin = "-2147483647";
    }
    else if (optionType == "float") {
      type = FormatOption::OPTfloat;
      if (optionMax == "")
	optionMax = "1.0E308";
      if (optionMin == "")
	optionMin = "-1.0E308";
    }
    else if (optionType == "file") {
      type = FormatOption::OPTinFile;
    }
    else if (optionType == "outfile") {
      type = FormatOption::OPToutFile;
    }
    else {
      type = FormatOption::OPTstring;
    }
    optionList << FormatOption(name, xlt(description),
			       type, QVariant(optionDef), QVariant(optionMin),
			       QVariant(optionMax), optionHtml);
    currentLine++;
  }
  QList <FormatOption> optionList2 = optionList;

  format = Format(hfields[2], xlt(hfields[4]),
		  hfields[1][0] == QChar('r'),  hfields[1][2] == QChar('r'),  hfields[1][4] == QChar('r'),
		  hfields[1][1] == QChar('w'),  hfields[1][3] == QChar('w'),  hfields[1][5] == QChar('w'),
		  hfields[0] == "file",
		  hfields[0] == "serial",
		  QStringList() << hfields[3],
		  optionList,
		  optionList2, htmlPage);
  if (htmlPage.length() > 0 && Format::getHtmlBase().length() == 0) {
    QString base = htmlPage;
    base.replace(QRegExp("/[^/]+$"), "/");
    Format::setHtmlBase(base);
  }
  return true;
}
예제 #3
0
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);
}