20 #define QT_NO_CAST_ASCII
21 #define QT_NO_ASCII_CAST
23 #include "optionparser.h"
25 #include <QCoreApplication>
34 QCoreApplication* qApp1 = QCoreApplication::instance();
36 qFatal(
"OptionParser: requires a QCoreApplication instance to be constructed first" );
38 init( qApp1->arguments(), 1 );
42 QCoreApplication* qApp1 = QCoreApplication::instance();
44 qFatal(
"OptionParser: requires a QApplication instance to be constructed first" );
46 init( qApp1->arguments(), offset );
58 void OptionParser::init(
int argc,
char *argv[],
int offset ) {
59 numReqArgs = numOptArgs = 0;
63 aname = QFileInfo( QString::fromUtf8( argv[0] ) ).fileName();
65 for (
int i = offset; i < argc; ++i ) {
66 args.append( QString::fromUtf8( argv[i] ) );
71 void OptionParser::init(
const QStringList& arguments,
int offset ) {
72 numReqArgs = numOptArgs = 0;
74 if ( !arguments.isEmpty() ) {
76 aname = QFileInfo( arguments[0] ).fileName();
78 for (
int i = offset; i < arguments.size(); ++i ) {
79 args.append( arguments[i] );
90 QStack<QString> stack;
92 QStringListIterator it(args);
94 while( it.hasPrevious() ) {
95 stack.push( it.previous() );
99 const OptionConstIterator obegin = options.begin();
100 const OptionConstIterator oend = options.end();
101 enum { StartState, ExpectingState, OptionalState } state = StartState;
103 enum TokenType { LongOpt, ShortOpt, Arg, End } t, currType = End;
104 bool extraLoop =
true;
105 while ( !stack.isEmpty() || extraLoop ) {
109 if ( !stack.isEmpty() ) {
114 if ( a.startsWith( QString::fromLatin1(
"--" ) ) ) {
118 qWarning(
"'--' feature not supported, yet" );
123 int equal = a.indexOf(
'=' );
125 stack.push( a.mid( equal + 1 ) );
129 }
else if ( a.length() == 1 ) {
131 }
else if ( a[0] ==
'-' ) {
132 #if 0 // compat mode for -long style options
133 if ( a.length() == 2 ) {
140 int equal = a.find(
'=' );
142 stack.push( a.mid( equal + 1 ) );
151 if ( a.length() > 2 ) {
152 stack.push( a.mid( 2 ) );
167 OptionConstIterator oit = obegin;
168 while ( oit != oend ) {
169 const Option &o = *oit;
170 if ( ( t == LongOpt && a == o.lname ) ||
171 ( t == ShortOpt && a[0].unicode() == o.sname ) ) {
177 if ( t == LongOpt && opt.type == OUnknown ) {
178 if ( currOpt.type != OVarLen ) {
179 qWarning(
"Unknown option --%s", a.toLatin1().data() );
185 }
else if ( t == ShortOpt && opt.type == OUnknown ) {
186 if ( currOpt.type != OVarLen ) {
187 qWarning(
"Unknown option -%c", a[0].unicode() );
195 opt = Option( OEnd );
201 if ( opt.type == OSwitch ) {
203 setOptions.insert( opt.lname, 1 );
204 setOptions.insert( QString( QChar( opt.sname ) ), 1 );
205 }
else if ( opt.type == OArg1 || opt.type == ORepeat ) {
206 state = ExpectingState;
209 setOptions.insert( opt.lname, 1 );
210 setOptions.insert( QString( QChar( opt.sname ) ), 1 );
211 }
else if ( opt.type == OOpt || opt.type == OVarLen ) {
212 state = OptionalState;
215 setOptions.insert( opt.lname, 1 );
216 setOptions.insert( QString( QChar( opt.sname ) ), 1 );
217 }
else if ( opt.type == OEnd ) {
219 }
else if ( opt.type == OUnknown && t == Arg ) {
220 if ( numReqArgs > 0 ) {
221 if ( reqArg.stringValue->isNull() ) {
222 *reqArg.stringValue = a;
224 qWarning(
"Too many arguments" );
227 }
else if ( numOptArgs > 0 ) {
228 if ( optArg.stringValue->isNull() ) {
229 *optArg.stringValue = a;
231 qWarning(
"Too many arguments" );
236 qFatal(
"unhandled StartState case %d", opt.type );
241 if ( currOpt.type == OArg1 ) {
242 *currOpt.stringValue = a;
244 }
else if ( currOpt.type == ORepeat ) {
245 currOpt.listValue->append( a );
251 QString n = currType == LongOpt ?
252 currOpt.lname : QString( QChar( currOpt.sname ) );
253 qWarning(
"Expected an argument after '%s' option", n.toLatin1().data() );
259 if ( currOpt.type == OOpt ) {
260 *currOpt.stringValue = a;
262 }
else if ( currOpt.type == OVarLen ) {
263 currOpt.listValue->append( origA );
270 if ( currOpt.type == OOpt )
271 *currOpt.stringValue = currOpt.def;
282 if ( untilFirstSwitchOnly && opt.type == OSwitch )
290 if ( numReqArgs > 0 && reqArg.stringValue->isNull() ) {
291 qWarning(
"Lacking required argument" );
304 Option opt( OSwitch, 0, lname );
311 void OptionParser::setSwitch(
const Option &o ) {
312 assert( o.type == OSwitch );
317 Option opt( OArg1, s, l );
324 Option opt( OVarLen, 0, l );
331 Option opt( ORepeat, s, QString::null );
338 Option opt( ORepeat, 0, l );
349 Option opt( OOpt, s, l );
357 Option opt( OUnknown, 0, name );
365 Option opt( OUnknown, 0, name );
374 return setOptions.find( name ) != setOptions.end();
void addRepeatableOption(char s, QStringList *v)
A macro to deprecate functions.
void addOption(char s, const QString &l, QString *v)
void addSwitch(const QString &lname, bool *b)
void addOptionalOption(const QString &l, QString *v, const QString &def)
OptionParser()
Constructs a command line parser from the arguments stored in a previously created QApplication insta...
void addOptionalArgument(const QString &name, QString *v)
void addArgument(const QString &name, QString *v)
void addVarLengthOption(const QString &l, QStringList *v)
bool isSet(const QString &name) const