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