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
|
#include "options.h"
#include <iostream>
#include <qapplication.h>
using namespace std;
Option::Option()
{
name="";
sName="";
lName="";
type=SwitchOption;
sarg="";
active=false;
}
void Option::set(const QString &n, const OptionType &t, const QString &s, const QString &l)
{
sName="-"+s;
lName="--"+l;
type=t;
name=n;
}
QString Option::getName () { return name; }
QString Option::getShort () { return sName; }
QString Option::getLong() { return lName; }
OptionType Option::getType() { return type; }
void Option::setArg(const QString& s) { sarg=s; }
QString Option::getArg() { return sarg; }
void Option::setActive() { active=true; }
bool Option::isActive() { return active; }
///////////////////////////////////////////////////////////////
Options::Options() {}
int Options::parse()
{
QStringList arglist;
int i=0;
while (i<qApp->argc())
{
arglist.append (qApp->argv()[i]);
i++;
}
// Get program name
progname=arglist.first();
arglist.pop_front();
// Work through rest of options
bool isFile;
OptionList::iterator itopt;
QStringList::iterator itarg;
itarg=arglist.begin();
while (itarg!=arglist.end())
{
isFile=true;
if ((*itarg).left(1)=="-")
{
// Compare given option to all defined options
itopt=optlist.begin();
while (itopt!=optlist.end())
{
if ((*itarg)==(*itopt).getShort() ||
(*itarg)==(*itopt).getLong())
{
(*itopt).setActive();
isFile=false;
if ((*itopt).getType()==StringOption)
{
itarg++;
if (itarg==arglist.end())
{
cout << "Error: argument to option missing\n";
return 1;
}
(*itopt).setArg (*itarg);
isFile=false;
}
break;
}
itopt++;
}
if (isFile)
{
cout << "Error: Unknown argument "<<*itarg<<endl;
return 1;
}
} else
filelist.append (*itarg);
itarg++;
}
return 0;
}
void Options::add (const QString &n, const OptionType &t=SwitchOption, const QString &s="", const QString &l="")
{
Option o;
o.set (n,t,s,l);
optlist.append (o);
}
void Options::setHelpText (const QString &s)
{
helptext=s;
}
QString Options::getHelpText ()
{
return helptext;
}
QString Options::getProgramName()
{
return progname;
}
QStringList Options::getFileList ()
{
return filelist;
}
bool Options::isOn(const QString &s)
{
OptionList::iterator it;
for ( it = optlist.begin(); it != optlist.end(); ++it )
if ((*it).getName()==s && (*it).isActive() )
return true;
return false;
}
QString Options::getArg(const QString &s)
{
OptionList::iterator it;
for ( it = optlist.begin(); it != optlist.end(); ++it )
{
if ((*it).getName()==s)
return (*it).getArg();
}
return "";
}
|