From 6ac94ffad050890ca47877df10d769a744f0cfb2 Mon Sep 17 00:00:00 2001 From: Lefteris Karapetsas Date: Mon, 8 Dec 2014 12:42:29 +0100 Subject: [PATCH] using boost::program_options for argument parsing --- solc/CMakeLists.txt | 1 + solc/main.cpp | 59 ++++++++++++++++++++++++--------------------- 2 files changed, 33 insertions(+), 27 deletions(-) diff --git a/solc/CMakeLists.txt b/solc/CMakeLists.txt index 386d4a1a8..689700a64 100644 --- a/solc/CMakeLists.txt +++ b/solc/CMakeLists.txt @@ -8,6 +8,7 @@ set(EXECUTABLE solc) add_executable(${EXECUTABLE} ${SRC_LIST}) +target_link_libraries(${EXECUTABLE} boost_program_options) target_link_libraries(${EXECUTABLE} solidity) install( TARGETS ${EXECUTABLE} DESTINATION bin ) diff --git a/solc/main.cpp b/solc/main.cpp index 61f73b45d..1f8348746 100644 --- a/solc/main.cpp +++ b/solc/main.cpp @@ -23,6 +23,9 @@ #include #include +#include + +#include "BuildInfo.h" #include #include #include @@ -38,43 +41,45 @@ using namespace std; using namespace dev; using namespace solidity; - -void help() -{ - cout << "Usage solc [OPTIONS] " << endl - << "Options:" << endl - << " -o,--optimize Optimize the bytecode for size." << endl - << " -h,--help Show this help message and exit." << endl - << " -V,--version Show the version and exit." << endl; - exit(0); -} +namespace po = boost::program_options; void version() { cout << "solc, the solidity complier commandline interface " << dev::Version << endl - << " by Christian , (c) 2014." << endl + << " by Christian and Lefteris , (c) 2014." << endl << "Build: " << DEV_QUOTED(ETH_BUILD_PLATFORM) << "/" << DEV_QUOTED(ETH_BUILD_TYPE) << endl; exit(0); } int main(int argc, char** argv) { - vector infiles; - bool optimize = false; - for (int i = 1; i < argc; ++i) - { - string arg = argv[i]; - if (arg == "-o" || arg == "--optimize") - optimize = true; - else if (arg == "-h" || arg == "--help") - help(); - else if (arg == "-V" || arg == "--version") - version(); - else - infiles.push_back(argv[i]); + // Declare the supported options. + po::options_description desc("Allowed options"); + desc.add_options() + ("help", "Show help message and exit") + ("version", "Show version and exit") + ("optimize", po::value()->default_value(false), "Optimize bytecode for size") + ("input-file", po::value>(), "input file"); + + // All positional options should be interpreted as input files + po::positional_options_description p; + p.add("input-file", -1); + + po::variables_map vm; + po::store(po::command_line_parser(argc, argv).options(desc).positional(p).run(), vm); + po::notify(vm); + + if (vm.count("help")) { + cout << desc; + return 0; + } + + if (vm.count("version")) { + version(); + return 0; } map sourceCodes; - if (infiles.empty()) + if (!vm.count("input-file")) { string s; while (!cin.eof()) @@ -84,7 +89,7 @@ int main(int argc, char** argv) } } else - for (string const& infile: infiles) + for (string const& infile: vm["input-file"].as>()) sourceCodes[infile] = asString(dev::contents(infile)); CompilerStack compiler; @@ -92,7 +97,7 @@ int main(int argc, char** argv) { for (auto const& sourceCode: sourceCodes) compiler.addSource(sourceCode.first, sourceCode.second); - compiler.compile(optimize); + compiler.compile( vm["optimize"].as()); } catch (ParserError const& exception) {