forked from victronenergy/dbus_modbustcp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharguments.cpp
61 lines (55 loc) · 1.37 KB
/
arguments.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
#include <QCoreApplication>
#include <iostream>
#include "arguments.h"
using namespace std;
Arguments::Arguments()
{
QStringList argList = QCoreApplication::arguments();
QString switchName;
mAppName = argList.at(0).section("/",-1);
const int listSize =argList.size();
for (int i = 1; i < listSize; i++) {
QString arg = argList.at(i);
if (arg.startsWith('-')) {
arg.remove(0,1);
if (arg.startsWith('-'))
arg.remove(0,1);
if (!arg.isEmpty()) {
if (switchName.isEmpty())
switchName = arg;
else {
mArgList.insert(switchName, QString());
switchName = arg;
}
}
} else {
mArgList.insert(switchName, arg);
switchName.clear();
}
}
if (!switchName.isEmpty())
mArgList.insert(switchName, QString());
}
void Arguments::print()
{
QMap<QString, QString>::const_iterator i = mArgList.constBegin();
while (i != mArgList.constEnd()) {
cout << "key = " << i.key().toStdString() << " value = " << i.value().toStdString() << "\n";
++i;
}
}
void Arguments::help()
{
cout << mAppName.toStdString() << "\n\n";
cout << "OPTIONS:" << "\n\n";
QMap<QString, QString>::iterator i;
for (i = mHelp.begin(); i != mHelp.end(); ++i) {
cout << i.key().toStdString() << "\n";
cout << "\t" << i.value().toStdString() << "\n";
}
cout << endl;
}
void Arguments::addArg(const QString & arg, const QString & description)
{
mHelp.insert(arg,description);
}