options.cpp
author insilmaril
Wed, 12 Nov 2008 11:15:37 +0000
branchrelease-1-12-maintained
changeset 52 6cd53d63956f
parent 44 2513e153d481
permissions -rw-r--r--
Fix: Save note of MapCenter
     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 	for (int i=0; i< arglist.size(); ++i)
    59 	{
    60 		isFile=true;
    61 		if (arglist[i].left(1)=="-")
    62 		{
    63 			// Compare given option to all defined options
    64 			for (int j=0; j < optlist.size(); ++j)
    65 			{
    66 				if (arglist.at(i)==optlist.value(j).getShort() || 
    67 					arglist.at(i)==optlist.value(j).getLong())
    68 				{	
    69 					optlist[j].setActive();
    70 					isFile=false;
    71 					if (optlist[j].getType()==Option::String)
    72 					{
    73 						i++;
    74 						if (i==arglist.size())
    75 						{
    76 							qWarning ("Error: argument to option missing");
    77 							return 1;
    78 						}
    79 						optlist[j].setArg (arglist[i]);
    80 						isFile=false;
    81 					}
    82 					break;
    83 				} 
    84 			}
    85 			if (isFile)
    86 			{
    87 				qWarning("Error: Unknown argument ");
    88 				return 1;
    89 			}
    90 		} else
    91 			filelist.append (arglist[i]);
    92 	}
    93 	return 0;
    94 }
    95 
    96 void Options::add (Option o)
    97 {
    98 	optlist.append (o);
    99 }
   100 
   101 void Options::add (const QString &n, const Option::Type &t=Option::Switch, const QString &s="", const QString &l="")
   102 {
   103 	Option o;
   104 	o.set (n,t,s,l);
   105 	optlist.append (o);
   106 }
   107 
   108 void Options::setHelpText (const QString &s)
   109 {
   110 	helptext=s;
   111 }
   112 	
   113 QString Options::getHelpText ()
   114 {
   115 	return helptext;
   116 }
   117 
   118 QString Options::getProgramName()
   119 {
   120 	return progname;
   121 }
   122 
   123 QStringList Options::getFileList ()
   124 {
   125 	return filelist;
   126 }
   127 
   128 bool Options::isOn(const QString &s)
   129 {
   130 	for (int i=0; i<optlist.size(); ++i)
   131 		if (optlist[i].getName()==s && optlist[i].isActive() )
   132 			return true;
   133 	return false;
   134 }
   135 
   136 QString Options::getArg(const QString &s)
   137 {
   138 	for (int i=0; i<optlist.size(); ++i)
   139 		if (optlist[i].getName()==s) return optlist[i].getArg();
   140 	return QString();
   141 }