-
Notifications
You must be signed in to change notification settings - Fork 0
/
argumentlist.h
95 lines (82 loc) · 2.61 KB
/
argumentlist.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
#ifndef ARGUMENTLIST_H
#define ARGUMENTLIST_H
#include <QStringList>
/** @short a simple interface for processing command line arguments
An object which provides a simple interface to the command
line arguments of a program. The methods
@ref getSwitch(QString) and @ref getSwitchArg(QString)
allow you to process <b>and remove</b> the switches and switched
arguments in the command line, so that the remaining
entries in the stringlist can be processed as a
uniform list.
<br>
It also happens to be derived from
QStringList, so you can use any of those
methods on this object too.
<br>
This helps you avoid entirely using the C arrays
in your application.
<br />
Usage.:
<br />
<pre>
int main(int argc, char** argv)
{
ArgumentList args(argc, argv);
bool verbose = args.getSwitch("-v");
// get all other switches
QString outputfile = args.getSwitchArg("-o", "default.out");
qout << args[0]; // prints the name of the executable
qout << args[1]; // prints the first unswitched argument
someObject.processEveryFile(args);
}
</pre>
@author S. Alan Ezust [email protected]
@since qt 3.2.1
*/
class ArgumentList : public QStringList
{
public:
/**
retrieve argument list from the qApp->argc() and argv() methods.
Only works if a @ref QApplication(argc, argv) was already created.
*/
ArgumentList();
/**
@param argc number of arguments
@param argv an array of command line arguments, as supplied
to main().
@see argsToStringList()
*/
ArgumentList(int argc, char* argv[])
{
argsToStringlist(argc, argv);
}
ArgumentList(const QStringList& argumentList):
QStringList(argumentList) {}
/**
finds <b>and removes</b> a switch from the string list, if it exists.
@param option the switch to search for
@return true if the switch was found
*/
bool getSwitch(const QString &option);
/**
finds/removes a switch <b>and its accompanying argument</b>
from the string list, if the switch is found. Does nothing if the
switch is not found.
@param option the switch to search for
@param defaultReturnValue the return value if option is not found in the stringlist
@return the argument following option, or defaultValue if the option
is not found.
*/
QString getSwitchArg(const QString &option,
const QString &defaultRetVal=QString());
private:
/**
(Re)loads argument lists into this object. This function is private because
it is part of the implementation of the class and not intended to be used by
client code.
*/
void argsToStringlist(int argc, char* argv[]);
};
#endif