insilmaril@0
|
1 |
#include "options.h"
|
insilmaril@408
|
2 |
|
insilmaril@0
|
3 |
#include <iostream>
|
insilmaril@408
|
4 |
#include <QApplication>
|
insilmaril@0
|
5 |
|
insilmaril@0
|
6 |
|
insilmaril@0
|
7 |
using namespace std;
|
insilmaril@0
|
8 |
|
insilmaril@0
|
9 |
|
insilmaril@0
|
10 |
Option::Option()
|
insilmaril@0
|
11 |
{
|
insilmaril@0
|
12 |
name="";
|
insilmaril@0
|
13 |
sName="";
|
insilmaril@0
|
14 |
lName="";
|
insilmaril@474
|
15 |
type=Switch;
|
insilmaril@0
|
16 |
sarg="";
|
insilmaril@0
|
17 |
active=false;
|
insilmaril@0
|
18 |
}
|
insilmaril@0
|
19 |
|
insilmaril@474
|
20 |
Option::Option (const QString &n, const Type &t, const QString &s, const QString &l)
|
insilmaril@408
|
21 |
{
|
insilmaril@408
|
22 |
sName="-"+s;
|
insilmaril@408
|
23 |
lName="--"+l;
|
insilmaril@408
|
24 |
type=t;
|
insilmaril@408
|
25 |
name=n;
|
insilmaril@408
|
26 |
}
|
insilmaril@408
|
27 |
|
insilmaril@474
|
28 |
void Option::set(const QString &n, const Type &t, const QString &s, const QString &l)
|
insilmaril@0
|
29 |
{
|
insilmaril@0
|
30 |
sName="-"+s;
|
insilmaril@0
|
31 |
lName="--"+l;
|
insilmaril@0
|
32 |
type=t;
|
insilmaril@0
|
33 |
name=n;
|
insilmaril@0
|
34 |
}
|
insilmaril@0
|
35 |
|
insilmaril@0
|
36 |
QString Option::getName () { return name; }
|
insilmaril@0
|
37 |
QString Option::getShort () { return sName; }
|
insilmaril@0
|
38 |
QString Option::getLong() { return lName; }
|
insilmaril@474
|
39 |
Option::Type Option::getType() { return type; }
|
insilmaril@0
|
40 |
void Option::setArg(const QString& s) { sarg=s; }
|
insilmaril@0
|
41 |
QString Option::getArg() { return sarg; }
|
insilmaril@0
|
42 |
void Option::setActive() { active=true; }
|
insilmaril@0
|
43 |
bool Option::isActive() { return active; }
|
insilmaril@0
|
44 |
|
insilmaril@0
|
45 |
///////////////////////////////////////////////////////////////
|
insilmaril@0
|
46 |
Options::Options() {}
|
insilmaril@0
|
47 |
|
insilmaril@0
|
48 |
int Options::parse()
|
insilmaril@0
|
49 |
{
|
insilmaril@0
|
50 |
QStringList arglist;
|
insilmaril@0
|
51 |
int i=0;
|
insilmaril@408
|
52 |
while ( i < qApp->argc())
|
insilmaril@0
|
53 |
{
|
insilmaril@0
|
54 |
arglist.append (qApp->argv()[i]);
|
insilmaril@0
|
55 |
i++;
|
insilmaril@0
|
56 |
}
|
insilmaril@0
|
57 |
|
insilmaril@0
|
58 |
// Get program name
|
insilmaril@0
|
59 |
progname=arglist.first();
|
insilmaril@0
|
60 |
arglist.pop_front();
|
insilmaril@0
|
61 |
|
insilmaril@0
|
62 |
// Work through rest of options
|
insilmaril@0
|
63 |
bool isFile;
|
insilmaril@408
|
64 |
for (i=0; i< arglist.size(); ++i)
|
insilmaril@0
|
65 |
{
|
insilmaril@0
|
66 |
isFile=true;
|
insilmaril@408
|
67 |
if (arglist[i].left(1)=="-")
|
insilmaril@0
|
68 |
{
|
insilmaril@0
|
69 |
// Compare given option to all defined options
|
insilmaril@408
|
70 |
for (int j=0; j < optlist.size(); ++j)
|
insilmaril@0
|
71 |
{
|
insilmaril@408
|
72 |
if (arglist.at(i)==optlist.value(j).getShort() ||
|
insilmaril@408
|
73 |
arglist.at(i)==optlist.value(j).getLong())
|
insilmaril@0
|
74 |
{
|
insilmaril@408
|
75 |
optlist[j].setActive();
|
insilmaril@0
|
76 |
isFile=false;
|
insilmaril@474
|
77 |
if (optlist[j].getType()==Option::String)
|
insilmaril@0
|
78 |
{
|
insilmaril@408
|
79 |
i++;
|
insilmaril@408
|
80 |
if (i==arglist.size())
|
insilmaril@0
|
81 |
{
|
insilmaril@408
|
82 |
qWarning ("Error: argument to option missing");
|
insilmaril@0
|
83 |
return 1;
|
insilmaril@0
|
84 |
}
|
insilmaril@408
|
85 |
optlist[j].setArg (arglist[i]);
|
insilmaril@0
|
86 |
isFile=false;
|
insilmaril@0
|
87 |
}
|
insilmaril@0
|
88 |
break;
|
insilmaril@0
|
89 |
}
|
insilmaril@0
|
90 |
}
|
insilmaril@0
|
91 |
if (isFile)
|
insilmaril@0
|
92 |
{
|
insilmaril@2
|
93 |
qWarning("Error: Unknown argument ");
|
insilmaril@0
|
94 |
return 1;
|
insilmaril@0
|
95 |
}
|
insilmaril@0
|
96 |
} else
|
insilmaril@408
|
97 |
filelist.append (arglist[i]);
|
insilmaril@0
|
98 |
}
|
insilmaril@0
|
99 |
return 0;
|
insilmaril@0
|
100 |
}
|
insilmaril@0
|
101 |
|
insilmaril@408
|
102 |
void Options::add (Option o)
|
insilmaril@408
|
103 |
{
|
insilmaril@408
|
104 |
optlist.append (o);
|
insilmaril@408
|
105 |
}
|
insilmaril@408
|
106 |
|
insilmaril@474
|
107 |
void Options::add (const QString &n, const Option::Type &t=Option::Switch, const QString &s="", const QString &l="")
|
insilmaril@0
|
108 |
{
|
insilmaril@0
|
109 |
Option o;
|
insilmaril@0
|
110 |
o.set (n,t,s,l);
|
insilmaril@0
|
111 |
optlist.append (o);
|
insilmaril@0
|
112 |
}
|
insilmaril@0
|
113 |
|
insilmaril@0
|
114 |
void Options::setHelpText (const QString &s)
|
insilmaril@0
|
115 |
{
|
insilmaril@0
|
116 |
helptext=s;
|
insilmaril@0
|
117 |
}
|
insilmaril@0
|
118 |
|
insilmaril@0
|
119 |
QString Options::getHelpText ()
|
insilmaril@0
|
120 |
{
|
insilmaril@0
|
121 |
return helptext;
|
insilmaril@0
|
122 |
}
|
insilmaril@0
|
123 |
|
insilmaril@0
|
124 |
QString Options::getProgramName()
|
insilmaril@0
|
125 |
{
|
insilmaril@0
|
126 |
return progname;
|
insilmaril@0
|
127 |
}
|
insilmaril@0
|
128 |
|
insilmaril@0
|
129 |
QStringList Options::getFileList ()
|
insilmaril@0
|
130 |
{
|
insilmaril@0
|
131 |
return filelist;
|
insilmaril@0
|
132 |
}
|
insilmaril@0
|
133 |
|
insilmaril@0
|
134 |
bool Options::isOn(const QString &s)
|
insilmaril@0
|
135 |
{
|
insilmaril@408
|
136 |
for (int i=0; i<optlist.size(); ++i)
|
insilmaril@408
|
137 |
if (optlist[i].getName()==s && optlist[i].isActive() )
|
insilmaril@0
|
138 |
return true;
|
insilmaril@0
|
139 |
return false;
|
insilmaril@0
|
140 |
}
|
insilmaril@0
|
141 |
|
insilmaril@0
|
142 |
QString Options::getArg(const QString &s)
|
insilmaril@0
|
143 |
{
|
insilmaril@408
|
144 |
for (int i=0; i<optlist.size(); ++i)
|
insilmaril@408
|
145 |
if (optlist[i].getName()==s) return optlist[i].getArg();
|
insilmaril@408
|
146 |
return QString();
|
insilmaril@0
|
147 |
}
|