1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3#include "tracer.h"
4
5#include <QtCore/QFileInfo>
6#include <QtCore/QStringBuilder>
7#include <QtWidgets/QMessageBox>
8
9#include "cmdlineparser.h"
10
11QT_BEGIN_NAMESPACE
12
13static const char helpMessage[] = QT_TRANSLATE_NOOP("CmdLineParser",
14 "Usage: assistant [Options]\n\n"
15 "-collectionFile file Uses the specified collection\n"
16 " file instead of the default one\n"
17 "-showUrl url Shows the document with the\n"
18 " url.\n"
19 "-enableRemoteControl Enables Assistant to be\n"
20 " remotely controlled.\n"
21 "-show widget Shows the specified dockwidget\n"
22 " which can be \"contents\", \"index\",\n"
23 " \"bookmarks\" or \"search\".\n"
24 "-activate widget Activates the specified dockwidget\n"
25 " which can be \"contents\", \"index\",\n"
26 " \"bookmarks\" or \"search\".\n"
27 "-hide widget Hides the specified dockwidget\n"
28 " which can be \"contents\", \"index\"\n"
29 " \"bookmarks\" or \"search\".\n"
30 "-register helpFile Registers the specified help file\n"
31 " (.qch) in the given collection\n"
32 " file.\n"
33 "-unregister helpFile Unregisters the specified help file\n"
34 " (.qch) from the give collection\n"
35 " file.\n"
36 "-setCurrentFilter filter Set the filter as the active filter.\n"
37 "-remove-search-index Removes the full text search index.\n"
38 "-rebuild-search-index Obsolete. Use -remove-search-index instead.\n"
39 " Removes the full text search index.\n"
40 " It will be rebuilt on next Assistant run.\n"
41 "-quiet Does not display any error or\n"
42 " status message.\n"
43 "-help Displays this help.\n"
44 );
45
46
47CmdLineParser::CmdLineParser(const QStringList &arguments)
48 : m_pos(0),
49 m_enableRemoteControl(false),
50 m_contents(Untouched),
51 m_index(Untouched),
52 m_bookmarks(Untouched),
53 m_search(Untouched),
54 m_register(None),
55 m_removeSearchIndex(false),
56 m_quiet(false)
57{
58 TRACE_OBJ
59 for (int i = 1; i < arguments.size(); ++i) {
60 const QString &arg = arguments.at(i);
61 if (arg.toLower() == "-quiet")
62 m_quiet = true;
63 else
64 m_arguments.append(t: arg);
65 }
66}
67
68CmdLineParser::Result CmdLineParser::parse()
69{
70 TRACE_OBJ
71 bool showHelp = false;
72
73 while (m_error.isEmpty() && hasMoreArgs()) {
74 const QString &arg = nextArg().toLower();
75 if (arg == QLatin1String("-collectionfile"))
76 handleCollectionFileOption();
77 else if (arg == QLatin1String("-showurl"))
78 handleShowUrlOption();
79 else if (arg == QLatin1String("-enableremotecontrol"))
80 m_enableRemoteControl = true;
81 else if (arg == QLatin1String("-show"))
82 handleShowOption();
83 else if (arg == QLatin1String("-hide"))
84 handleHideOption();
85 else if (arg == QLatin1String("-activate"))
86 handleActivateOption();
87 else if (arg == QLatin1String("-register"))
88 handleRegisterOption();
89 else if (arg == QLatin1String("-unregister"))
90 handleUnregisterOption();
91 else if (arg == QLatin1String("-setcurrentfilter"))
92 handleSetCurrentFilterOption();
93 else if (arg == QLatin1String("-remove-search-index"))
94 m_removeSearchIndex = true;
95 else if (arg == QLatin1String("-rebuild-search-index"))
96 m_removeSearchIndex = true;
97 else if (arg == QLatin1String("-help"))
98 showHelp = true;
99 else
100 m_error = tr(sourceText: "Unknown option: %1").arg(a: arg);
101 }
102
103 if (!m_error.isEmpty()) {
104 showMessage(msg: m_error + QLatin1String("\n\n\n") + tr(sourceText: helpMessage), error: true);
105 return Error;
106 } else if (showHelp) {
107 showMessage(msg: tr(sourceText: helpMessage), error: false);
108 return Help;
109 }
110 return Ok;
111}
112
113bool CmdLineParser::hasMoreArgs() const
114{
115 TRACE_OBJ
116 return m_pos < m_arguments.size();
117}
118
119const QString &CmdLineParser::nextArg()
120{
121 TRACE_OBJ
122 Q_ASSERT(hasMoreArgs());
123 return m_arguments.at(i: m_pos++);
124}
125
126void CmdLineParser::handleCollectionFileOption()
127{
128 TRACE_OBJ
129 if (hasMoreArgs()) {
130 const QString &fileName = nextArg();
131 m_collectionFile = getFileName(fileName);
132 if (m_collectionFile.isEmpty())
133 m_error = tr(sourceText: "The collection file '%1' does not exist.").
134 arg(a: fileName);
135 } else {
136 m_error = tr(sourceText: "Missing collection file.");
137 }
138}
139
140void CmdLineParser::handleShowUrlOption()
141{
142 TRACE_OBJ
143 if (hasMoreArgs()) {
144 const QString &urlString = nextArg();
145 QUrl url(urlString);
146 if (url.isValid()) {
147 m_url = url;
148 } else
149 m_error = tr(sourceText: "Invalid URL '%1'.").arg(a: urlString);
150 } else {
151 m_error = tr(sourceText: "Missing URL.");
152 }
153}
154
155void CmdLineParser::handleShowOption()
156{
157 TRACE_OBJ
158 handleShowOrHideOrActivateOption(state: Show);
159}
160
161void CmdLineParser::handleHideOption()
162{
163 TRACE_OBJ
164 handleShowOrHideOrActivateOption(state: Hide);
165}
166
167void CmdLineParser::handleActivateOption()
168{
169 TRACE_OBJ
170 handleShowOrHideOrActivateOption(state: Activate);
171}
172
173void CmdLineParser::handleShowOrHideOrActivateOption(ShowState state)
174{
175 TRACE_OBJ
176 if (hasMoreArgs()) {
177 const QString &widget = nextArg().toLower();
178 if (widget == QLatin1String("contents"))
179 m_contents = state;
180 else if (widget == QLatin1String("index"))
181 m_index = state;
182 else if (widget == QLatin1String("bookmarks"))
183 m_bookmarks = state;
184 else if (widget == QLatin1String("search"))
185 m_search = state;
186 else
187 m_error = tr(sourceText: "Unknown widget: %1").arg(a: widget);
188 } else {
189 m_error = tr(sourceText: "Missing widget.");
190 }
191}
192
193void CmdLineParser::handleRegisterOption()
194{
195 TRACE_OBJ
196 handleRegisterOrUnregisterOption(state: Register);
197}
198
199void CmdLineParser::handleUnregisterOption()
200{
201 TRACE_OBJ
202 handleRegisterOrUnregisterOption(state: Unregister);
203}
204
205void CmdLineParser::handleRegisterOrUnregisterOption(RegisterState state)
206{
207 TRACE_OBJ
208 if (hasMoreArgs()) {
209 const QString &fileName = nextArg();
210 m_helpFile = getFileName(fileName);
211 if (m_helpFile.isEmpty())
212 m_error = tr(sourceText: "The Qt help file '%1' does not exist.").arg(a: fileName);
213 else
214 m_register = state;
215 } else {
216 m_error = tr(sourceText: "Missing help file.");
217 }
218}
219
220void CmdLineParser::handleSetCurrentFilterOption()
221{
222 TRACE_OBJ
223 if (hasMoreArgs())
224 m_currentFilter = nextArg();
225 else
226 m_error = tr(sourceText: "Missing filter argument.");
227}
228
229QString CmdLineParser::getFileName(const QString &fileName)
230{
231 TRACE_OBJ
232 QFileInfo fi(fileName);
233 if (!fi.exists())
234 return QString();
235 return fi.absoluteFilePath();
236}
237
238void CmdLineParser::showMessage(const QString &msg, bool error)
239{
240 TRACE_OBJ
241 if (m_quiet)
242 return;
243#ifdef Q_OS_WIN
244 QString message = QLatin1String("<pre>") % msg % QLatin1String("</pre>");
245 if (error)
246 QMessageBox::critical(0, tr("Error"), message);
247 else
248 QMessageBox::information(0, tr("Notice"), message);
249#else
250 fprintf(stream: error ? stderr : stdout, format: "%s\n", qPrintable(msg));
251#endif
252}
253
254void CmdLineParser::setCollectionFile(const QString &file)
255{
256 TRACE_OBJ
257 m_collectionFile = file;
258}
259
260QString CmdLineParser::collectionFile() const
261{
262 TRACE_OBJ
263 return m_collectionFile;
264}
265
266bool CmdLineParser::collectionFileGiven() const
267{
268 TRACE_OBJ
269 return m_arguments.contains(str: QLatin1String("-collectionfile"),
270 cs: Qt::CaseInsensitive);
271}
272
273QUrl CmdLineParser::url() const
274{
275 TRACE_OBJ
276 return m_url;
277}
278
279bool CmdLineParser::enableRemoteControl() const
280{
281 TRACE_OBJ
282 return m_enableRemoteControl;
283}
284
285CmdLineParser::ShowState CmdLineParser::contents() const
286{
287 TRACE_OBJ
288 return m_contents;
289}
290
291CmdLineParser::ShowState CmdLineParser::index() const
292{
293 TRACE_OBJ
294 return m_index;
295}
296
297CmdLineParser::ShowState CmdLineParser::bookmarks() const
298{
299 TRACE_OBJ
300 return m_bookmarks;
301}
302
303CmdLineParser::ShowState CmdLineParser::search() const
304{
305 TRACE_OBJ
306 return m_search;
307}
308
309QString CmdLineParser::currentFilter() const
310{
311 TRACE_OBJ
312 return m_currentFilter;
313}
314
315bool CmdLineParser::removeSearchIndex() const
316{
317 TRACE_OBJ
318 return m_removeSearchIndex;
319}
320
321CmdLineParser::RegisterState CmdLineParser::registerRequest() const
322{
323 TRACE_OBJ
324 return m_register;
325}
326
327QString CmdLineParser::helpFile() const
328{
329 TRACE_OBJ
330 return m_helpFile;
331}
332
333QT_END_NAMESPACE
334

source code of qttools/src/assistant/assistant/cmdlineparser.cpp