diff options
author | JF Bastien <jfb@chromium.org> | 2016-01-11 17:23:27 -0800 |
---|---|---|
committer | JF Bastien <jfb@chromium.org> | 2016-01-11 17:23:27 -0800 |
commit | 1b3600b0aca6a8f23b6d9e8ae33dcd75c9493810 (patch) | |
tree | 606226cd0b851f09999647643476c492a80a93a3 /src/support | |
parent | 75a562190a9f4588c8ffb19b8304f76c15a850c6 (diff) | |
download | binaryen-1b3600b0aca6a8f23b6d9e8ae33dcd75c9493810.tar.gz binaryen-1b3600b0aca6a8f23b6d9e8ae33dcd75c9493810.tar.bz2 binaryen-1b3600b0aca6a8f23b6d9e8ae33dcd75c9493810.zip |
asm2wasm: use support's command-line
Diffstat (limited to 'src/support')
-rw-r--r-- | src/support/command-line.cpp | 25 | ||||
-rw-r--r-- | src/support/command-line.h | 4 | ||||
-rw-r--r-- | src/support/file.cpp | 9 | ||||
-rw-r--r-- | src/support/file.h | 9 |
4 files changed, 34 insertions, 13 deletions
diff --git a/src/support/command-line.cpp b/src/support/command-line.cpp index 659c055c9..c078ef45f 100644 --- a/src/support/command-line.cpp +++ b/src/support/command-line.cpp @@ -19,7 +19,7 @@ using namespace wasm; Options::Options(const std::string &command, const std::string &description) - : debug(false), positional(Arguments::Zero) { + : debug(0), 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; @@ -39,8 +39,10 @@ Options::Options(const std::string &command, const std::string &description) std::cerr << '\n'; exit(EXIT_SUCCESS); }); - add("--debug", "-d", "Print debug information to stderr", Arguments::Zero, - [](Options *o, const std::string &) { o->debug = true; }); + add("--debug", "-d", "Print debug information to stderr", Arguments::Optional, + [](Options *o, const std::string &arguments) { + o->debug = arguments.size() ? std::stoi(arguments) : 1; + }); } Options::~Options() {} @@ -79,6 +81,7 @@ void Options::parse(int argc, const char *argv[]) { << "'\n"; exit(EXIT_FAILURE); case Arguments::One: + case Arguments::Optional: if (positionalsSeen) { std::cerr << "Unexpected second positional argument '" << currentOption << "' for " << positionalName << '\n'; @@ -88,6 +91,7 @@ void Options::parse(int argc, const char *argv[]) { case Arguments::N: positionalAction(this, currentOption); ++positionalsSeen; + break; } continue; } @@ -110,8 +114,8 @@ void Options::parse(int argc, const char *argv[]) { switch (option->arguments) { case Arguments::Zero: if (argument.size()) { - std::cerr << "Unexpected argument '" << argument - << "' for option '" << currentOption << "'\n"; + std::cerr << "Unexpected argument '" << argument << "' for option '" + << currentOption << "'\n"; exit(EXIT_FAILURE); } break; @@ -121,15 +125,22 @@ void Options::parse(int argc, const char *argv[]) { << currentOption << "'\n"; exit(EXIT_FAILURE); } - // Fallthrough. + // Fallthrough. case Arguments::N: if (!argument.size()) { if (i + 1 == e) { - std::cerr << "Couldn't find expected argument for '" << currentOption << "'\n"; + std::cerr << "Couldn't find expected argument for '" + << currentOption << "'\n"; exit(EXIT_FAILURE); } argument = argv[++i]; } + break; + case Arguments::Optional: + if (!argument.size()) { + if (i + 1 != e) argument = argv[++i]; + } + break; } option->action(this, argument); ++option->seen; diff --git a/src/support/command-line.h b/src/support/command-line.h index 14dbce57f..6e0af846e 100644 --- a/src/support/command-line.h +++ b/src/support/command-line.h @@ -35,9 +35,9 @@ namespace wasm { class Options { public: typedef std::function<void(Options *, const std::string &)> Action; - enum class Arguments { Zero, One, N }; + enum class Arguments { Zero, One, N, Optional }; - bool debug; + int debug; std::map<std::string, std::string> extra; Options(const std::string &command, const std::string &description); diff --git a/src/support/file.cpp b/src/support/file.cpp index 470d07737..8813750d4 100644 --- a/src/support/file.cpp +++ b/src/support/file.cpp @@ -18,7 +18,8 @@ #include <cstdlib> -std::string wasm::read_file(const std::string &filename, bool debug) { +template <typename T> +T wasm::read_file(const std::string &filename, bool debug) { if (debug) std::cerr << "Loading '" << filename << "'..." << std::endl; std::ifstream infile(filename); if (!infile.is_open()) { @@ -27,12 +28,16 @@ std::string wasm::read_file(const std::string &filename, bool debug) { } infile.seekg(0, std::ios::end); size_t insize = infile.tellg(); - std::string input(insize + 1, '\0'); + T input(insize + 1, '\0'); infile.seekg(0); infile.read(&input[0], insize); return input; } +// Explicit instantiations for the explicit specializations. +template std::string wasm::read_file<>(const std::string &, bool); +template std::vector<char> wasm::read_file<>(const std::string &, bool); + wasm::Output::Output(const std::string &filename, bool debug) : outfile(), out([this, filename, debug]() { std::streambuf *buffer; diff --git a/src/support/file.h b/src/support/file.h index e9c35f5f3..5e464d6c1 100644 --- a/src/support/file.h +++ b/src/support/file.h @@ -25,9 +25,14 @@ #include <iostream> #include <string> #include <utility> +#include <vector> namespace wasm { -std::string read_file(const std::string &filename, bool debug); +template <typename T> +T read_file(const std::string &filename, bool debug); +// Declare the valid explicit specializations. +extern template std::string read_file<>(const std::string &, bool); +extern template std::vector<char> read_file<>(const std::string &, bool); class Output { public: @@ -48,4 +53,4 @@ class Output { }; } -#endif // wasm_support_file_h +#endif // wasm_support_file_h |