options.cpp
author insilmaril
Tue, 23 Jan 2007 11:50:53 +0000
changeset 422 07a2f3f31101
parent 408 c2a05fa925a1
child 474 d9add21ee9d3
permissions -rw-r--r--
1.8.65 Various fixes
     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=SwitchOption;
    16 	sarg="";
    17 	active=false;
    18 }
    19 
    20 Option::Option (const QString &n, const OptionType &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 OptionType &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 OptionType 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;
    51 	int i=0;
    52 	while ( i < qApp->argc())
    53 	{	
    54 		arglist.append (qApp->argv()[i]);
    55 		i++;
    56 	}
    57 
    58 	// Get program name
    59 	progname=arglist.first();
    60 	arglist.pop_front();
    61 
    62 	// Work through rest of options
    63 	bool isFile;
    64 	for (i=0; i< arglist.size(); ++i)
    65 	{
    66 		isFile=true;
    67 		if (arglist[i].left(1)=="-")
    68 		{
    69 			// Compare given option to all defined options
    70 			for (int j=0; j < optlist.size(); ++j)
    71 			{
    72 				if (arglist.at(i)==optlist.value(j).getShort() || 
    73 					arglist.at(i)==optlist.value(j).getLong())
    74 				{	
    75 					optlist[j].setActive();
    76 					isFile=false;
    77 					if (optlist[j].getType()==StringOption)
    78 					{
    79 						i++;
    80 						if (i==arglist.size())
    81 						{
    82 							qWarning ("Error: argument to option missing");
    83 							return 1;
    84 						}
    85 						optlist[j].setArg (arglist[i]);
    86 						isFile=false;
    87 					}
    88 					break;
    89 				} 
    90 			}
    91 			if (isFile)
    92 			{
    93 				qWarning("Error: Unknown argument ");
    94 				return 1;
    95 			}
    96 		} else
    97 			filelist.append (arglist[i]);
    98 	}
    99 	return 0;
   100 }
   101 
   102 void Options::add (Option o)
   103 {
   104 	optlist.append (o);
   105 }
   106 
   107 void Options::add (const QString &n, const OptionType &t=SwitchOption, const QString &s="", const QString &l="")
   108 {
   109 	Option o;
   110 	o.set (n,t,s,l);
   111 	optlist.append (o);
   112 }
   113 
   114 void Options::setHelpText (const QString &s)
   115 {
   116 	helptext=s;
   117 }
   118 	
   119 QString Options::getHelpText ()
   120 {
   121 	return helptext;
   122 }
   123 
   124 QString Options::getProgramName()
   125 {
   126 	return progname;
   127 }
   128 
   129 QStringList Options::getFileList ()
   130 {
   131 	return filelist;
   132 }
   133 
   134 bool Options::isOn(const QString &s)
   135 {
   136 	for (int i=0; i<optlist.size(); ++i)
   137 		if (optlist[i].getName()==s && optlist[i].isActive() )
   138 			return true;
   139 	return false;
   140 }
   141 
   142 QString Options::getArg(const QString &s)
   143 {
   144 	for (int i=0; i<optlist.size(); ++i)
   145 		if (optlist[i].getName()==s) return optlist[i].getArg();
   146 	return QString();
   147 }