duckscraper/src/commandline.cpp

65 lines
2.1 KiB
C++

#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