-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmdline_parser.h
More file actions
214 lines (189 loc) · 6.81 KB
/
cmdline_parser.h
File metadata and controls
214 lines (189 loc) · 6.81 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#ifndef CMDLINE_PARSER_H
#define CMDLINE_PARSER_H
#include <iostream>
#include <map>
#include <string>
#include <vector>
class CmdLineParser
{
public:
explicit CmdLineParser(const std::string &description = "")
: description_(description)
, version_string_("")
{}
void set_version(const std::string &version) { version_string_ = version; }
void add_option(
const std::string &short_opt,
const std::string &long_opt,
const std::string &help,
bool has_value = false,
const std::string &default_value = "")
{
Option opt;
opt.short_opt = short_opt;
opt.long_opt = long_opt;
opt.help = help;
opt.has_value = has_value;
opt.has_optional_value = false;
opt.default_value = default_value;
opt.is_set = false;
options_.push_back(opt);
}
void add_option_with_optional_value(
const std::string &short_opt,
const std::string &long_opt,
const std::string &help,
const std::string &default_value = "")
{
Option opt;
opt.short_opt = short_opt;
opt.long_opt = long_opt;
opt.help = help;
opt.has_value = true;
opt.has_optional_value = true;
opt.default_value = default_value;
opt.is_set = false;
options_.push_back(opt);
}
bool parse(int argc, const char *const argv[])
{
program_name_ = argv[0];
for (int i = 1; i < argc; i++) {
std::string arg = argv[i];
if (arg == "--help" || arg == "-h") {
print_help();
return false;
}
if (arg == "--version" || arg == "-v") {
print_version();
return false;
}
bool option_found = false;
// Check for --option=value format
size_t eq_pos = arg.find('=');
if (eq_pos != std::string::npos) {
std::string option_part = arg.substr(0, eq_pos);
std::string option_value = arg.substr(eq_pos + 1);
for (auto &opt : options_) {
if (option_part == "--" + opt.long_opt || option_part == "-" + opt.short_opt) {
opt.is_set = true;
opt.value = option_value;
option_found = true;
break;
}
}
}
// Check for short and long options
else {
for (auto &opt : options_) {
if (arg == "--" + opt.long_opt || arg == "-" + opt.short_opt) {
opt.is_set = true;
if (opt.has_value && !opt.has_optional_value) {
// Mandatory value
if (i + 1 >= argc) {
std::cerr << "Error: Option " << arg << " requires a value"
<< std::endl;
return false;
}
opt.value = argv[++i];
} else if (opt.has_optional_value) {
// Optional value - check if next arg is a value or another option
if (i + 1 < argc && argv[i + 1][0] != '-') {
opt.value = argv[++i];
} else {
opt.value = opt.default_value; // Use default if no value provided
}
}
option_found = true;
break;
}
}
}
if (!option_found) {
// Assume it's a positional argument
positional_args_.push_back(arg);
}
}
return true;
}
bool is_set(const std::string &long_opt) const
{
for (const auto &opt : options_) {
if (opt.long_opt == long_opt) {
return opt.is_set;
}
}
return false;
}
std::string get_value(const std::string &long_opt) const
{
for (const auto &opt : options_) {
if (opt.long_opt == long_opt) {
return opt.is_set ? opt.value : opt.default_value;
}
}
return "";
}
const std::vector<std::string> &get_positional_args() const { return positional_args_; }
void print_help() const
{
std::cout << description_ << std::endl;
// Extract program name from path
std::string prog_name = program_name_;
size_t last_slash = prog_name.find_last_of("/\\");
if (last_slash != std::string::npos) {
prog_name = prog_name.substr(last_slash + 1);
}
std::cout << "\nUsage: " << prog_name << " <input_file.rdl> [options]" << std::endl;
std::cout << "\nOptions:" << std::endl;
for (const auto &opt : options_) {
if (opt.short_opt.empty()) {
std::cout << " --" << opt.long_opt;
} else {
std::cout << " -" << opt.short_opt << ", --" << opt.long_opt;
}
if (opt.has_value && !opt.has_optional_value) {
std::cout << " <value>";
} else if (opt.has_optional_value) {
std::cout << "[=<value>]";
}
std::cout << "\t" << opt.help;
if (!opt.default_value.empty()) {
std::cout << " (default: " << opt.default_value << ")";
}
std::cout << std::endl;
}
}
void print_version() const
{
if (!version_string_.empty()) {
std::cout << version_string_ << std::endl;
} else {
// Extract program name from path
std::string prog_name = program_name_;
size_t last_slash = prog_name.find_last_of("/\\");
if (last_slash != std::string::npos) {
prog_name = prog_name.substr(last_slash + 1);
}
std::cout << prog_name << " version information not available" << std::endl;
}
}
private:
struct Option
{
std::string short_opt;
std::string long_opt;
std::string help;
bool has_value;
bool has_optional_value;
std::string default_value;
bool is_set;
std::string value;
};
std::string description_;
std::string program_name_;
std::string version_string_;
std::vector<Option> options_;
std::vector<std::string> positional_args_;
};
#endif // CMDLINE_PARSER_H