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