1 /*
2  * simplecli.h - Simple CommandLine Interface parser / manager
3  * Copyright (C) 2009  Maciej Niedzielski
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  */
20 
21 #ifndef SIMPLECLI_H
22 #define SIMPLECLI_H
23 
24 #include <QObject>
25 #include <QMap>
26 #include <QStringList>
27 
28 class SimpleCli : public QObject
29 {
30 public:
31 	void defineSwitch(const QByteArray& name, const QString& help = QString());
32 	void defineParam(const QByteArray &name, const QString& valueHelp = QString("ARG"), const QString& help = QString());
33 
34 	void defineAlias(const QByteArray &alias, const QByteArray &originalName);
35 
36 	QHash<QByteArray, QByteArray> parse(int argc, char* argv[],
37 									   const QList<QByteArray> &terminalArgs = QList<QByteArray>(),
38 									   int* safeArgc = 0);
39 
40 	QString optionsHelp(int textWidth);
41 	static QString wrap(QString text, int width, int margin = 0, int firstMargin = -1);
42 
43 private:
44 	struct Arg {
45 		QByteArray name;
46 		QList<QByteArray> aliases;
47 		QChar shortName;
48 
49 		bool needsValue;
50 
51 		QString help;
52 		QString valueHelp;
53 
54 
ArgArg55 		Arg(const QByteArray& argName, const QString& argValueHelp, const QString& argHelp, bool argNeedsValue)
56 				: name(argName), needsValue(argNeedsValue), help(argHelp), valueHelp(argValueHelp) {}
57 
ArgArg58 		Arg() : needsValue(false) {}	// needed only by QMap
59 	};
60 
61 	QMap<QByteArray, Arg> argdefs;
62 	QMap<QByteArray, QByteArray> aliases;
63 };
64 
65 #endif
66