-
Notifications
You must be signed in to change notification settings - Fork 0
/
commandLineOptions.h
executable file
·182 lines (159 loc) · 5 KB
/
commandLineOptions.h
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
//-*-c++-*-
#pragma once
#ifndef COMMANDLINEOPTIONS_H
#define COMMANDLINEOPTIONS_H
#include <QCommandLineParser>
#include <QList>
#include <QVariant>
#include <QDebug>
#include <iostream>
///////////////////////////////////////////////////////////////////////////
//
// This is a Qt command line processor
//
// To use:
// CommandLineOptions options;
// options.add(<name of option, no dashes>, <QVariant variable>)
// options.parse()
//
// 1. Dashes are thrown out, so you can use either -d or --d.
// 2. You can also use an equals assignment, e.g -d=100
// 3. A param with no argument should only be used with boolean
// variants. A boolean variant passed with a -<param> will be
// inverted (e.g. just "-d"). You can also assign it using
// the "=" option, e.g. "-d=false".
// 4. Variables other then boolean should always have an argument
// else it's an error, e.g. -f 3.14159
//
// See example at bottom.
//
///////////////////////////////////////////////////////////////////////////
class CommandLineOptions {
struct Options {
QString _name;
QVariant *_result;
};
QList<Options> _options;
public:
CommandLineOptions() {}
~CommandLineOptions() { _options.clear();}
//
// add an option
//
void add(const char *name, QVariant *result) {
Options s;
s._name = name;
s._result = result;
_options.push_back(s);
}
void parse(void) {
processCommandLine();
}
private:
//
// check for argument of the form "x=100"
// call with command line arg, the name of the option,
// and the result if match found
// e.g. checkEqualsArg(arg,"x",xarg)
//
bool checkEqualsArg(const QString &arg
,const QString &option_in
,QVariant *result
) {
QString option = QString(option_in) + "=";
if (arg.startsWith(option, Qt::CaseInsensitive)) {
QStringList parts = arg.split("=");
if (parts.count() > 1) {
QString value = parts[1];
// this will work with a boolean, no need to check!
*result = value;
return true;
}
}
return false;
}
void processCommandLine(void) {
QStringList args = QCoreApplication::arguments();
QVariant *variantPointer = nullptr;
//
// first arg will be the program name
//
foreach(QString arg, args) {
//
// waiting for an argument to a "-<value>". variantPointer
// should be pointing to the variable found below
//
if (variantPointer != nullptr) {
*variantPointer = arg;
variantPointer = nullptr;
}
//
// remove all "-"'s and leave the name only
//
QString tmp = arg;
while (tmp.startsWith("-")) {
tmp.remove(0,1); // remove the '-'
}
//
// See if each option matches up with the command
// line argument
//
for(int i=0; i<_options.size(); ++i) {
Options s = _options.at(i);
//
// Found a match
// If the variant variable is a boolean, invert it,
// else just assign the value.
//
if (s._name == tmp) {
#if QT_VERSION > QT_VERSION_CHECK(6, 0, 0)
if (s._result->typeId() == QMetaType::Bool ) {
#else
if (s._result->type() == QVariant::Bool ) {
#endif
bool b =(*s._result).toBool();
*s._result = !b;
} else {
variantPointer = s._result;
}
break;
}
//
// process all <letter>=<value> args
//
if (checkEqualsArg(tmp
,s._name
,s._result)) {
break;
}
}
}
}
};
#ifdef COMMANDLINEOPTIONS_EXAMPLE
#include <QCoreApplication>
#include <QVariant>
#include <iostream>
#include "commandLineOptions.h"
int main(int argc, char * argv[])
{
using namespace std;
QVariant stringVar = "hello";
QVariant boolVar = false;
QVariant intVar = 30;
QVariant floatVar = 3.14159;
QCoreApplication app(argc,argv);
CommandLineOptions options;
options.add("s",&stringVar);
options.add("b",&boolVar);
options.add("i",&intVar);
options.add("f",&floatVar);
options.parse();
cout << qPrintable(stringVar.toString()) << endl;
cout << boolVar.toBool() << endl; // displays 0 or 1
cout << intVar.toInt() << endl;
cout << floatVar.toFloat() << endl; // or toDouble
return 0;
}
#endif
#endif //COMMANDLINEOPTIONS_H