diff options
author | Loo Rong Jie <loorongjie@gmail.com> | 2016-11-29 04:05:27 +0800 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2016-11-28 12:05:27 -0800 |
commit | e2581997bbf81b828d9c60601b003b0f7330aef3 (patch) | |
tree | a9426a674ff42531858f8c70dab94632f129577b /src/support | |
parent | 3da6038303d25ffa2599971c94967a86a2300886 (diff) | |
download | binaryen-e2581997bbf81b828d9c60601b003b0f7330aef3.tar.gz binaryen-e2581997bbf81b828d9c60601b003b0f7330aef3.tar.bz2 binaryen-e2581997bbf81b828d9c60601b003b0f7330aef3.zip |
Wrap description (#839)
Diffstat (limited to 'src/support')
-rw-r--r-- | src/support/command-line.cpp | 35 | ||||
-rw-r--r-- | src/support/file.cpp | 1 | ||||
-rw-r--r-- | src/support/file.h | 1 |
3 files changed, 32 insertions, 5 deletions
diff --git a/src/support/command-line.cpp b/src/support/command-line.cpp index 71cb33c2a..1c80b3738 100644 --- a/src/support/command-line.cpp +++ b/src/support/command-line.cpp @@ -18,25 +18,52 @@ 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] != ' ') { + 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(); + } + } +} + 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" << description << "\n\nOptions:\n"; + 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()); } - // TODO: line-wrap description. 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, ' ') << o.description - << '\n'; + << o.shortName << std::string(pad, ' '); + printWrap(std::cerr, optionWidth + 4, o.description); + std::cerr << '\n'; } std::cerr << '\n'; exit(EXIT_SUCCESS); diff --git a/src/support/file.cpp b/src/support/file.cpp index 666c07b87..fd38a7ae3 100644 --- a/src/support/file.cpp +++ b/src/support/file.cpp @@ -16,6 +16,7 @@ #include "support/file.h" +#include <iostream> #include <cstdlib> #include <cstdint> #include <limits> diff --git a/src/support/file.h b/src/support/file.h index 01c7a8546..0f0ec15f3 100644 --- a/src/support/file.h +++ b/src/support/file.h @@ -22,7 +22,6 @@ #define wasm_support_file_h #include <fstream> -#include <iostream> #include <string> #include <utility> #include <vector> |