options.cpp
branchrelease-1-12-maintained
changeset 79 32f499cbe874
parent 0 7a96bd401351
     1.1 --- a/options.cpp	Sun Jan 30 12:58:47 2005 +0000
     1.2 +++ b/options.cpp	Fri Mar 05 19:15:08 2010 +0000
     1.3 @@ -1,7 +1,8 @@
     1.4  #include "options.h"
     1.5 +
     1.6  #include <iostream>
     1.7 +#include <QApplication>
     1.8  
     1.9 -#include <qapplication.h>
    1.10  
    1.11  using namespace std;
    1.12  
    1.13 @@ -11,12 +12,20 @@
    1.14  	name="";
    1.15  	sName="";
    1.16  	lName="";
    1.17 -	type=SwitchOption;
    1.18 +	type=Switch;
    1.19  	sarg="";
    1.20  	active=false;
    1.21  }
    1.22  
    1.23 -void Option::set(const QString &n, const OptionType &t, const QString &s, const QString &l)
    1.24 +Option::Option (const QString &n, const Type &t, const QString &s, const QString &l)
    1.25 +{
    1.26 +	sName="-"+s;
    1.27 +	lName="--"+l;
    1.28 +	type=t;
    1.29 +	name=n;
    1.30 +}
    1.31 +
    1.32 +void Option::set(const QString &n, const Type &t, const QString &s, const QString &l)
    1.33  {
    1.34  	sName="-"+s;
    1.35  	lName="--"+l;
    1.36 @@ -27,7 +36,7 @@
    1.37  QString Option::getName () { return name; }
    1.38  QString Option::getShort () { return sName; }
    1.39  QString Option::getLong() { return lName; }
    1.40 -OptionType Option::getType() { return type; }
    1.41 +Option::Type Option::getType() { return type; }
    1.42  void Option::setArg(const QString& s) { sarg=s; }
    1.43  QString Option::getArg() { return sarg; }	
    1.44  void Option::setActive() { active=true; }	
    1.45 @@ -38,13 +47,7 @@
    1.46  
    1.47  int Options::parse()
    1.48  {
    1.49 -	QStringList arglist;
    1.50 -	int i=0;
    1.51 -	while (i<qApp->argc())
    1.52 -	{	
    1.53 -		arglist.append (qApp->argv()[i]);
    1.54 -		i++;
    1.55 -	}
    1.56 +	QStringList arglist=qApp->arguments();
    1.57  
    1.58  	// Get program name
    1.59  	progname=arglist.first();
    1.60 @@ -52,51 +55,50 @@
    1.61  
    1.62  	// Work through rest of options
    1.63  	bool isFile;
    1.64 -	OptionList::iterator itopt;
    1.65 -	QStringList::iterator itarg;
    1.66 -	itarg=arglist.begin();
    1.67 -	while (itarg!=arglist.end())
    1.68 +	for (int i=0; i< arglist.size(); ++i)
    1.69  	{
    1.70  		isFile=true;
    1.71 -		if ((*itarg).left(1)=="-")
    1.72 +		if (arglist[i].left(1)=="-")
    1.73  		{
    1.74  			// Compare given option to all defined options
    1.75 -			itopt=optlist.begin();
    1.76 -			while (itopt!=optlist.end())
    1.77 +			for (int j=0; j < optlist.size(); ++j)
    1.78  			{
    1.79 -				if ((*itarg)==(*itopt).getShort() || 
    1.80 -					(*itarg)==(*itopt).getLong())
    1.81 +				if (arglist.at(i)==optlist.value(j).getShort() || 
    1.82 +					arglist.at(i)==optlist.value(j).getLong())
    1.83  				{	
    1.84 -					(*itopt).setActive();
    1.85 +					optlist[j].setActive();
    1.86  					isFile=false;
    1.87 -					if ((*itopt).getType()==StringOption)
    1.88 +					if (optlist[j].getType()==Option::String)
    1.89  					{
    1.90 -						itarg++;
    1.91 -						if (itarg==arglist.end())
    1.92 +						i++;
    1.93 +						if (i==arglist.size())
    1.94  						{
    1.95 -							cout << "Error: argument to option missing\n";
    1.96 +							qWarning ("Error: argument to option missing");
    1.97  							return 1;
    1.98  						}
    1.99 -						(*itopt).setArg (*itarg);
   1.100 +						optlist[j].setArg (arglist[i]);
   1.101  						isFile=false;
   1.102  					}
   1.103  					break;
   1.104  				} 
   1.105 -				itopt++;
   1.106  			}
   1.107  			if (isFile)
   1.108  			{
   1.109 -				cout << "Error: Unknown argument "<<*itarg<<endl;
   1.110 +				qWarning("Error: Unknown argument ");
   1.111  				return 1;
   1.112  			}
   1.113  		} else
   1.114 -			filelist.append (*itarg);
   1.115 -		itarg++;
   1.116 +			filelist.append (arglist[i]);
   1.117  	}
   1.118  	return 0;
   1.119  }
   1.120  
   1.121 -void Options::add (const QString &n, const OptionType &t=SwitchOption, const QString &s="", const QString &l="")
   1.122 +void Options::add (Option o)
   1.123 +{
   1.124 +	optlist.append (o);
   1.125 +}
   1.126 +
   1.127 +void Options::add (const QString &n, const Option::Type &t=Option::Switch, const QString &s="", const QString &l="")
   1.128  {
   1.129  	Option o;
   1.130  	o.set (n,t,s,l);
   1.131 @@ -125,20 +127,15 @@
   1.132  
   1.133  bool Options::isOn(const QString &s)
   1.134  {
   1.135 -	OptionList::iterator it;
   1.136 -	for ( it = optlist.begin(); it != optlist.end(); ++it )
   1.137 -		if ((*it).getName()==s && (*it).isActive() )
   1.138 +	for (int i=0; i<optlist.size(); ++i)
   1.139 +		if (optlist[i].getName()==s && optlist[i].isActive() )
   1.140  			return true;
   1.141  	return false;
   1.142  }
   1.143  
   1.144  QString Options::getArg(const QString &s)
   1.145  {
   1.146 -	OptionList::iterator it;
   1.147 -	for ( it = optlist.begin(); it != optlist.end(); ++it )
   1.148 -	{
   1.149 -		if ((*it).getName()==s)
   1.150 -			return (*it).getArg();
   1.151 -	}	
   1.152 -	return "";
   1.153 +	for (int i=0; i<optlist.size(); ++i)
   1.154 +		if (optlist[i].getName()==s) return optlist[i].getArg();
   1.155 +	return QString();
   1.156  }