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.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
6int main(int argc, char **argv)
7{
8 {
9 QCommandLineParser parser;
10
11 //! [0]
12 bool verbose = parser.isSet("verbose");
13 //! [0]
14 }
15
16 {
17 //! [1]
18 QCoreApplication app(argc, argv);
19 QCommandLineParser parser;
20 QCommandLineOption verboseOption("verbose");
21 parser.addOption(verboseOption);
22 parser.process(app);
23 bool verbose = parser.isSet(verboseOption);
24 //! [1]
25 }
26
27 {
28 QCommandLineParser parser;
29 //! [2]
30 // Usage: image-editor file
31 //
32 // Arguments:
33 // file The file to open.
34 parser.addPositionalArgument("file", QCoreApplication::translate("main", "The file to open."));
35
36 // Usage: web-browser [urls...]
37 //
38 // Arguments:
39 // urls URLs to open, optionally.
40 parser.addPositionalArgument("urls", QCoreApplication::translate("main", "URLs to open, optionally."), "[urls...]");
41
42 // Usage: cp source destination
43 //
44 // Arguments:
45 // source Source file to copy.
46 // destination Destination directory.
47 parser.addPositionalArgument("source", QCoreApplication::translate("main", "Source file to copy."));
48 parser.addPositionalArgument("destination", QCoreApplication::translate("main", "Destination directory."));
49 //! [2]
50 }
51
52 {
53 //! [3]
54 QCoreApplication app(argc, argv);
55 QCommandLineParser parser;
56 parser.addHelpOption();
57
58 parser.addPositionalArgument("command", "The command to execute.");
59
60 // Call parse() to find out the positional arguments.
61 parser.parse(QCoreApplication::arguments());
62
63 const QStringList args = parser.positionalArguments();
64 const QString command = args.isEmpty() ? QString() : args.first();
65 if (command == "resize") {
66 parser.clearPositionalArguments();
67 parser.addPositionalArgument("resize", "Resize the object to a new size.", "resize [resize_options]");
68 parser.addOption(QCommandLineOption("size", "New size.", "new_size"));
69 }
70
71 // Call process() to handle --help and to check the options.
72 parser.process(app);
73
74 /*
75 This code results in context-dependent help:
76
77 $ tool --help
78 Usage: tool [options] command
79
80 Options:
81 -h, --help Displays help on commandline options.
82 --help-all Displays help, including generic Qt options.
83
84 Arguments:
85 command The command to execute.
86
87 $ tool resize --help
88 Usage: tool [options] resize [resize_options]
89
90 Options:
91 -h, --help Displays help on commandline options.
92 --help-all Displays help, including generic Qt options.
93 --size <new_size> New size.
94
95 Arguments:
96 resize Resize the object to a new size.
97 */
98 //! [3]
99 }
100}
int main(int argc, char *argv[])
[ctor_close]