QtBase  v6.3.1
src_corelib_tools_qcommandlineparser.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 David Faure <faure@kde.org>
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the documentation of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** BSD License Usage
18 ** Alternatively, you may use this file under the terms of the BSD license
19 ** as follows:
20 **
21 ** "Redistribution and use in source and binary forms, with or without
22 ** modification, are permitted provided that the following conditions are
23 ** met:
24 ** * Redistributions of source code must retain the above copyright
25 ** notice, this list of conditions and the following disclaimer.
26 ** * Redistributions in binary form must reproduce the above copyright
27 ** notice, this list of conditions and the following disclaimer in
28 ** the documentation and/or other materials provided with the
29 ** distribution.
30 ** * Neither the name of The Qt Company Ltd nor the names of its
31 ** contributors may be used to endorse or promote products derived
32 ** from this software without specific prior written permission.
33 **
34 **
35 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46 **
47 ** $QT_END_LICENSE$
48 **
49 ****************************************************************************/
50 
51 #include <qcommandlineparser.h>
52 
53 int main(int argc, char **argv)
54 {
55 
56 {
59 bool verbose = parser.isSet("verbose");
61 }
62 
63 {
65 QCoreApplication app(argc, argv);
67 QCommandLineOption verboseOption("verbose");
68 parser.addOption(verboseOption);
69 parser.process(app);
70 bool verbose = parser.isSet(verboseOption);
72 }
73 
74 {
77 // Usage: image-editor file
78 //
79 // Arguments:
80 // file The file to open.
81 parser.addPositionalArgument("file", QCoreApplication::translate("main", "The file to open."));
82 
83 // Usage: web-browser [urls...]
84 //
85 // Arguments:
86 // urls URLs to open, optionally.
87 parser.addPositionalArgument("urls", QCoreApplication::translate("main", "URLs to open, optionally."), "[urls...]");
88 
89 // Usage: cp source destination
90 //
91 // Arguments:
92 // source Source file to copy.
93 // destination Destination directory.
94 parser.addPositionalArgument("source", QCoreApplication::translate("main", "Source file to copy."));
95 parser.addPositionalArgument("destination", QCoreApplication::translate("main", "Destination directory."));
97 }
98 
99 {
101 QCoreApplication app(argc, argv);
103 
104 parser.addPositionalArgument("command", "The command to execute.");
105 
106 // Call parse() to find out the positional arguments.
108 
109 const QStringList args = parser.positionalArguments();
110 const QString command = args.isEmpty() ? QString() : args.first();
111 if (command == "resize") {
112  parser.clearPositionalArguments();
113  parser.addPositionalArgument("resize", "Resize the object to a new size.", "resize [resize_options]");
114  parser.addOption(QCommandLineOption("size", "New size.", "new_size"));
115  parser.process(app);
116  // ...
117 }
118 
119 /*
120 This code results in context-dependent help:
121 
122 $ tool --help
123 Usage: tool command
124 
125 Arguments:
126  command The command to execute.
127 
128 $ tool resize --help
129 Usage: tool resize [resize_options]
130 
131 Options:
132  --size <size> New size.
133 
134 Arguments:
135  resize Resize the object to a new size.
136 */
138 }
139 
140 {
143 parser.setApplicationDescription(QCoreApplication::translate("main", "The best application in the world"));
144 parser.addHelpOption();
146 }
147 
148 }
The QCommandLineOption class defines a possible command-line option. \inmodule QtCore.
The QCommandLineParser class provides a means for handling the command line options.
The QCoreApplication class provides an event loop for Qt applications without UI.
static QString translate(const char *context, const char *key, const char *disambiguation=nullptr, int n=-1)
static QStringList arguments()
The QString class provides a Unicode character string.
Definition: qstring.h:388
The QStringList class provides a list of strings.
parser
Definition: devices.py:74
#define QString()
Definition: parse-defines.h:51
int main(int argc, char **argv)
[1]
QApplication app(argc, argv)
[0]