Move command line parsing code to a new file.

This commit is contained in:
King_DuckZ 2015-09-30 01:13:48 +02:00
parent c304ffbbf0
commit 05af365c58
4 changed files with 92 additions and 69 deletions

View file

@ -34,6 +34,7 @@ include_directories(
add_executable(${PROJECT_NAME} add_executable(${PROJECT_NAME}
src/main.cpp src/main.cpp
src/htmlretrieve.cpp src/htmlretrieve.cpp
src/commandline.cpp
) )
if (BUILD_SHARED_TIDY) if (BUILD_SHARED_TIDY)

65
src/commandline.cpp Normal file
View file

@ -0,0 +1,65 @@
#include "commandline.hpp"
#include "duckscraperConfig.h"
#include <boost/program_options.hpp>
#include <iostream>
#include <stdexcept>
#include <ciso646>
#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<std::string>(), "Cleans the retrieved html and saves it to the named file; use - for stdout")
("dump-raw,D", po::value<std::string>(), "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<std::string>(), "Input URL")
("xpath", po::value<std::string>(), "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...] <url> <xpath>\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

10
src/commandline.hpp Normal file
View file

@ -0,0 +1,10 @@
#ifndef id84F335333BEB461BB3FB74293DBDA4FC
#define id84F335333BEB461BB3FB74293DBDA4FC
#include <boost/program_options/variables_map.hpp>
namespace duck {
bool parse_commandline ( int parArgc, char* parArgv[], boost::program_options::variables_map& parVarMap );
} //namespace duck
#endif

View file

@ -1,4 +1,5 @@
#include "htmlretrieve.hpp" #include "htmlretrieve.hpp"
#include "commandline.hpp"
#include "duckscraperConfig.h" #include "duckscraperConfig.h"
#include <iostream> #include <iostream>
#include <string> #include <string>
@ -7,87 +8,23 @@
#include <fstream> #include <fstream>
#include <utility> #include <utility>
#include <ciso646> #include <ciso646>
#include <boost/program_options.hpp>
#include <memory> #include <memory>
#include <functional>
#include <iterator> #include <iterator>
#include <stdexcept> #include <stdexcept>
#define STRINGIZE_IMPL(s) #s
#define STRINGIZE(s) STRINGIZE_IMPL(s)
namespace po = boost::program_options;
namespace { namespace {
typedef std::pair<int, int> LineColType; typedef std::pair<int, int> LineColType;
LineColType line_col_from_offset ( ptrdiff_t parOffset, const std::string& parData ); LineColType line_col_from_offset ( ptrdiff_t parOffset, const std::string& parData );
void dump_string ( const std::string& parPathDest, 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<std::string>(), "Cleans the retrieved html and saves it to the named file; use - for stdout")
("dump-raw,D", po::value<std::string>(), "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<std::string>(), "Input URL")
("xpath", po::value<std::string>(), "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...] <url> <xpath>\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<std::ofstream> 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 } //unnamed namespace
int main (int argc, char* argv[]) { int main (int argc, char* argv[]) {
po::variables_map vm; using boost::program_options::variables_map;
variables_map vm;
try { try {
if (parse_commandline(argc, argv, vm)) { if (duck::parse_commandline(argc, argv, vm)) {
return 0; return 0;
} }
} }
@ -168,4 +105,14 @@ namespace {
} }
return std::make_pair(line, chara); return std::make_pair(line, chara);
} }
void dump_string (const std::string& parPathDest, const std::string& parData) {
std::unique_ptr<std::ofstream> 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 } //unnamed namespace