options.cpp
changeset 328 33310803546c
child 2 608f976aa7bb
child 44 2513e153d481
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/options.cpp	Mon May 15 07:32:10 2006 +0000
     1.3 @@ -0,0 +1,144 @@
     1.4 +#include "options.h"
     1.5 +#include <iostream>
     1.6 +
     1.7 +#include <qapplication.h>
     1.8 +
     1.9 +using namespace std;
    1.10 +
    1.11 +
    1.12 +Option::Option()
    1.13 +{
    1.14 +	name="";
    1.15 +	sName="";
    1.16 +	lName="";
    1.17 +	type=SwitchOption;
    1.18 +	sarg="";
    1.19 +	active=false;
    1.20 +}
    1.21 +
    1.22 +void Option::set(const QString &n, const OptionType &t, const QString &s, const QString &l)
    1.23 +{
    1.24 +	sName="-"+s;
    1.25 +	lName="--"+l;
    1.26 +	type=t;
    1.27 +	name=n;
    1.28 +}
    1.29 +
    1.30 +QString Option::getName () { return name; }
    1.31 +QString Option::getShort () { return sName; }
    1.32 +QString Option::getLong() { return lName; }
    1.33 +OptionType Option::getType() { return type; }
    1.34 +void Option::setArg(const QString& s) { sarg=s; }
    1.35 +QString Option::getArg() { return sarg; }	
    1.36 +void Option::setActive() { active=true; }	
    1.37 +bool Option::isActive() { return active; }
    1.38 +
    1.39 +///////////////////////////////////////////////////////////////
    1.40 +Options::Options() {}	
    1.41 +
    1.42 +int Options::parse()
    1.43 +{
    1.44 +	QStringList arglist;
    1.45 +	int i=0;
    1.46 +	while (i<qApp->argc())
    1.47 +	{	
    1.48 +		arglist.append (qApp->argv()[i]);
    1.49 +		i++;
    1.50 +	}
    1.51 +
    1.52 +	// Get program name
    1.53 +	progname=arglist.first();
    1.54 +	arglist.pop_front();
    1.55 +
    1.56 +	// Work through rest of options
    1.57 +	bool isFile;
    1.58 +	OptionList::iterator itopt;
    1.59 +	QStringList::iterator itarg;
    1.60 +	itarg=arglist.begin();
    1.61 +	while (itarg!=arglist.end())
    1.62 +	{
    1.63 +		isFile=true;
    1.64 +		if ((*itarg).left(1)=="-")
    1.65 +		{
    1.66 +			// Compare given option to all defined options
    1.67 +			itopt=optlist.begin();
    1.68 +			while (itopt!=optlist.end())
    1.69 +			{
    1.70 +				if ((*itarg)==(*itopt).getShort() || 
    1.71 +					(*itarg)==(*itopt).getLong())
    1.72 +				{	
    1.73 +					(*itopt).setActive();
    1.74 +					isFile=false;
    1.75 +					if ((*itopt).getType()==StringOption)
    1.76 +					{
    1.77 +						itarg++;
    1.78 +						if (itarg==arglist.end())
    1.79 +						{
    1.80 +							cout << "Error: argument to option missing\n";
    1.81 +							return 1;
    1.82 +						}
    1.83 +						(*itopt).setArg (*itarg);
    1.84 +						isFile=false;
    1.85 +					}
    1.86 +					break;
    1.87 +				} 
    1.88 +				itopt++;
    1.89 +			}
    1.90 +			if (isFile)
    1.91 +			{
    1.92 +				cout << "Error: Unknown argument "<<*itarg<<endl;
    1.93 +				return 1;
    1.94 +			}
    1.95 +		} else
    1.96 +			filelist.append (*itarg);
    1.97 +		itarg++;
    1.98 +	}
    1.99 +	return 0;
   1.100 +}
   1.101 +
   1.102 +void Options::add (const QString &n, const OptionType &t=SwitchOption, const QString &s="", const QString &l="")
   1.103 +{
   1.104 +	Option o;
   1.105 +	o.set (n,t,s,l);
   1.106 +	optlist.append (o);
   1.107 +}
   1.108 +
   1.109 +void Options::setHelpText (const QString &s)
   1.110 +{
   1.111 +	helptext=s;
   1.112 +}
   1.113 +	
   1.114 +QString Options::getHelpText ()
   1.115 +{
   1.116 +	return helptext;
   1.117 +}
   1.118 +
   1.119 +QString Options::getProgramName()
   1.120 +{
   1.121 +	return progname;
   1.122 +}
   1.123 +
   1.124 +QStringList Options::getFileList ()
   1.125 +{
   1.126 +	return filelist;
   1.127 +}
   1.128 +
   1.129 +bool Options::isOn(const QString &s)
   1.130 +{
   1.131 +	OptionList::iterator it;
   1.132 +	for ( it = optlist.begin(); it != optlist.end(); ++it )
   1.133 +		if ((*it).getName()==s && (*it).isActive() )
   1.134 +			return true;
   1.135 +	return false;
   1.136 +}
   1.137 +
   1.138 +QString Options::getArg(const QString &s)
   1.139 +{
   1.140 +	OptionList::iterator it;
   1.141 +	for ( it = optlist.begin(); it != optlist.end(); ++it )
   1.142 +	{
   1.143 +		if ((*it).getName()==s)
   1.144 +			return (*it).getArg();
   1.145 +	}	
   1.146 +	return "";
   1.147 +}