-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandLine.cc
More file actions
183 lines (167 loc) · 6.15 KB
/
CommandLine.cc
File metadata and controls
183 lines (167 loc) · 6.15 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
/*
* Copyright (C) 2014 -- 2023 SZIGETI János <szigeti at pilar dot hu>
*
* This file is part of CsvTools.
*
* CsvTools is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* CsvTools is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with CsvTools. If not, see http://www.gnu.org/licenses/.
*/
#include <iostream>
#include <algorithm>
#include "CommandLine.h"
#include "stl_out.h"
constexpr char DASH_CHR = '-';
constexpr char EQ_CHR = '=';
using namespace std;
/// strict weak ordering by flag (lexicographic).
bool Option::operator<(const _Option &b) const {
return (flag && b.flag) ? std::strcmp(flag, b.flag) < 0 : (flag < b.flag);
}
ostream &operator<<(ostream &a, const Option &b) {
if (b.flag)
a << DASH_CHR << b.flag;
if (b.longname) {
a << "\t" << DASH_CHR << DASH_CHR << b.longname;
if (b.arg_num) {
for (int i = 0; i < b.arg_num; ++i) {
a << (i == 0 ? EQ_CHR : ' ') << "<arg" << (i + 1) << ">";
}
}
}
if (b.help) {
a << "\t" << b.help;
}
return a;
}
CommandLine::CommandLine(const set<Option> &a) : logger(LogConfig()), option_s(a) {
for (auto option : a) {
if (option.flag) {
flag_m.insert(make_pair(option.flag, option));
}
if (option.longname) {
longname_m.insert(make_pair(option.longname, option));
}
}
}
int CommandLine::parse(int argc, const char *argv[]) {
progname = argv[0];
int i = 1;
while (i < argc) {
const char *xcmd = argv[i];
Option xoption;
// option begins with dash
if (xcmd[0] == DASH_CHR) {
int arg_chr_shift = 0;
// double dash denotes longname option
if (xcmd[1] == DASH_CHR) {
// longname option may be followed by equals sign
const char *xcmd_e = xcmd + strlen(xcmd);
const char *eq_pos = find(xcmd, xcmd_e, EQ_CHR);
auto it = longname_m.find(string(xcmd + 2, eq_pos));
if (it == longname_m.end()) {
ERROR(logger, "Cannot interpret option [" << xcmd << "], aborting.");
return 1;
}
xoption = it->second;
if (xoption.arg_num) {
if (eq_pos == xcmd_e) {
ERROR(logger, "Option [" << xoption.longname << "] requires argument concatenated with '" << EQ_CHR << "'. Aborting.");
return 1;
}
arg_chr_shift = eq_pos - xcmd + 1;
} else {
if (eq_pos != xcmd_e) {
ERROR(logger, "Option [" << xoption.longname << "] does not require any argument concatenated with '" << EQ_CHR << "'. Aborting.");
return 1;
}
++i;
}
} else {
auto it = flag_m.find(xcmd + 1);
if (it == flag_m.end()) {
ERROR(logger, "Cannot interpret option [" << xcmd << "], skipped.");
return 1;
}
xoption = it->second;
++i;
}
vector<const char *> value_v;
for (int j = 0; j < xoption.arg_num; ++j, ++i) {
if (argc <= i) {
ERROR(logger, "Unexpected end of command line (option argument missing)");
return 1;
}
value_v.push_back(argv[i] + arg_chr_shift);
arg_chr_shift = 0;
}
// decide what to do according to multidef setting
auto found_option = value_m.find(xoption);
if (xoption.multidef == IGNORE && found_option != value_m.end()) {
WARN(logger, "Option " << xcmd << " ignored (multiple definition).");
} else if (xoption.multidef == OVERRIDE && found_option != value_m.end()) {
WARN(logger, "Option " << xcmd << " overrides previous setting (multiple definition).");
found_option->second = value_v;
} else {
value_m.insert(make_pair(xoption, value_v));
}
} else {
ERROR(logger, "Parse error at cmdline argument #" << i << ": " << xcmd);
return 1;
}
}
DEBUG(logger, "parsed: " << value_m);
parsed = true;
return 0;
}
bool CommandLine::is_set_option(const Option &a) const {
return value_m.find(a) != value_m.end();
}
bool CommandLine::is_set_flag(const char *a) const {
return a && flag_m.find(a) != flag_m.end() && is_set_option(flag_m.find(a)->second);
}
bool CommandLine::is_set_longname(const char *a) const {
return a && longname_m.find(a) != longname_m.end() && is_set_option(longname_m.find(a)->second);
}
vector<vector<const char*> > CommandLine::get_values_for_option(const Option &a) const {
vector<vector<const char*> > retv;
for (auto it = value_m.lower_bound(a); it != value_m.upper_bound(a); ++it)
retv.push_back(it->second);
return retv;
}
vector<vector<const char*> > CommandLine::get_values_for_flag(const char *a) const {
return (a && flag_m.find(a) != flag_m.end()) ? get_values_for_option(flag_m.find(a)->second) : vector<vector<const char*> >();
}
vector<vector<const char*> > CommandLine::get_values_for_longname(const char *a) const {
return (a && longname_m.find(a) != longname_m.end()) ? get_values_for_option(longname_m.find(a)->second) : vector<vector<const char*> >();
}
const char* CommandLine::get_arg_for_option(const Option &a, int i) const {
return is_set_option(a) ? value_m.find(a)->second.at(i) : NULL;
}
const char* CommandLine::get_arg_for_flag(const char *a, int i) const {
return is_set_flag(a) ? value_m.find(flag_m.find(a)->second)->second.at(i) : NULL;
}
const char* CommandLine::get_arg_for_longname(const char *a, int i) const {
return is_set_longname(a) ? value_m.find(longname_m.find(a)->second)->second.at(i) : NULL;
}
std::vector<std::string> str_split(const std::string &str, char sep, int parts) {
vector<string> retv;
int idx_b = 0;
for (int i = 0; (i < parts - 1); ++i) {
int idx_sep = str.find(sep, idx_b);
if (idx_sep == string::npos) break;
retv.push_back(str.substr(idx_b, idx_sep - idx_b));
idx_b = idx_sep + 1;
}
retv.push_back(str.substr(idx_b));
return retv;
}