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
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
4#include <QCommandLineParser>
5
6//! [0]
7int main(int argc, char *argv[])
8{
9 QCoreApplication app(argc, argv);
10 QCoreApplication::setApplicationName("my-copy-program");
11 QCoreApplication::setApplicationVersion("1.0");
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//! [0]
47
48void f(QCommandLineParser parser)
49{
50 //! [cxx11]
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 });
63 //! [cxx11]
64}
int main(int argc, char *argv[])
[ctor_close]
void f(int c)
[26]