Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qtpaths.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 Sune Vuorela <sune@kde.org>
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3
4#include <QCoreApplication>
5#include <QCommandLineParser>
6#include <QStandardPaths>
7#include <QHash>
8#include <QLibraryInfo>
9
10#include <algorithm>
11
12#include <stdio.h>
13
14#if QT_CONFIG(settings)
15# include <private/qlibraryinfo_p.h>
16# include <qmakelibraryinfo.h>
17# include <propertyprinter.h>
18# include <property.h>
19#endif
20
22
27static void message(const QString &string)
28{
29 fprintf(stdout, "%s\n", qPrintable(string));
30}
31
36Q_NORETURN static void error(const QString &message)
37{
38 fprintf(stderr, "%s\n", qPrintable(message));
39 ::exit(EXIT_FAILURE);
40}
41
43public:
44 const char *stringvalue;
47
51 QString mapName(const QString &s) const
52 {
53 return hasappname ? QString(s).replace("qtpaths", "<APPNAME>") : s;
54 }
55};
56
57static const StringEnum lookupTableData[] = {
58 { "AppConfigLocation", QStandardPaths::AppConfigLocation, true },
59 { "AppDataLocation", QStandardPaths::AppDataLocation, true },
60 { "AppLocalDataLocation", QStandardPaths::AppLocalDataLocation, true },
61 { "ApplicationsLocation", QStandardPaths::ApplicationsLocation, false },
62 { "CacheLocation", QStandardPaths::CacheLocation, true },
63 { "ConfigLocation", QStandardPaths::ConfigLocation, false },
64 { "DesktopLocation", QStandardPaths::DesktopLocation, false },
65 { "DocumentsLocation", QStandardPaths::DocumentsLocation, false },
66 { "DownloadLocation", QStandardPaths::DownloadLocation, false },
67 { "FontsLocation", QStandardPaths::FontsLocation, false },
68 { "GenericCacheLocation", QStandardPaths::GenericCacheLocation, false },
69 { "GenericConfigLocation", QStandardPaths::GenericConfigLocation, false },
70 { "GenericDataLocation", QStandardPaths::GenericDataLocation, false },
71 { "GenericStateLocation", QStandardPaths::GenericStateLocation, false },
72 { "HomeLocation", QStandardPaths::HomeLocation, false },
73 { "MoviesLocation", QStandardPaths::MoviesLocation, false },
74 { "MusicLocation", QStandardPaths::MusicLocation, false },
75 { "PicturesLocation", QStandardPaths::PicturesLocation, false },
76 { "PublicShareLocation", QStandardPaths::PublicShareLocation, false },
77 { "RuntimeLocation", QStandardPaths::RuntimeLocation, false },
78 { "StateLocation", QStandardPaths::StateLocation, true },
79 { "TemplatesLocation", QStandardPaths::TemplatesLocation, false },
80 { "TempLocation", QStandardPaths::TempLocation, false }
81};
82
87{
88 QStringList typelist;
89 for (const StringEnum &se : lookupTableData)
90 typelist << QString::fromLatin1(se.stringvalue);
91 std::sort(typelist.begin(), typelist.end());
92 return typelist;
93}
94
99static const StringEnum &parseLocationOrError(const QString &locationString)
100{
101 for (const StringEnum &se : lookupTableData)
102 if (locationString == QLatin1StringView(se.stringvalue))
103 return se;
104
105 QString message = QStringLiteral("Unknown location: %1");
106 error(message.arg(locationString));
107}
108
116{
117 int positionalArgumentCount = parser->positionalArguments().size();
118 if (positionalArgumentCount != 1)
119 error(QStringLiteral("Exactly one argument needed as searchitem"));
120 return parser->positionalArguments().constFirst();
121}
122
123int main(int argc, char **argv)
124{
125 QString qtconfManualPath;
126 QCoreApplication app(argc, argv);
127 app.setApplicationVersion(QTPATHS_VERSION_STR);
128
129#ifdef Q_OS_WIN
130 const QLatin1Char pathsep(';');
131#else
132 const QLatin1Char pathsep(':');
133#endif
134
135 QCommandLineParser parser;
136 parser.setApplicationDescription(QStringLiteral("Command line client to QStandardPaths and QLibraryInfo"));
137 parser.addPositionalArgument(QStringLiteral("[name]"), QStringLiteral("Name of file or directory"));
138 parser.addPositionalArgument(QStringLiteral("[properties]"), QStringLiteral("List of the Qt properties to query by the --qt-query argument."));
140 parser.addHelpOption();
141 parser.addVersionOption();
142
143 //setting up options
144 QCommandLineOption types(QStringLiteral("types"), QStringLiteral("Available location types."));
145 parser.addOption(types);
146
147 QCommandLineOption paths(QStringLiteral("paths"), QStringLiteral("Find paths for <type>."), QStringLiteral("type"));
148 parser.addOption(paths);
149
150 QCommandLineOption writablePath(QStringLiteral("writable-path"),
151 QStringLiteral("Find writable path for <type>."), QStringLiteral("type"));
152 parser.addOption(writablePath);
153
154 QCommandLineOption locateDir(QStringList() << QStringLiteral("locate-dir") << QStringLiteral("locate-directory"),
155 QStringLiteral("Locate directory [name] in <type>."), QStringLiteral("type"));
156 parser.addOption(locateDir);
157
158 QCommandLineOption locateDirs(QStringList() << QStringLiteral("locate-dirs") << QStringLiteral("locate-directories"),
159 QStringLiteral("Locate directories [name] in all paths for <type>."), QStringLiteral("type"));
160 parser.addOption(locateDirs);
161
162 QCommandLineOption locateFile(QStringLiteral("locate-file"),
163 QStringLiteral("Locate file [name] for <type>."), QStringLiteral("type"));
164 parser.addOption(locateFile);
165
166 QCommandLineOption locateFiles(QStringLiteral("locate-files"),
167 QStringLiteral("Locate files [name] in all paths for <type>."), QStringLiteral("type"));
168 parser.addOption(locateFiles);
169
170 QCommandLineOption findExe(QStringList() << QStringLiteral("find-exe") << QStringLiteral("find-executable"),
171 QStringLiteral("Find executable with [name]."));
172 parser.addOption(findExe);
173
175 QStringLiteral("Prints user readable name for <type>."), QStringLiteral("type"));
176 parser.addOption(display);
177
178 QCommandLineOption testmode(QStringList() << QStringLiteral("testmode") << QStringLiteral("test-mode"),
179 QStringLiteral("Use paths specific for unit testing."));
180 parser.addOption(testmode);
181
182 QCommandLineOption qtversion(QStringLiteral("qt-version"), QStringLiteral("Qt version."));
183 qtversion.setFlags(QCommandLineOption::HiddenFromHelp);
184 parser.addOption(qtversion);
185
186 QCommandLineOption installprefix(QStringLiteral("install-prefix"), QStringLiteral("Installation prefix for Qt."));
187 installprefix.setFlags(QCommandLineOption::HiddenFromHelp);
188 parser.addOption(installprefix);
189
190 QCommandLineOption bindir(QStringList() << QStringLiteral("binaries-dir") << QStringLiteral("binaries-directory"),
191 QStringLiteral("Location of Qt executables."));
192 bindir.setFlags(QCommandLineOption::HiddenFromHelp);
193 parser.addOption(bindir);
194
195 QCommandLineOption plugindir(QStringList() << QStringLiteral("plugin-dir") << QStringLiteral("plugin-directory"),
196 QStringLiteral("Location of Qt plugins."));
197 plugindir.setFlags(QCommandLineOption::HiddenFromHelp);
198 parser.addOption(plugindir);
199
201 QStringList() << QStringLiteral("qt-query") << QStringLiteral("query"),
202 QStringLiteral("List of Qt properties. Can be used standalone or with the "
203 "--query-format and --qtconf options."));
204 parser.addOption(query);
205
206 QCommandLineOption queryformat(QStringLiteral("query-format"),
207 QStringLiteral("Output format for --qt-query.\nSupported formats: qmake (default), json"),
208 QStringLiteral("format"));
209 queryformat.setDefaultValue("qmake");
210 parser.addOption(queryformat);
211
212 QCommandLineOption qtconf(QStringLiteral("qtconf"),
213 QStringLiteral("Path to qt.conf file that will be used to override the queried Qt properties."),
214 QStringLiteral("path"));
215 parser.addOption(qtconf);
216
217 parser.process(app);
218
220
221#if QT_CONFIG(settings)
222 if (parser.isSet(qtconf)) {
223 qtconfManualPath = parser.value(qtconf);
224 QLibraryInfoPrivate::setQtconfManualPath(&qtconfManualPath);
225 }
226#endif
227
229 if (parser.isSet(qtversion)) {
230 QString qtversionstring = QString::fromLatin1(QT_VERSION_STR);
231 results << qtversionstring;
232 }
233
234 if (parser.isSet(installprefix)) {
236 results << path;
237 }
238
239 if (parser.isSet(bindir)) {
241 results << path;
242 }
243
244 if (parser.isSet(plugindir)) {
246 results << path;
247 }
248
249 if (parser.isSet(types)) {
250 QStringList typesList = ::types();
251 results << typesList.join('\n');
252 }
253
255#if defined(Q_CC_GNU_ONLY) && Q_CC_GNU >= 1300 && Q_CC_GNU < 1500
256 QT_WARNING_DISABLE_GCC("-Wdangling-reference")
257#endif
258 if (parser.isSet(display)) {
261 results << location.mapName(text);
262 }
263
264 if (parser.isSet(paths)) {
267 results << location.mapName(paths.join(pathsep));
268 }
269
270 if (parser.isSet(writablePath)) {
271 const StringEnum &location = parseLocationOrError(parser.value(writablePath));
273 results << location.mapName(path);
274 }
275
276 if (parser.isSet(findExe)) {
277 QString searchitem = searchStringOrError(&parser);
279 results << path;
280 }
281
282 if (parser.isSet(locateDir)) {
283 const StringEnum &location = parseLocationOrError(parser.value(locateDir));
284 QString searchitem = searchStringOrError(&parser);
286 results << location.mapName(path);
287 }
288
289 if (parser.isSet(locateFile)) {
290 const StringEnum &location = parseLocationOrError(parser.value(locateFile));
291 QString searchitem = searchStringOrError(&parser);
293 results << location.mapName(path);
294 }
295
296 if (parser.isSet(locateDirs)) {
297 const StringEnum &location = parseLocationOrError(parser.value(locateDirs));
298 QString searchitem = searchStringOrError(&parser);
300 results << location.mapName(paths.join(pathsep));
301 }
302
303 if (parser.isSet(locateFiles)) {
304 const StringEnum &location = parseLocationOrError(parser.value(locateFiles));
305 QString searchitem = searchStringOrError(&parser);
307 results << location.mapName(paths.join(pathsep));
308 }
310
311#if !QT_CONFIG(settings)
312 if (parser.isSet(query) || parser.isSet(qtconf) || parser.isSet(queryformat)) {
313 error(QStringLiteral("--qt-query, --qtconf and --query-format options are not supported. The 'settings' feature is missing."));
314 }
315#else
316 if (parser.isSet(query)) {
317 if (!results.isEmpty()) {
318 QString errorMessage = QStringLiteral("Several options given, only one is supported at a time.");
320 }
321
322 PropertyPrinter printer;
323 if (parser.isSet(queryformat)) {
324 QString formatValue = parser.value(queryformat);
325 if (formatValue == "json") {
326 printer = jsonPropertyPrinter;
327 } else if (formatValue != "qmake") {
328 QString errorMessage = QStringLiteral("Invalid output format %1. Supported formats: qmake, json").arg(formatValue);
330 }
331 }
332
333 QStringList optionProperties = parser.positionalArguments();
334 QMakeProperty prop;
335 if (printer) {
336 return prop.queryProperty(optionProperties, printer);
337 }
338 return prop.queryProperty(optionProperties);
339 } else if (parser.isSet(queryformat)) {
340 error(QStringLiteral("--query-format is set, but --qt-query is not requested."));
341 }
342#endif
343
344 if (results.isEmpty()) {
345 parser.showHelp();
346 } else if (results.size() == 1) {
347 const QString &item = results.constFirst();
348 message(item);
349 if (item.isEmpty())
350 return EXIT_FAILURE;
351 } else {
352 QString errorMessage = QStringLiteral("Several options given, only one is supported at a time.");
354 }
355 return EXIT_SUCCESS;
356}
The QCommandLineOption class defines a possible command-line option. \inmodule QtCore.
The QCommandLineParser class provides a means for handling the command line options.
QString value(const QString &name) const
Returns the option value found for the given option name optionName, or an empty string if not found.
void addPositionalArgument(const QString &name, const QString &description, const QString &syntax=QString())
Defines an additional argument to the application, for the benefit of the help text.
QStringList positionalArguments() const
Returns a list of positional arguments.
void setSingleDashWordOptionMode(SingleDashWordOptionMode parsingMode)
Sets the parsing mode to singleDashWordOptionMode.
void setApplicationDescription(const QString &description)
Sets the application description shown by helpText().
bool addOption(const QCommandLineOption &commandLineOption)
Adds the option option to look for while parsing.
Q_NORETURN void showHelp(int exitCode=0)
Displays the help information, and exits the application.
bool isSet(const QString &name) const
Checks whether the option name was passed to the application.
void process(const QStringList &arguments)
Processes the command line arguments.
QCommandLineOption addVersionOption()
Adds the {-v} / {–version} option, which displays the version string of the application.
QCommandLineOption addHelpOption()
Adds help options to the command-line parser.
\inmodule QtCore
static void setApplicationVersion(const QString &version)
static QString path(LibraryPath p)
qsizetype size() const noexcept
Definition qlist.h:397
bool isEmpty() const noexcept
Definition qlist.h:401
const T & constFirst() const noexcept
Definition qlist.h:647
static QStringList locateAll(StandardLocation type, const QString &fileName, LocateOptions options=LocateFile)
[0]
static void setTestModeEnabled(bool testMode)
static QStringList standardLocations(StandardLocation type)
static QString writableLocation(StandardLocation type)
static QString displayName(StandardLocation type)
static QString findExecutable(const QString &executableName, const QStringList &paths=QStringList())
static QString locate(StandardLocation type, const QString &fileName, LocateOptions options=LocateFile)
StandardLocation
This enum describes the different locations that can be queried using methods such as QStandardPaths:...
\inmodule QtCore
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
QString & replace(qsizetype i, qsizetype len, QChar after)
Definition qstring.cpp:3824
static QString fromLatin1(QByteArrayView ba)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qstring.cpp:5871
const char * stringvalue
Definition qtpaths.cpp:44
bool hasappname
Definition qtpaths.cpp:46
QStandardPaths::StandardLocation enumvalue
Definition qtpaths.cpp:45
QString mapName(const QString &s) const
Definition qtpaths.cpp:51
int main()
[0]
QString text
struct wl_display * display
Definition linuxdmabuf.h:41
#define Q_NORETURN
#define QT_WARNING_POP
#define QT_WARNING_DISABLE_GCC(text)
#define QT_WARNING_PUSH
QList< QString > QStringList
Constructs a string list that contains the given string, str.
DBusConnection const char DBusError * error
GLint location
GLsizei GLenum GLenum * types
GLsizei const GLuint * paths
GLuint GLsizei const GLchar * message
GLdouble s
[6]
Definition qopenglext.h:235
GLenum query
GLsizei const GLchar *const * path
#define qPrintable(string)
Definition qstring.h:1531
#define QStringLiteral(str)
static QStringList types()
Definition qtpaths.cpp:86
static const StringEnum & parseLocationOrError(const QString &locationString)
Definition qtpaths.cpp:99
static const StringEnum lookupTableData[]
Definition qtpaths.cpp:57
static QString searchStringOrError(QCommandLineParser *parser)
Definition qtpaths.cpp:115
static QString errorMessage(QUrlPrivate::ErrorCode errorCode, const QString &errorSource, qsizetype errorPosition)
Definition qurl.cpp:3517
QGraphicsItem * item
QApplication app(argc, argv)
[0]
\inmodule QtCore \reentrant
Definition qchar.h:18