[be54f4]: src / gui / args.cc  Maximize  Restore  History

Download this file

235 lines (215 with data), 7.0 kB

  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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*
* PDFedit - free program for PDF document manipulation.
* Copyright (C) 2006, 2007, 2008 PDFedit team: Michal Hocko,
* Miroslav Jahoda,
* Jozef Misutka,
* Martin Petricek
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program (in doc/LICENSE.GPL); if not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*
* Project is hosted on http://sourceforge.net/projects/pdfedit
*/
/** @file
Utility functions to work with commandline arguments
\brief Commandline arguments utility functions
@author Martin Petricek
*/
#include "args.h"
#include "qtcompat.h"
#include "util.h"
#include "version.h"
#include <assert.h>
#include <iostream>
#include <qobject.h>
#include QLIST
#include <qstringlist.h>
#include <stdlib.h>
using namespace std;
using namespace util;
/** Option handler map*/
typedef QMap<QString, optHandler> OptionMap;
/** Option handler map (with parameter) */
typedef QMap<QString, optHandlerParam> OptionMapParam;
/** Option help map*/
typedef QMap<QString, QString> OptionHelp;
/** Stop processing options from commandline? */
bool stopOpt=false;
/** Name of the program (argv[0]) */
QString binName;
/** Option help texts */
OptionHelp optHelp;
/** Option parameter names */
OptionHelp optParamName;
/** Option handlers (without parameters) */
OptionMap optMap;
/** Option handlers (with parameters) */
OptionMapParam optMapParam;
/** Argument index currently processed */
int argIndex;
/** Standard argc */
int argc;
/** Standard argv */
char **argv;
/** Length of longest parameter name (including name of its argument)*/
int maxParamNameLen=1;
//Forward declarations that should not be visible in header file
int handleOption(const QString &param);
const QString nextParam(const QString &param);
/**
Print out application header (name and version) to stdout
*/
void printHeader() {
cout << APP_NAME << " " << PDFEDIT_VERSION << endl;
}
/**
Print out application header (name and version) to stderr
*/
void printHeaderErr() {
cerr << APP_NAME << " " << PDFEDIT_VERSION << endl;
}
/**
Standard handler to handle --help parameter
Prints all registered commandline options along with their helptext to STDOUT
and exits
*/
void handleHelpOptions(){
cout << convertFromUnicode(QObject::tr("Options: "),CON) << endl;
Q_List<QString> opt=optHelp.keys();
for (Q_List<QString>::Iterator it=opt.begin();it!=opt.end();++it) {
QString paramName=*it;
if (optParamName.contains(paramName)) { //Add name of parameter
paramName+=" ["+optParamName[paramName]+"]";
}
cout << " ";
cout.width(maxParamNameLen+1); //width of option name
cout.flags(ios::left);
cout << convertFromUnicode(paramName,CON) << convertFromUnicode(optHelp[*it],CON) << endl;
}
exit(0);
}
/** handle -- parameter (stop processing option) */
void handleStopOpt(){
stopOpt=true;
}
/**
Register function to handle option without parameter
@param param Name of option (case sensitive)
@param h Function to handle this option
@param help Brief one-line help about this option
*/
void optionHandler(const QString &param, optHandler h,const QString &help) {
optMap[param]=h;
optHelp[param]=help;
int len=param.length(); //Length of parameter including its name in help
if (len>maxParamNameLen) maxParamNameLen=len;
}
/**
Register function to handle option with parameter
Parameters can be accepted as: -opt[n], -opt [n]
@param param Name of option (case sensitive)
@param paramName Name of parameter (only shown in help)
@param h Function to handle this option
@param help Brief one-line help about this option
*/
void optionHandlerParam(const QString &param, const QString &paramName, optHandlerParam h,const QString &help) {
optMapParam[param]=h;
optHelp[param]=help;
optParamName[param]=paramName;
int len=param.length()+3+paramName.length(); //Length of parameter including its name in help
if (len>maxParamNameLen) maxParamNameLen=len;
}
/**
Calls option handler on given option
@param param Commandline option to check
@return 0 if option is valid, but without parameters, 1 if option 'consumed' parameter after it, -1 if option is invalid
*/
int handleOption(const QString &param) {
//parameterless option
if (optMap.contains(param)) {
assert(optMap[param]);
optMap[param]();
return 0;
}
//parameterized option, exact match, option parameter is next parameter
if (optMapParam.contains(param)) {
assert(optMapParam[param]);
optMapParam[param](nextParam(param));
return 1;
}
//parameterized option, option parameter is directly appended
QString paramBase;
for(unsigned int i=2;i<param.length();i++) {
//Try to find prefix as name of parameters
paramBase=param.left(i);
if (optMapParam.contains(paramBase)) {
//option is found -> execute handler
assert(optMapParam[paramBase]);
optMapParam[paramBase](param.mid(i));
return 0;
}
}
return -1;
}
/**
Sets name of binary to show in help, etc ...
(parameter should be argv[0])
@param name name of binary
*/
void setArgv0(const QString &name) {
binName=name;
}
/**
Get next parameter from commandline
@param param name of currently processed parameter
*/
const QString nextParam(const QString &param) {
argIndex++;
if (argIndex<argc) {
assert(argv[argIndex]);
return argv[argIndex];
}
printHeaderErr();
fatalError(QObject::tr("Parameter missing for option : ")+param+"\n\n"
+QObject::tr("Use '%1 --help' to see list of commandline options and their parameters").arg(binName));
return "";
}
/**
Function to handle commandline parameters using installed callbacks
Will terminate program with error message if unknown or invalid commandline options are found
@param _argc count of parameters (standard argc)
@param _argv parameters (standard argv)
@return List of non-option parameters;
*/
QStringList handleParams(int _argc,char **_argv) {
argc=_argc;
argv=_argv;
setArgv0(argv[0]);
QString param;
QStringList params;
for (argIndex=1;argIndex<argc;argIndex++) {
assert(argv[argIndex]);
param=argv[argIndex];
if (param.startsWith("-") && !stopOpt) { //option
if (handleOption(param)==-1) {
printHeaderErr();
fatalError(QObject::tr("Invalid commandline option : ")+param+"\n\n"
+QObject::tr("Use '%1 --help' to see list of commandline options and their parameters").arg(binName));
}
} else {
params+=param;
}
}
return params;
}

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks