-
Notifications
You must be signed in to change notification settings - Fork 2
/
params.cpp
362 lines (274 loc) · 8.58 KB
/
params.cpp
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*
* Copyright (C) 2018-2024 QuasarApp.
* Distributed under the lgplv3 software license, see the accompanying
* Everyone is permitted to copy and distribute verbatim copies
* of this license document, but changing it is not allowed.
*/
#include "params.h"
#include <QVariantMap>
#include <QDebug>
#include <QFileInfo>
#include <QDateTime>
#include <QCoreApplication>
#include "qaglobalutils.h"
#include <QtLogging>
#ifdef Q_OS_DARWIN
#include <mach-o/dyld.h>
#endif
#ifdef Q_OS_WIN
#include "windows.h"
#else
#include <unistd.h>
#endif
using namespace QuasarAppUtils;
QMap<QString, QString> Params::params = QMap<QString, QString>();
QString Params::appPath = "";
QString Params::appName = "";
Help::Section Params::userHelp = {};
OptionsDataList Params::inputOptions = {};
bool Params::isEndable(const QString& key) {
return params.contains(key);
}
void Params::log(const QString &log, VerboseLvl vLvl) {
auto lvl = getVerboseLvl();
if (vLvl <= lvl) {
switch (vLvl) {
case VerboseLvl::Error:
qCritical().noquote() << log;
break;
case VerboseLvl::Warning: {
qWarning().noquote() << log;
break;
}
case VerboseLvl::Debug: {
qDebug().noquote() << log;
break;
}
case VerboseLvl::Info:
default: {
qInfo().noquote() << log;
break;
}
}
}
}
VerboseLvl Params::getVerboseLvl() {
return static_cast<VerboseLvl>(getArg("verbose", DEFAULT_VERBOSE_LVL).toInt());
}
bool Params::isDebug() {
return getVerboseLvl() >= VerboseLvl::Debug;
}
bool Params::isDebugBuild() {
#ifdef QT_DEBUG
return true;
#else
return false;
#endif
}
void Params::showHelp() {
if (inputOptions.size() > 1) {
showHelpForInputOptions();
} else {
Help::print(userHelp);
}
}
void Params::showHelpForInputOptions() {
Help::print(getHelpOfInputOptions());
}
Help::Section Params::getHelpOfInputOptions() {
if (inputOptions.size() <= 1 ) {
return {};
}
Help::Options help;
for (const auto &optionData: std::as_const(inputOptions) ) {
help.unite(optionData.toHelp());
}
return Help::Section{{"Information about using options", help}};
}
const Help::Section &Params::getHelp() {
return userHelp;
}
const QMap<QString, QString>& Params::getUserParamsMap() {
return params;
}
void Params::clearParsedData() {
params.clear();
appPath = "";
appName = "";
}
QString Params::getCurrentExecutable() {
return getCurrentExecutableDir() + "/" + appName;
}
QString Params::getCurrentExecutableDir() {
return appPath;
}
OptionsDataList Params::availableArguments() {
return OptionsDataList{
{
"Base Options",
OptionData{
{"-verbose"}, "(level 1 - 3)", "Shows debug log"
}
},
{
"Base Options",
OptionData{
{"-fileLog"}, "(path to file)", "Sets path of log file. Default it is path to executable file with suffix '.log'"
}
}
};
}
int Params::size() {
return params.size();
}
bool Params::optionsForEach(const QStringList ¶msArray,
const OptionsDataList& availableOptions) {
for (int i = 0 ; i < paramsArray.size(); ++i) {
QStringList virtualOptionsList = paramsArray[i].split('=');
if (virtualOptionsList.size() > 1) {
return optionsForEach(virtualOptionsList, availableOptions);
}
auto optionData = availableOptions.value(paramsArray[i], {{}});
if (!checkOption(optionData, paramsArray[i])) {
return false;
}
inputOptions.insert(paramsArray[i], optionData);
if (paramsArray[i][0] == '-') {
if (i < (paramsArray.size() - 1) && paramsArray[i + 1][0] != '-') {
params[paramsArray[i].mid(1)] = paramsArray[i + 1];
i++;
} else {
qCritical() << "Missing argument for " + paramsArray[i];
return false;
}
} else {
params[paramsArray[i]] = "";
}
}
return true;
}
bool Params::parseParams(const int argc, const char *argv[], const OptionsDataList& options) {
QStringList params;
for (int i = 1; i < argc; i++) {
params.push_back(argv[i]);
}
return parseParams(params, options);
}
bool Params::parseParams(int argc, char *argv[], const OptionsDataList& options) {
return parseParams(argc, const_cast<const char**>(argv), options);
}
bool Params::parseParams(const QStringList ¶msArray, const OptionsDataList &options) {
params.clear();
OptionsDataList availableOptions;
parseAvailableOptions(OptionsDataList{}.unite(options).unite(availableArguments()),
&availableOptions,
&userHelp);
#ifdef Q_OS_WIN
char buffer[MAX_PATH];
memset(buffer, 0, sizeof buffer);
GetModuleFileNameA(nullptr, buffer, MAX_PATH);
appPath = QFileInfo(buffer).absolutePath();
appName = QFileInfo(buffer).fileName();
#endif
#ifdef Q_OS_LINUX
char path[2048];
memset(path, 0, sizeof path);
if (readlink("/proc/self/exe", path, 2048) < 0) {
qCritical() << "parseParams can't get self path!";
return false;
}
appPath = QFileInfo(path).absolutePath();
appName = QFileInfo(path).fileName();
#endif
#ifdef Q_OS_DARWIN
uint32_t size = 0;
_NSGetExecutablePath(nullptr, &size); // request to buffer size
std::vector<char> buffer(size);
if (_NSGetExecutablePath(buffer.data(), &size) == 0) {
appPath = QString::fromUtf8(buffer.data());
} else {
appPath = QString();
}
appName = QCoreApplication::applicationName();
#endif
if (!appPath.size()) {
return false;
}
if (!optionsForEach(paramsArray, availableOptions)) {
return false;
}
printWorkingOptions();
return true;
}
void Params::printWorkingOptions() {
qDebug() << "--- Working options table start ---";
QMap<QString, QString>::const_iterator iter = params.constBegin();
while (iter != params.constEnd()) {
QString row = QString{"Option[%0]"}.arg(iter.key());
QString value = iter.value();
if (!value.isEmpty()) {
row += QString{": %1"}.arg(value);
}
qDebug() << row;
++iter;
}
qDebug() << "--- Working options table end ---";
}
bool Params::checkOption(const OptionData& optionData, const QString& rawOptionName) {
#ifndef QA_ALLOW_NOT_SUPPORTED_OPTIONS
if (!optionData.isValid()) {
qCritical() << QString("The '%0' option not exists!"
" You use wrong option name,"
" please check the help before run your commnad.").
arg(rawOptionName);
return false;
}
#else
Q_UNUSED(rawOptionName);
#endif
if (optionData.isDepricated()) {
if (optionData.isRemoved()) {
qCritical() << optionData.depricatedMsg();
return false;
}
qWarning() << QString("The %0 option(s) marked as deprecated! "
"And most likely will be removed in next release.").
arg(optionData.names().join("/"));
qWarning() << QString("Option message: %0").arg(optionData.depricatedMsg());
}
return true;
}
void Params::parseAvailableOptions(const OptionsDataList &availableOptionsListIn,
OptionsDataList *availableOptionsListOut,
Help::Section *helpOut) {
if (!(availableOptionsListOut && helpOut))
return;
helpOut->clear();
QHash<QString, Help::Options> options;
for (auto it = availableOptionsListIn.begin(); it != availableOptionsListIn.end(); ++it) {
if (availableOptionsListOut) {
for (const auto &name : std::as_const(it.value().names())) {
availableOptionsListOut->insert(name, it.value());
}
}
if (helpOut) {
options[it.key()].unite(it.value().toHelp());
}
}
for (auto it = options.begin(); it != options.end(); ++it) {
helpOut->insert(it.key(), it.value());
}
}
QString Params::getArg(const QString& key,const QString& def) {
return params.value(key, def);
}
void Params::setArg(const QString &key, const QString &val) {
params.insert(key, val);
}
void Params::setEnable(const QString &key, bool enable) {
if (enable) {
params.insert(key, "");
} else {
params.remove(key);
}
}