Pass title by const reference
[dueringa_WikiWalker.git] / src / GetoptCommandLineParser.cpp
blob85dda241fcd556770717a73fe9f52bd5bf15782e
1 #include "GetoptCommandLineParser.h"
3 #include <getopt.h>
5 #include <iomanip>
6 #include <iostream>
7 #include <utility>
9 #include "CommandLineParserUtils.h"
10 #include "WalkerException.h"
12 namespace WikiWalker
14 static struct option options[] = {
15 {"help", no_argument, nullptr, 'h'},
16 {"version", no_argument, nullptr, 'v'},
17 {"deep", no_argument, nullptr, 'd'},
18 {"skip-ssl-verification", no_argument, nullptr, 255},
19 {"dot-out", required_argument, nullptr, 'o'},
20 {"json-cache", required_argument, nullptr, 'j'},
21 {nullptr, 0, nullptr, 0},
24 std::string GetoptCommandLineParser::getValue(
25 CommandLineParserBase::CommandLineOptions option)
27 return setOptions.find(CommandLineParserUtils::getStringFlag(option))
28 ->second;
31 bool GetoptCommandLineParser::hasSet(
32 CommandLineParserBase::CommandLineOptions flag)
34 return setOptions.find(CommandLineParserUtils::getStringFlag(flag)) !=
35 setOptions.end();
38 void GetoptCommandLineParser::parse(int argc, char** argv)
40 opterr = 0;
41 // ignoreing long option index
42 int index = 0;
44 int opt = 0;
45 while((opt = getopt_long(argc, argv, "vhdj:o:", options, &index)) != -1) {
46 switch(opt) {
47 case 'h':
48 setOptions.insert(std::make_pair("help", "1"));
49 break;
50 case 'v':
51 setOptions.insert(std::make_pair("version", "1"));
52 break;
53 case 'd':
54 setOptions.insert(std::make_pair("deep", "1"));
55 break;
56 case 'j': {
57 setOptions.insert(std::make_pair("json-cache", optarg));
58 } break;
59 case 'o':
60 setOptions.insert(std::make_pair("dot-out", optarg));
61 break;
62 // missing or unknows
63 case '?': {
64 std::string errorMsg = "Missing argument or unknown option: -";
65 errorMsg.push_back(optopt);
67 throw WalkerException(errorMsg);
68 } break;
72 if(optind < argc) {
73 setOptions.insert(std::make_pair("url", argv[optind]));
77 static void helpFormatter(const std::string& option,
78 const std::string& description)
80 std::cout << std::setw(5) << "" << std::left << std::setw(20) << option
81 << " " << description << std::endl;
84 void GetoptCommandLineParser::printHelp()
86 std::cout << "walker usage: walker [options] <URL>" << std::endl;
87 std::cout << " walker -j <file> -o <file> [URL]" << std::endl;
88 std::cout << std::endl;
90 helpFormatter("-h, --help", "print program help");
91 helpFormatter("-v, --version", "print program version");
92 helpFormatter("-d, --deep",
93 "whether to fetch and analyze linked articles as well");
94 helpFormatter("-j, --json-cache", "cache file in JSON format");
95 helpFormatter("-o, --dot-out", "output file for dot/graphviz");
97 std::cout << std::endl;
99 std::cout << "The environment variable CURL_CA_BUNDLE is read for"
100 " finding a CA bundle"
101 << std::endl
102 << " for curl." << std::endl;
104 std::cout << std::endl;
106 } // namespace WikiWalker