<commit_before>/*
 * Copyright 2015 WebAssembly Community Group participants
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "support/command-line.h"

using namespace wasm;

#ifndef SCREEN_WIDTH
#define SCREEN_WIDTH 80
#endif

void printWrap(std::ostream& os, int leftPad, const std::string& content) {
  int len = content.size();
  int space = SCREEN_WIDTH - leftPad;
  std::string nextWord;
  std::string pad(leftPad, ' ');
  for (int i = 0; i <= len; ++i) {
    if (i != len && content[i] != ' ' && content[i] != '\n') {
      nextWord += content[i];
    } else {
      if (static_cast<int>(nextWord.size()) > space) {
        os << '\n' << pad;
        space = SCREEN_WIDTH - leftPad;
      }
      os << nextWord;
      space -= nextWord.size() + 1;
      if (space > 0) os << ' ';
      nextWord.clear();
      if (content[i] == '\n') {
        os << '\n';
        space = SCREEN_WIDTH - leftPad;
      }
    }
  }
}

Options::Options(const std::string& command, const std::string& description)
    : debug(false), positional(Arguments::Zero) {
  add("--help", "-h", "Show this help message and exit", Arguments::Zero,
      [this, command, description](Options* o, const std::string&) {
        std::cerr << command;
        if (positional != Arguments::Zero) std::cerr << ' ' << positionalName;
        std::cerr << "\n\n";
        printWrap(std::cerr, 0, description);
        std::cerr << "\n\nOptions:\n";
        size_t optionWidth = 0;
        for (const auto& o : options) {
          optionWidth =
              std::max(optionWidth, o.longName.size() + o.shortName.size());
        }
        for (const auto& o : options) {
          bool long_n_short = o.longName.size() != 0 && o.shortName.size() != 0;
          size_t pad = 1 + optionWidth - o.longName.size() - o.shortName.size();
          std::cerr << "  " << o.longName << (long_n_short ? ',' : ' ')
                    << o.shortName << std::string(pad, ' ');
          printWrap(std::cerr, optionWidth + 4, o.description);
          std::cerr << '\n';
        }
        std::cerr << '\n';
        exit(EXIT_SUCCESS);
      });
  add("--debug", "-d", "Print debug information to stderr", Arguments::Zero,
      [&](Options* o, const std::string& arguments) { debug = true; });
}
<commit_msg>Use stdout for --help message (#1937)<commit_after>/*
 * Copyright 2015 WebAssembly Community Group participants
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "support/command-line.h"

using namespace wasm;

#ifndef SCREEN_WIDTH
#define SCREEN_WIDTH 80
#endif

void printWrap(std::ostream& os, int leftPad, const std::string& content) {
  int len = content.size();
  int space = SCREEN_WIDTH - leftPad;
  std::string nextWord;
  std::string pad(leftPad, ' ');
  for (int i = 0; i <= len; ++i) {
    if (i != len && content[i] != ' ' && content[i] != '\n') {
      nextWord += content[i];
    } else {
      if (static_cast<int>(nextWord.size()) > space) {
        os << '\n' << pad;
        space = SCREEN_WIDTH - leftPad;
      }
      os << nextWord;
      space -= nextWord.size() + 1;
      if (space > 0) os << ' ';
      nextWord.clear();
      if (content[i] == '\n') {
        os << '\n';
        space = SCREEN_WIDTH - leftPad;
      }
    }
  }
}

Options::Options(const std::string& command, const std::string& description)
    : debug(false), positional(Arguments::Zero) {
  add("--help", "-h", "Show this help message and exit", Arguments::Zero,
      [this, command, description](Options* o, const std::string&) {
        std::cout << command;
        if (positional != Arguments::Zero) std::cout << ' ' << positionalName;
        std::cout << "\n\n";
        printWrap(std::cout, 0, description);
        std::cout << "\n\nOptions:\n";
        size_t optionWidth = 0;
        for (const auto& o : options) {
          optionWidth =
              std::max(optionWidth, o.longName.size() + o.shortName.size());
        }
        for (const auto& o : options) {
          bool long_n_short = o.longName.size() != 0 && o.shortName.size() != 0;
          size_t pad = 1 + optionWidth - o.longName.size() - o.shortName.size();
          std::cout << "  " << o.longName << (long_n_short ? ',' : ' ')
                    << o.shortName << std::string(pad, ' ');
          printWrap(std::cout, optionWidth + 4, o.description);
          std::cout << '\n';
        }
        std::cout << '\n';
        exit(EXIT_SUCCESS);
      });
  add("--debug", "-d", "Print debug information to stderr", Arguments::Zero,
      [&](Options* o, const std::string& arguments) { debug = true; });
}

{'fraction_non_alphanumeric': 0.1586366880799429, 'fraction_numerical': 0.007494646680942184, 'mean_word_length': 2.6254851228978007, 'pattern_counts': {'":': 0, '<': 75, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '898679', 'n_tokens_mistral': 1767, 'n_tokens_neox': 1725, 'n_words': 511}