File: options.cpp

package info (click to toggle)
vym 2.6.11-3
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye, buster, sid, trixie
  • size: 12,628 kB
  • sloc: cpp: 34,052; ruby: 846; xml: 278; sh: 187; makefile: 28
file content (142 lines) | stat: -rw-r--r-- 2,758 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include "options.h"

#include <iostream>
#include <QApplication>


using namespace std;


Option::Option()
{
    name="";
    sName="";
    lName="";
    type=Switch;
    sarg="";
    active=false;
}

Option::Option (const QString &n, const Type &t, const QString &s, const QString &l)
{
    sName="-"+s;
    lName="--"+l;
    type=t;
    name=n;
}

void Option::set(const QString &n, const Type &t, const QString &s, const QString &l)
{
    sName="-"+s;
    lName="--"+l;
    type=t;
    name=n;
}

QString Option::getName () { return name; }
QString Option::getShort () { return sName; }
QString Option::getLong() { return lName; }
Option::Type Option::getType() { return type; }
void Option::setArg(const QString& s) { sarg=s; }
QString Option::getArg() { return sarg; }   
void Option::setActive() { active=true; }   
bool Option::isActive() { return active; }

///////////////////////////////////////////////////////////////
Options::Options() {}	

int Options::parse()
{
    QStringList arglist=qApp->arguments();

    // Get program name
    progname=arglist.first();
    arglist.pop_front();

    // Work through rest of options
    bool isFile;
    int i=0;
    for (i=0; i< arglist.size(); ++i)
    {
	isFile=true;
	if (arglist[i].left(1)=="-")
	{
	    // Compare given option to all defined options
	    for (int j=0; j < optlist.size(); ++j)
	    {
		if (arglist.at(i)==optlist.value(j).getShort() || 
		    arglist.at(i)==optlist.value(j).getLong())
		{   
		    optlist[j].setActive();
		    isFile=false;
		    if (optlist[j].getType()==Option::String)
		    {
			i++;
			if (i==arglist.size())
			{
			    qWarning ("Error: argument to option missing");
			    return 1;
			}
			optlist[j].setArg (arglist[i]);
			isFile=false;
		    }
		    break;
		} 
	    }
	    if (isFile)
	    {
		qWarning("Error: Unknown argument ");
		return 1;
	    }
	} else
	    filelist.append (arglist[i]);
    }
    return 0;
}

void Options::add (Option o)
{
    optlist.append (o);
}

void Options::add (const QString &n, const Option::Type &t=Option::Switch, const QString &s="", const QString &l="")
{
    Option o;
    o.set (n,t,s,l);
    optlist.append (o);
}

void Options::setHelpText (const QString &s)
{
    helptext=s;
}
    
QString Options::getHelpText ()
{
    return helptext;
}

QString Options::getProgramName()
{
    return progname;
}

QStringList Options::getFileList ()
{
    return filelist;
}

bool Options::isOn(const QString &s)
{
    for (int i=0; i<optlist.size(); ++i)
	if (optlist[i].getName()==s && optlist[i].isActive() )
	    return true;
    return false;
}

QString Options::getArg(const QString &s)
{
    for (int i=0; i<optlist.size(); ++i)
	if (optlist[i].getName()==s) return optlist[i].getArg();
    return QString();
}