Qt 6.x
The Qt SDK
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 BSD-3-Clause
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#if QT_VERSION < QT_VERSION_CHECK(6,0,0)
65 { "DataLocation", QStandardPaths::DataLocation, true },
66#endif
67 { "DesktopLocation", QStandardPaths::DesktopLocation, false },
68 { "DocumentsLocation", QStandardPaths::DocumentsLocation, false },
69 { "DownloadLocation", QStandardPaths::DownloadLocation, false },
70 { "FontsLocation", QStandardPaths::FontsLocation, false },
71 { "GenericCacheLocation", QStandardPaths::GenericCacheLocation, false },
72 { "GenericConfigLocation", QStandardPaths::GenericConfigLocation, false },
73 { "GenericDataLocation", QStandardPaths::GenericDataLocation, false },
74 { "HomeLocation", QStandardPaths::HomeLocation, false },
75 { "MoviesLocation", QStandardPaths::MoviesLocation, false },
76 { "MusicLocation", QStandardPaths::MusicLocation, false },
77 { "PicturesLocation", QStandardPaths::PicturesLocation, false },
78 { "PublicShareLocation", QStandardPaths::PublicShareLocation, false },
79 { "RuntimeLocation", QStandardPaths::RuntimeLocation, false },
80 { "TemplatesLocation", QStandardPaths::TemplatesLocation, false },
81 { "TempLocation", QStandardPaths::TempLocation, false }
82};
83
88{
89 QStringList typelist;
90 for (const StringEnum &se : lookupTableData)
91 typelist << QString::fromLatin1(se.stringvalue);
92 std::sort(typelist.begin(), typelist.end());
93 return typelist;
94}
95
100static const StringEnum &parseLocationOrError(const QString &locationString)
101{
102 for (const StringEnum &se : lookupTableData)
103 if (locationString == QLatin1StringView(se.stringvalue))
104 return se;
105
106 QString message = QStringLiteral("Unknown location: %1");
107 error(message.arg(locationString));
108}
109
117{
118 int positionalArgumentCount = parser->positionalArguments().size();
119 if (positionalArgumentCount != 1)
120 error(QStringLiteral("Exactly one argument needed as searchitem"));
121 return parser->positionalArguments().constFirst();
122}
123
124int main(int argc, char **argv)
125{
126 QString qtconfManualPath;
127 QCoreApplication app(argc, argv);
128 app.setApplicationVersion(QTPATHS_VERSION_STR);
129
130#ifdef Q_OS_WIN
131 const QLatin1Char pathsep(';');
132#else
133 const QLatin1Char pathsep(':');
134#endif
135
136 QCommandLineParser parser;
137 parser.setApplicationDescription(QStringLiteral("Command line client to QStandardPaths and QLibraryInfo"));
138 parser.addPositionalArgument(QStringLiteral("[name]"), QStringLiteral("Name of file or directory"));
139 parser.addPositionalArgument(QStringLiteral("[properties]"), QStringLiteral("List of the Qt properties to query by the --qt-query argument."));
141 parser.addHelpOption();
142 parser.addVersionOption();
143
144 //setting up options
145 QCommandLineOption types(QStringLiteral("types"), QStringLiteral("Available location types."));
146 parser.addOption(types);
147
148 QCommandLineOption paths(QStringLiteral("paths"), QStringLiteral("Find paths for <type>."), QStringLiteral("type"));
149 parser.addOption(paths);
150
151 QCommandLineOption writablePath(QStringLiteral("writable-path"),
152 QStringLiteral("Find writable path for <type>."), QStringLiteral("type"));
153 parser.addOption(writablePath);
154
155 QCommandLineOption locateDir(QStringList() << QStringLiteral("locate-dir") << QStringLiteral("locate-directory"),
156 QStringLiteral("Locate directory [name] in <type>."), QStringLiteral("type"));
157 parser.addOption(locateDir);
158
159 QCommandLineOption locateDirs(QStringList() << QStringLiteral("locate-dirs") << QStringLiteral("locate-directories"),
160 QStringLiteral("Locate directories [name] in all paths for <type>."), QStringLiteral("type"));
161 parser.addOption(locateDirs);
162
163 QCommandLineOption locateFile(QStringLiteral("locate-file"),
164 QStringLiteral("Locate file [name] for <type>."), QStringLiteral("type"));
165 parser.addOption(locateFile);
166
167 QCommandLineOption locateFiles(QStringLiteral("locate-files"),
168 QStringLiteral("Locate files [name] in all paths for <type>."), QStringLiteral("type"));
169 parser.addOption(locateFiles);
170
171 QCommandLineOption findExe(QStringList() << QStringLiteral("find-exe") << QStringLiteral("find-executable"),
172 QStringLiteral("Find executable with [name]."));
173 parser.addOption(findExe);
174
176 QStringLiteral("Prints user readable name for <type>."), QStringLiteral("type"));
177 parser.addOption(display);
178
179 QCommandLineOption testmode(QStringList() << QStringLiteral("testmode") << QStringLiteral("test-mode"),
180 QStringLiteral("Use paths specific for unit testing."));
181 parser.addOption(testmode);
182
183 QCommandLineOption qtversion(QStringLiteral("qt-version"), QStringLiteral("Qt version."));
185 parser.addOption(qtversion);
186
187 QCommandLineOption installprefix(QStringLiteral("install-prefix"), QStringLiteral("Installation prefix for Qt."));
189 parser.addOption(installprefix);
190
191 QCommandLineOption bindir(QStringList() << QStringLiteral("binaries-dir") << QStringLiteral("binaries-directory"),
192 QStringLiteral("Location of Qt executables."));
194 parser.addOption(bindir);
195
196 QCommandLineOption plugindir(QStringList() << QStringLiteral("plugin-dir") << QStringLiteral("plugin-directory"),
197 QStringLiteral("Location of Qt plugins."));
199 parser.addOption(plugindir);
200
202 QStringList() << QStringLiteral("qt-query") << QStringLiteral("query"),
203 QStringLiteral("List of Qt properties. Can be used standalone or with the "
204 "--query-format and --qtconf options."));
205 parser.addOption(query);
206
207 QCommandLineOption queryformat(QStringLiteral("query-format"),
208 QStringLiteral("Output format for --qt-query.\nSupported formats: qmake (default), json"),
209 QStringLiteral("format"));
210 queryformat.setDefaultValue("qmake");
211 parser.addOption(queryformat);
212
213 QCommandLineOption qtconf(QStringLiteral("qtconf"),
214 QStringLiteral("Path to qt.conf file that will be used to override the queried Qt properties."),
215 QStringLiteral("path"));
216 parser.addOption(qtconf);
217
218 parser.process(app);
219
221
222#if QT_CONFIG(settings)
223 if (parser.isSet(qtconf)) {
224 qtconfManualPath = parser.value(qtconf);
225 QLibraryInfoPrivate::qtconfManualPath = &qtconfManualPath;
226 }
227#endif
228
230 if (parser.isSet(qtversion)) {
231 QString qtversionstring = QString::fromLatin1(QT_VERSION_STR);
232 results << qtversionstring;
233 }
234
235 if (parser.isSet(installprefix)) {
237 results << path;
238 }
239
240 if (parser.isSet(bindir)) {
242 results << path;
243 }
244
245 if (parser.isSet(plugindir)) {
247 results << path;
248 }
249
250 if (parser.isSet(types)) {
251 QStringList typesList = ::types();
252 results << typesList.join('\n');
253 }
254
256#if defined(Q_CC_GNU_ONLY) && Q_CC_GNU >= 1300 && Q_CC_GNU < 1400
257 QT_WARNING_DISABLE_GCC("-Wdangling-reference")
258#endif
259 if (parser.isSet(display)) {
262 results << location.mapName(text);
263 }
264
265 if (parser.isSet(paths)) {
268 results << location.mapName(paths.join(pathsep));
269 }
270
271 if (parser.isSet(writablePath)) {
272 const StringEnum &location = parseLocationOrError(parser.value(writablePath));
274 results << location.mapName(path);
275 }
276
277 if (parser.isSet(findExe)) {
278 QString searchitem = searchStringOrError(&parser);
280 results << path;
281 }
282
283 if (parser.isSet(locateDir)) {
284 const StringEnum &location = parseLocationOrError(parser.value(locateDir));
285 QString searchitem = searchStringOrError(&parser);
287 results << location.mapName(path);
288 }
289
290 if (parser.isSet(locateFile)) {
291 const StringEnum &location = parseLocationOrError(parser.value(locateFile));
292 QString searchitem = searchStringOrError(&parser);
294 results << location.mapName(path);
295 }
296
297 if (parser.isSet(locateDirs)) {
298 const StringEnum &location = parseLocationOrError(parser.value(locateDirs));
299 QString searchitem = searchStringOrError(&parser);
301 results << location.mapName(paths.join(pathsep));
302 }
303
304 if (parser.isSet(locateFiles)) {
305 const StringEnum &location = parseLocationOrError(parser.value(locateFiles));
306 QString searchitem = searchStringOrError(&parser);
308 results << location.mapName(paths.join(pathsep));
309 }
311
312#if !QT_CONFIG(settings)
313 if (parser.isSet(query) || parser.isSet(qtconf) || parser.isSet(queryformat)) {
314 error(QStringLiteral("--qt-query, --qtconf and --query-format options are not supported. The 'settings' feature is missing."));
315 }
316#else
317 if (parser.isSet(query)) {
318 if (!results.isEmpty()) {
319 QString errorMessage = QStringLiteral("Several options given, only one is supported at a time.");
321 }
322
323 PropertyPrinter printer;
324 if (parser.isSet(queryformat)) {
325 QString formatValue = parser.value(queryformat);
326 if (formatValue == "json") {
327 printer = jsonPropertyPrinter;
328 } else if (formatValue != "qmake") {
329 QString errorMessage = QStringLiteral("Invalid output format %1. Supported formats: qmake, json").arg(formatValue);
331 }
332 }
333
334 QStringList optionProperties = parser.positionalArguments();
335 QMakeProperty prop;
336 if (printer) {
337 return prop.queryProperty(optionProperties, printer);
338 }
339 return prop.queryProperty(optionProperties);
340 } else if (parser.isSet(queryformat)) {
341 error(QStringLiteral("--query-format is set, but --qt-query is not requested."));
342 }
343#endif
344
345 if (results.isEmpty()) {
346 parser.showHelp();
347 } else if (results.size() == 1) {
348 const QString &item = results.first();
349 message(item);
350 if (item.isEmpty())
351 return EXIT_FAILURE;
352 } else {
353 QString errorMessage = QStringLiteral("Several options given, only one is supported at a time.");
355 }
356 return EXIT_SUCCESS;
357}
The QCommandLineOption class defines a possible command-line option. \inmodule QtCore.
void setDefaultValue(const QString &defaultValue)
Sets the default value used for this option to defaultValue.
void setFlags(Flags aflags)
Set the set of flags that affect this command-line option to flags.
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:386
bool isEmpty() const noexcept
Definition qlist.h:390
T & first()
Definition qlist.h:628
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:127
QString & replace(qsizetype i, qsizetype len, QChar after)
Definition qstring.cpp:3794
static QString fromLatin1(QByteArrayView ba)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qstring.cpp:5710
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
DBusConnection const char DBusError * error
GLint location
GLsizei GLenum GLenum * types
GLsizei const GLuint * paths
GLuint GLsizei const GLchar * message
GLenum query
GLsizei const GLchar *const * path
GLdouble s
[6]
Definition qopenglext.h:235
#define qPrintable(string)
Definition qstring.h:1391
#define QStringLiteral(str)
static QStringList types()
Definition qtpaths.cpp:87
static const StringEnum & parseLocationOrError(const QString &locationString)
Definition qtpaths.cpp:100
static const StringEnum lookupTableData[]
Definition qtpaths.cpp:57
static QString searchStringOrError(QCommandLineParser *parser)
Definition qtpaths.cpp:116
static QString errorMessage(QUrlPrivate::ErrorCode errorCode, const QString &errorSource, qsizetype errorPosition)
Definition qurl.cpp:3503
QGraphicsItem * item
QApplication app(argc, argv)
[0]
\inmodule QtCore \reentrant
Definition qchar.h:17