From 05af365c58d5f4c006c8b3042bce0b94857cab6b Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Wed, 30 Sep 2015 01:13:48 +0200 Subject: [PATCH] Move command line parsing code to a new file. --- CMakeLists.txt | 1 + src/commandline.cpp | 65 ++++++++++++++++++++++++++++++++++ src/commandline.hpp | 10 ++++++ src/main.cpp | 85 +++++++++------------------------------------ 4 files changed, 92 insertions(+), 69 deletions(-) create mode 100644 src/commandline.cpp create mode 100644 src/commandline.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index a23308e..29d6f2b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -34,6 +34,7 @@ include_directories( add_executable(${PROJECT_NAME} src/main.cpp src/htmlretrieve.cpp + src/commandline.cpp ) if (BUILD_SHARED_TIDY) diff --git a/src/commandline.cpp b/src/commandline.cpp new file mode 100644 index 0000000..4d55418 --- /dev/null +++ b/src/commandline.cpp @@ -0,0 +1,65 @@ +#include "commandline.hpp" +#include "duckscraperConfig.h" +#include +#include +#include +#include + +#define STRINGIZE_IMPL(s) #s +#define STRINGIZE(s) STRINGIZE_IMPL(s) + +namespace po = boost::program_options; + +namespace duck { + namespace { + const char* const g_version_string = + PROGRAM_NAME " v" STRINGIZE(VERSION_MAJOR) "." STRINGIZE(VERSION_MINOR) +#if VERSION_BETA + "b" +#endif + ; + } //unnamed namespace + + bool parse_commandline (int parArgc, char* parArgv[], po::variables_map& parVarMap) { + po::options_description desc("General"); + desc.add_options() + ("help,h", "Produces this help message") + ("version", "Prints the program's version and quits") + ("dump,d", po::value(), "Cleans the retrieved html and saves it to the named file; use - for stdout") + ("dump-raw,D", po::value(), "Saves the retrieved html to the named file; use - for stdout") + ; + po::options_description positional_options("Positional options"); + positional_options.add_options() + ("input-url", po::value(), "Input URL") + ("xpath", po::value(), "XPath expression") + ; + po::options_description all("Available options"); + all.add(desc).add(positional_options); + po::positional_options_description pd; + pd.add("input-url", 1).add("xpath", 1); + po::store(po::command_line_parser(parArgc, parArgv).options(all).positional(pd).run(), parVarMap); + po::notify(parVarMap); + + if (parVarMap.count("help")) { + po::options_description visible("Available options"); + visible.add(desc); + std::cout << "Usage: " << PROGRAM_NAME << " [options...] \n"; + std::cout << "You can pass - as the url to read from stdin\n"; + std::cout << visible; + return true; + } + else if (parVarMap.count("version")) { + std::cout << g_version_string; + std::cout << " git revision " << VERSION_GIT << "\n"; + return true; + } + + if (parVarMap.count("input-url") == 0) { + throw std::invalid_argument("No input URL specified"); + } + if (parVarMap.count("xpath") == 0) { + throw std::invalid_argument("No XPath expression specified"); + } + return false; + } +} //namespace duck diff --git a/src/commandline.hpp b/src/commandline.hpp new file mode 100644 index 0000000..c3a03df --- /dev/null +++ b/src/commandline.hpp @@ -0,0 +1,10 @@ +#ifndef id84F335333BEB461BB3FB74293DBDA4FC +#define id84F335333BEB461BB3FB74293DBDA4FC + +#include + +namespace duck { + bool parse_commandline ( int parArgc, char* parArgv[], boost::program_options::variables_map& parVarMap ); +} //namespace duck + +#endif diff --git a/src/main.cpp b/src/main.cpp index c0ce648..568d9e1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,4 +1,5 @@ #include "htmlretrieve.hpp" +#include "commandline.hpp" #include "duckscraperConfig.h" #include #include @@ -7,87 +8,23 @@ #include #include #include -#include #include -#include #include #include -#define STRINGIZE_IMPL(s) #s -#define STRINGIZE(s) STRINGIZE_IMPL(s) - -namespace po = boost::program_options; - namespace { typedef std::pair LineColType; LineColType line_col_from_offset ( ptrdiff_t parOffset, const std::string& parData ); - - const char* const g_version_string = - PROGRAM_NAME " v" STRINGIZE(VERSION_MAJOR) "." STRINGIZE(VERSION_MINOR) -#if VERSION_BETA - "b" -#endif - ; - - bool parse_commandline (int parArgc, char* parArgv[], po::variables_map& parVarMap) { - po::options_description desc("General"); - desc.add_options() - ("help,h", "Produces this help message") - ("version", "Prints the program's version and quits") - ("dump,d", po::value(), "Cleans the retrieved html and saves it to the named file; use - for stdout") - ("dump-raw,D", po::value(), "Saves the retrieved html to the named file; use - for stdout") - ; - po::options_description positional_options("Positional options"); - positional_options.add_options() - ("input-url", po::value(), "Input URL") - ("xpath", po::value(), "XPath expression") - ; - po::options_description all("Available options"); - all.add(desc).add(positional_options); - po::positional_options_description pd; - pd.add("input-url", 1).add("xpath", 1); - po::store(po::command_line_parser(parArgc, parArgv).options(all).positional(pd).run(), parVarMap); - po::notify(parVarMap); - - if (parVarMap.count("help")) { - po::options_description visible("Available options"); - visible.add(desc); - std::cout << "Usage: " << PROGRAM_NAME << " [options...] \n"; - std::cout << "You can pass - as the url to read from stdin\n"; - std::cout << visible; - return true; - } - else if (parVarMap.count("version")) { - std::cout << g_version_string; - std::cout << " git revision " << VERSION_GIT << "\n"; - return true; - } - - if (parVarMap.count("input-url") == 0) { - throw std::invalid_argument("No input URL specified"); - } - if (parVarMap.count("xpath") == 0) { - throw std::invalid_argument("No XPath expression specified"); - } - return false; - } - - void dump_string (const std::string& parPathDest, const std::string& parData) { - std::unique_ptr ofs; - const bool use_stdout = ("-" == parPathDest); - if (not use_stdout) { - ofs.reset(new std::ofstream(parPathDest)); - } - std::ostream* const os = (use_stdout ? &std::cout : ofs.get()); - *os << parData; - } + void dump_string ( const std::string& parPathDest, const std::string& parData ); } //unnamed namespace int main (int argc, char* argv[]) { - po::variables_map vm; + using boost::program_options::variables_map; + + variables_map vm; try { - if (parse_commandline(argc, argv, vm)) { + if (duck::parse_commandline(argc, argv, vm)) { return 0; } } @@ -168,4 +105,14 @@ namespace { } return std::make_pair(line, chara); } + + void dump_string (const std::string& parPathDest, const std::string& parData) { + std::unique_ptr ofs; + const bool use_stdout = ("-" == parPathDest); + if (not use_stdout) { + ofs.reset(new std::ofstream(parPathDest)); + } + std::ostream* const os = (use_stdout ? &std::cout : ofs.get()); + *os << parData; + } } //unnamed namespace