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
src_corelib_tools_qcommandlineparser_main.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 David Faure <faure@kde.org>
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
5
7int main(int argc, char *argv[])
8{
9 QCoreApplication app(argc, argv);
10 QCoreApplication::setApplicationName("my-copy-program");
12
13 QCommandLineParser parser;
14 parser.setApplicationDescription("Test helper");
15 parser.addHelpOption();
16 parser.addVersionOption();
17 parser.addPositionalArgument("source", QCoreApplication::translate("main", "Source file to copy."));
18 parser.addPositionalArgument("destination", QCoreApplication::translate("main", "Destination directory."));
19
20 // A boolean option with a single name (-p)
21 QCommandLineOption showProgressOption("p", QCoreApplication::translate("main", "Show progress during copy"));
22 parser.addOption(showProgressOption);
23
24 // A boolean option with multiple names (-f, --force)
25 QCommandLineOption forceOption(QStringList() << "f" << "force",
26 QCoreApplication::translate("main", "Overwrite existing files."));
27 parser.addOption(forceOption);
28
29 // An option with a value
30 QCommandLineOption targetDirectoryOption(QStringList() << "t" << "target-directory",
31 QCoreApplication::translate("main", "Copy all source files into <directory>."),
32 QCoreApplication::translate("main", "directory"));
33 parser.addOption(targetDirectoryOption);
34
35 // Process the actual command line arguments given by the user
36 parser.process(app);
37
38 const QStringList args = parser.positionalArguments();
39 // source is args.at(0), destination is args.at(1)
40
41 bool showProgress = parser.isSet(showProgressOption);
42 bool force = parser.isSet(forceOption);
43 QString targetDir = parser.value(targetDirectoryOption);
44 // ...
45}
46
48
49void f() {
51 parser.addOptions({
52 // A boolean option with a single name (-p)
53 {"p",
54 QCoreApplication::translate("main", "Show progress during copy")},
55 // A boolean option with multiple names (-f, --force)
56 {{"f", "force"},
57 QCoreApplication::translate("main", "Overwrite existing files.")},
58 // An option with a value
59 {{"t", "target-directory"},
60 QCoreApplication::translate("main", "Copy all source files into <directory>."),
61 QCoreApplication::translate("main", "directory")},
62 });
64}
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 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.
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 QString translate(const char *context, const char *key, const char *disambiguation=nullptr, int n=-1)
\threadsafe
static void setApplicationName(const QString &application)
static void setApplicationVersion(const QString &version)
\inmodule QtCore
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
int main()
[0]
QList< QString > QStringList
Constructs a string list that contains the given string, str.
QApplication app(argc, argv)
[0]
QJSValueList args