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