1 /*
2 * simplecli.cpp - 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 #include "simplecli.h"
22
23 #include <QDebug>
24
25 /**
26 * \class SimpleCli
27 * \brief Simple Commandline Interface parser.
28 *
29 * This class allows you to define Commandline Interface for your application
30 * and then use this information to convert argv to a map of options and their values.
31 *
32 * Please note that support for short options (-x) is very limited
33 * and provided only to support options like -h and -v.
34 */
35
36
37 /**
38 * \brief Define a switch (option that does not have a value)
39 */
defineSwitch(const QByteArray & name,const QString & help)40 void SimpleCli::defineSwitch(const QByteArray& name, const QString& help)
41 {
42 argdefs[name] = Arg(name, "", help, false);
43 aliases[name] = name;
44 }
45
46 /**
47 * \brief Define a parameter (option that requires a value)
48 */
defineParam(const QByteArray & name,const QString & valueHelp,const QString & help)49 void SimpleCli::defineParam(const QByteArray& name, const QString& valueHelp, const QString& help)
50 {
51 argdefs[name] = Arg(name, valueHelp, help, true);
52 aliases[name] = name;
53 }
54
55 /**
56 * \brief Add alias for already existing option.
57 * \a alias will be mapped to \a originalName in parse() result.
58 */
defineAlias(const QByteArray & alias,const QByteArray & originalName)59 void SimpleCli::defineAlias(const QByteArray& alias, const QByteArray& originalName)
60 {
61 if (!argdefs.contains(originalName)) {
62 qDebug("CLI: cannot add alias '%s' because name '%s' does not exist",
63 alias.constData(), originalName.constData());
64 return;
65 }
66 argdefs[originalName].aliases.append(alias);
67 aliases[alias] = originalName;
68 if (alias.length() == 1 && argdefs[originalName].shortName.isNull()) {
69 argdefs[originalName].shortName = alias.at(0);
70 }
71 }
72
73 /**
74 * \brief Parse \a argv into a name,value map.
75 * \param terminalArgs stop parsing when one of these options is found (it will be included in result)
76 * \param safeArgs if not NULL, will be used to pass number of arguments before terminal argument (or argc if there was no terminal argument)
77 *
78 * Supported options syntax: --switch; --param=value; --param value; -switch; -param=value; -param value.
79 * Additionally on Windows: /switch; /param:value; /param value.
80 *
81 * When creating the map, alias names are converted to original option names.
82 *
83 * Use \a terminalArgs if you want need to stop parsing after certain options for security reasons, etc.
84 */
parse(int argc,char * argv[],const QList<QByteArray> & terminalArgs,int * safeArgc)85 QHash<QByteArray, QByteArray> SimpleCli::parse(int argc, char* argv[], const QList<QByteArray>& terminalArgs, int* safeArgc)
86 {
87 #ifdef Q_OS_WIN
88 const bool winmode = true;
89 #else
90 const bool winmode = false;
91 #endif
92
93 QHash<QByteArray, QByteArray> map;
94 int safe = 1;
95 int n = 1;
96 for (; n < argc; ++n) {
97 QByteArray str = QByteArray(argv[n]);
98 QByteArray left, right;
99 int sep = str.indexOf('=');
100 if (sep == -1) {
101 left = str;
102 } else {
103 left = str.mid(0, sep);
104 right = str.mid(sep + 1);
105 }
106
107 bool unnamedArgument = true;
108 if (left.startsWith("--")) {
109 left = left.mid(2);
110 unnamedArgument = false;
111 } else if (left.startsWith('-') || (left.startsWith('/') && winmode)) {
112 left = left.mid(1);
113 unnamedArgument = false;
114 } else if (n == 1 && left.startsWith("xmpp:")) {
115 unnamedArgument = false;
116 left = "uri";
117 right = str;
118 }
119
120 QByteArray name, value;
121 if (unnamedArgument) {
122 value = left;
123 } else {
124 name = left;
125 value = right;
126 if (aliases.contains(name)) {
127 name = argdefs[aliases[name]].name;
128 if (argdefs[name].needsValue && value.isNull() && n + 1 < argc) {
129 value = QByteArray(argv[++n]);
130 }
131 }
132
133 }
134
135 if (map.contains(name)) {
136 qDebug("CLI: Ignoring next value ('%s') for '%s' arg.",
137 value.constData(), name.constData());
138 } else {
139 map[name] = value;
140 }
141
142 if (terminalArgs.contains(name)) {
143 break;
144 } else {
145 safe = n + 1;
146 }
147 }
148
149 if (safeArgc) {
150 *safeArgc = safe;
151 }
152 return map;
153 }
154
155 /**
156 * \brief Produce options description, for use in --help.
157 * \param textWidth wrap text when wider than \a textWidth
158 */
optionsHelp(int textWidth)159 QString SimpleCli::optionsHelp(int textWidth)
160 {
161 QString ret;
162
163 int margin = 2;
164
165 int longest = -1;
166 bool foundShort = false;
167
168 foreach (Arg arg, argdefs) {
169 if (arg.needsValue) {
170 longest = qMax(arg.name.length() + arg.valueHelp.length() + 1, longest);
171 } else {
172 longest = qMax(arg.name.length(), longest);
173 }
174
175 foundShort = foundShort || !arg.shortName.isNull();
176 }
177 longest += 2; // 2 = length("--")
178 int helpPadding = longest + 6; // 6 = 2 (left margin) + 2 (space before help) + 2 (next line indent)
179 if (foundShort) {
180 helpPadding += 4; // 4 = length("-x, ")
181 }
182
183 foreach (Arg arg, argdefs) {
184 QString line;
185 line.fill(' ', margin);
186 if (foundShort) {
187 if (arg.shortName.isNull()) {
188 line += " ";
189 } else {
190 line += '-' + arg.shortName + ", ";
191 }
192 }
193 QString longarg = "--" + arg.name;
194 if (arg.needsValue) {
195 longarg += '=' + arg.valueHelp;
196 }
197 line += longarg;
198 line += QString().fill(' ', longest - longarg.length() + 2); // 2 (space before help)
199 line += arg.help;
200
201 ret += wrap(line, textWidth, helpPadding, 0);
202 }
203
204 return ret;
205 }
206
207 /**
208 * \brief Wrap text for printing on console.
209 * \param text text to be wrapped
210 * \param width width of the text
211 * \param margin left margin (filled with spaces)
212 * \param firstMargin margin in first line
213 * Note: This function is designed for text that do not contain tabs or line breaks.
214 * Results may be not pretty if such \a text is passed.
215 */
wrap(QString text,int width,int margin,int firstMargin)216 QString SimpleCli::wrap(QString text, int width, int margin, int firstMargin)
217 {
218 if (firstMargin < 0) {
219 firstMargin = margin;
220 }
221
222 QString output;
223
224 int prevBreak = -1;
225 int currentMargin = firstMargin;
226 int nextBreak;
227
228 do {
229 nextBreak = prevBreak + width + 1 - currentMargin;
230 if (nextBreak < text.length()) {
231 int lastSpace = text.lastIndexOf(' ', nextBreak);
232 if (lastSpace > prevBreak) {
233 nextBreak = lastSpace;
234 } else {
235 // will be a bit longer...
236 nextBreak = text.indexOf(' ', nextBreak);
237 if (nextBreak == -1) {
238 nextBreak = text.length();
239 }
240 }
241 } else {
242 nextBreak = text.length();
243 }
244
245 output += QString().fill(' ', currentMargin);
246 output += text.mid(prevBreak + 1, nextBreak - prevBreak - 1);
247 output += '\n';
248
249 prevBreak = nextBreak;
250 currentMargin = margin;
251 } while (nextBreak < text.length());
252
253 return output;
254 }
255