diff options
author | JF Bastien <jfb@chromium.org> | 2015-12-22 11:49:44 -0800 |
---|---|---|
committer | JF Bastien <jfb@chromium.org> | 2015-12-22 11:49:44 -0800 |
commit | 8c8294bad3068267940f293be9c2c26b64e66219 (patch) | |
tree | 623ab3e72bc1274393bb2e1c8404dfd58cc8ba9a /src | |
parent | 05a6eb48c1258a9d8f7cb8356e541d0fad50bc30 (diff) | |
download | binaryen-8c8294bad3068267940f293be9c2c26b64e66219.tar.gz binaryen-8c8294bad3068267940f293be9c2c26b64e66219.tar.bz2 binaryen-8c8294bad3068267940f293be9c2c26b64e66219.zip |
Move command line to its own file.
Diffstat (limited to 'src')
-rw-r--r-- | src/command-line.h | 72 | ||||
-rw-r--r-- | src/s2wasm-main.cpp | 53 |
2 files changed, 75 insertions, 50 deletions
diff --git a/src/command-line.h b/src/command-line.h new file mode 100644 index 000000000..77c10d3e7 --- /dev/null +++ b/src/command-line.h @@ -0,0 +1,72 @@ +/* + * 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. + */ + +// +// Command line helpers. +// + +#include "wasm.h" + +namespace wasm { + +struct Options { + bool debug; + std::string infile; + std::string outfile; + Options() : debug(false) {} +}; + +bool optionIs(const char *arg, const char *LongOpt, const char *ShortOpt) { + return strcmp(arg, LongOpt) == 0 || strcmp(arg, ShortOpt) == 0; +} + +// TODO(jfb) Make this configurable: callers should pass in option handlers. +void processCommandLine(int argc, const char *argv[], Options *options) { + for (size_t i = 1; i != argc; ++i) { + if (optionIs(argv[i], "--help", "-h")) { + std::cerr << "s2wasm INFILE\n\n" + "Link .s file into .wast\n\n" + "Optional arguments:\n" + " -n, --help Show this help message and exit\n" + " -d, --debug Print debug information to stderr\n" + " -o, --output Output file (stdout if not specified)\n" + << std::endl; + exit(EXIT_SUCCESS); + } else if (optionIs(argv[i], "--debug", "-d")) { + options->debug = true; + } else if (optionIs(argv[i], "--output", "-o")) { + if (i + 1 == argc) { + std::cerr << "No output file" << std::endl; + exit(EXIT_FAILURE); + } + if (options->outfile.size()) { + std::cerr << "Expected only one output file, got '" << options->outfile + << "' and '" << argv[i] << "'" << std::endl; + exit(EXIT_FAILURE); + } + options->outfile = argv[++i]; + } else { + if (options->infile.size()) { + std::cerr << "Expected only one input file, got '" << options->infile + << "' and '" << argv[i] << "'" << std::endl; + exit(EXIT_FAILURE); + } + options->infile = argv[i]; + } + } +} + +} // namespace wasm diff --git a/src/s2wasm-main.cpp b/src/s2wasm-main.cpp index 7e5b99057..cb60d2a57 100644 --- a/src/s2wasm-main.cpp +++ b/src/s2wasm-main.cpp @@ -18,68 +18,21 @@ // wasm2asm console tool // +#include "command-line.h" #include "s2wasm.h" using namespace cashew; using namespace wasm; -namespace wasm { -struct Options { - bool debug; - std::string infile; - std::string outfile; - Options() : debug(false) {} -}; - -bool optionIs(const char *arg, const char *LongOpt, const char *ShortOpt) { - return strcmp(arg, LongOpt) == 0 || strcmp(arg, ShortOpt) == 0; -} - -void processCommandLine(int argc, const char *argv[], Options *options) { - for (size_t i = 1; i != argc; ++i) { - if (optionIs(argv[i], "--help", "-h")) { - std::cerr << "s2wasm INFILE\n\n" - "Link .s file into .wast\n\n" - "Optional arguments:\n" - " -n, --help Show this help message and exit\n" - " -d, --debug Print debug information to stderr\n" - " -o, --output Output file (stdout if not specified)\n" - << std::endl; - exit(EXIT_SUCCESS); - } else if (optionIs(argv[i], "--debug", "-d")) { - options->debug = true; - } else if (optionIs(argv[i], "--output", "-o")) { - if (i + 1 == argc) { - std::cerr << "No output file" << std::endl; - exit(EXIT_FAILURE); - } - if (options->outfile.size()) { - std::cerr << "Expected only one output file, got '" << options->outfile - << "' and '" << argv[i] << "'" << std::endl; - exit(EXIT_FAILURE); - } - options->outfile = argv[++i]; - } else { - if (options->infile.size()) { - std::cerr << "Expected only one input file, got '" << options->infile - << "' and '" << argv[i] << "'" << std::endl; - exit(EXIT_FAILURE); - } - options->infile = argv[i]; - } - } -} - -} // namespace wasm - int main(int argc, const char *argv[]) { Options options; processCommandLine(argc, argv, &options); std::string input; { - if (options.debug) + if (options.debug) { std::cerr << "Loading '" << options.infile << "'..." << std::endl; + } std::ifstream infile(options.infile); if (!infile.is_open()) { std::cerr << "Failed opening '" << options.infile << "'" << std::endl; |