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
57 parser.addPositionalArgument("command", "The command to execute.");
58
59 // Call parse() to find out the positional arguments.
60 parser.parse(QCoreApplication::arguments());
61
62 const QStringList args = parser.positionalArguments();
63 const QString command = args.isEmpty() ? QString() : args.first();
64 if (command == "resize") {
65 parser.clearPositionalArguments();
66 parser.addPositionalArgument("resize", "Resize the object to a new size.", "resize [resize_options]");
67 parser.addOption(QCommandLineOption("size", "New size.", "new_size"));
68 parser.process(app);
69 // ...
70 }
71
72 /*
73 This code results in context-dependent help:
74
75 $ tool --help
76 Usage: tool command
77
78 Arguments:
79 command The command to execute.
80
81 $ tool resize --help
82 Usage: tool resize [resize_options]
83
84 Options:
85 --size <size> New size.
86
87 Arguments:
88 resize Resize the object to a new size.
89 */
90 //! [3]
91 }
92}
int main(int argc, char *argv[])
[ctor_close]