-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathargparser.cpp
More file actions
106 lines (95 loc) · 3.21 KB
/
argparser.cpp
File metadata and controls
106 lines (95 loc) · 3.21 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//
// Created by Artur Laskowski on 10/11/2017.
//
#include "argparser.h"
typedef int64_t lint;
argparser::argparser::argparser() {}
argparser::argparser::~argparser() {}
std::string fill_space(int shrt_size, int full_size) {
std::string res = "";
for(int i = 40 - shrt_size - full_size - 1; i--;) {
res += ' ';
}
return res;
}
void argparser::argparser::show_usage(std::string name) {
std::cerr << "Usage: " << name << " <option(s)>\n"
<< "Options:\n"
<< " -h,--help";
for(int i = 40 - 9; i--;) {
std::cerr << " ";
}
std::cerr << "Show this help message;\n";
for(int i = 0; i < this->args_types.size(); ++i) {
std::string shrt, full, desc;
shrt = this->args_types[i].short_name;
full = this->args_types[i].long_name;
desc = this->args_types[i].description;
std::cerr << " " << shrt << "," << full << fill_space(shrt.size(), full.size()) << desc;
if(i + 1 == this->args_types.size()) {
std::cerr << ".\n";
} else {
std::cerr << ";\n";
}
}
}
bool argparser::argparser::parse_args(int argc, char* argv[]) {
for(int i = 1; i < argc; ++i) {
std::string arg = argv[i];
bool arg_found = false;
for(int j = 0; j < this->args_types.size(); ++j) {
std::string shrt, full;
shrt = this->args_types[j].short_name;
full = this->args_types[j].long_name;
bool bin = this->args_types[j].binary;
if(arg == shrt || arg == full) {
if(bin == true) {
args.insert({shrt, "true"});
args.insert({full, "true"});
arg_found = true;
break;
} else if(i + 1 < argc) {
args.insert({shrt, argv[++i]});
args.insert({full, argv[i]});
arg_found = true;
break;
}
}
}
if(!arg_found){
if((arg != "-h") && (arg != "--help")) {
std::cerr << "Unrecognized option: " << arg << std::endl;
}
show_usage(argv[0]);
return false;
}
}
for(int i = 0; i < this->args_types.size(); ++i) {
std::string shrt, full;
shrt = this->args_types[i].short_name;
full = this->args_types[i].long_name;
bool bin = this->args_types[i].binary;
if(bin == true && args.count(shrt) == 0) {
args.insert({shrt, "false"});
args.insert({full, "false"});
}
}
return true;
}
void argparser::argparser::addArgHandler(__type t) {
args_types.push_back(t);
}
bool argparser::argparser::exists(std::string argument_name) {
return args.count(argument_name);
}
std::string argparser::argparser::handle_argument(std::string param, std::string default_value) {
std::string argument_value = "";
if(this->exists(param)) {
return this->args[param];
} else if(default_value == "") {
std::cerr << "Missing " << param << " param" << std::endl;
this->show_usage("<binary>");
throw "Missing " + param + " param";
}
return default_value;
}