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.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
6int main(int argc, char **argv)
7{
8
9{
12bool verbose = parser.isSet("verbose");
14}
15
16{
18QCoreApplication app(argc, argv);
20QCommandLineOption verboseOption("verbose");
21parser.addOption(verboseOption);
22parser.process(app);
23bool verbose = parser.isSet(verboseOption);
25}
26
27{
30// Usage: image-editor file
31//
32// Arguments:
33// file The file to open.
34parser.addPositionalArgument("file", QCoreApplication::translate("main", "The file to open."));
35
36// Usage: web-browser [urls...]
37//
38// Arguments:
39// urls URLs to open, optionally.
40parser.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.
47parser.addPositionalArgument("source", QCoreApplication::translate("main", "Source file to copy."));
48parser.addPositionalArgument("destination", QCoreApplication::translate("main", "Destination directory."));
50}
51
52{
54QCoreApplication app(argc, argv);
56
57parser.addPositionalArgument("command", "The command to execute.");
58
59// Call parse() to find out the positional arguments.
61
62const QStringList args = parser.positionalArguments();
63const QString command = args.isEmpty() ? QString() : args.first();
64if (command == "resize") {
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/*
73This code results in context-dependent help:
74
75$ tool --help
76Usage: tool command
77
78Arguments:
79 command The command to execute.
80
81$ tool resize --help
82Usage: tool resize [resize_options]
83
84Options:
85 --size <size> New size.
86
87Arguments:
88 resize Resize the object to a new size.
89*/
91}
92
93}
The QCommandLineOption class defines a possible command-line option. \inmodule QtCore.
The QCommandLineParser class provides a means for handling the command line options.
bool parse(const QStringList &arguments)
Parses the command line arguments.
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.
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.
void clearPositionalArguments()
Clears the definitions of additional arguments from the help text.
\inmodule QtCore
static QString translate(const char *context, const char *key, const char *disambiguation=nullptr, int n=-1)
\threadsafe
static QStringList arguments()
bool isEmpty() const noexcept
Definition qlist.h:401
T & first()
Definition qlist.h:645
\inmodule QtCore
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
int main()
[0]
QApplication app(argc, argv)
[0]
QJSValueList args