-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptions.cpp
More file actions
91 lines (84 loc) · 3.3 KB
/
options.cpp
File metadata and controls
91 lines (84 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include "options.hpp"
#include <iostream>
#include "boost/program_options.hpp"
#include "boost/filesystem.hpp"
Options::Options(int argc, char *argv[])
{
std::string appName = boost::filesystem::basename(argv[0]);
boost::program_options::options_description desc("Allowed options");
desc.add_options()
("help", "Print usage")
("print-duplicates,p", boost::program_options::bool_switch()->default_value(false), "Enables print of duplicates to stdout")
("separators,s", boost::program_options::value<std::string>(), "Set separators. Default value is $' \\n'")
("input-file", boost::program_options::value< std::vector<std::string> >(), "Path to input file - mandatory argument")
("output-file", boost::program_options::value< std::vector<std::string> >(), "Path to output file - mandatory argument")
;
boost::program_options::variables_map vm;
boost::program_options::positional_options_description positionalOptions;
positionalOptions.add("input-file", 1);
positionalOptions.add("output-file", 1);
try {
boost::program_options::store(
boost::program_options::command_line_parser(argc, (const char *const *) argv).
options(desc).
positional(positionalOptions).
run(),
vm
);
if (vm.count("help")) {
this->printHelp(appName);
std::cout << desc << std::endl;
exit(0);
}
if (vm.count("input-file")) {
this->inputFileName = vm["input-file"].as< std::vector<std::string> >()[0];
}
if (vm.count("output-file")) {
this->outputFileName = vm["output-file"].as< std::vector<std::string> >()[0];
}
}
catch (boost::program_options::required_option& e) {
std::cerr << "Option ERROR: " << e.what() << std::endl << std::endl;
this->printHelp(appName);
std::cout << desc << std::endl;
exit(1);
}
catch (boost::program_options::error& e) {
std::cerr << "Option ERROR: " << e.what() << std::endl << std::endl;
this->printHelp(appName);
std::cout << desc << std::endl;
exit(1);
}
if (this->inputFileName.empty()) {
std::cerr << "Input file is missing" << std::endl << std::endl;
this->printHelp(appName);
std::cout << desc << std::endl;
exit(1);
}
if (this->outputFileName.empty()) {
std::cerr << "Output file is missing" << std::endl << std::endl;
this->printHelp(appName);
std::cout << desc << std::endl;
exit(1);
}
if (vm.count("separators")) {
this->separators = vm["separators"].as<std::string>();
}
else {
this->separators = " \n";
}
if (vm.count("print-duplicates")) {
this->printDuplicities = vm["print-duplicates"].as<bool>();
}
else {
this->printDuplicities = false;
}
}
void Options::printHelp(const std::string & appName)
{
std::cout <<
"Search in the text file for all word or sentence" << std::endl <<
"duplicates, removes them and saves the text to" << std::endl <<
"output file" << std::endl << std::endl;
std::cout << "Usage: " << appName << " [options] <input file> <output file>" << std::endl;
}