In this example the command line is retrieved and the arguments passed by the user are displayed. Two extra positional arguments are added by the program to demonstrate the QCommandLineParser class.
#include <QtCore>
#include <QtGui>
class MainWindow : public QWidget
{
public:
MainWindow();
private:
void prepareCommandLine(const QStringList &arguments)
QCommandLineParser m_parser;
};
The MainWindow class declares one method and a single data member which is used to store the command line values passed by the user.
MainWindow::MainWindow()
{
QStringList argList = QCoreApplication::arguments();
QString appName = argList.takeFirst();
QString commandLine = argList.join(", ");
prepareCommandLine( {appName, "other_A", "other_B"} );
const QStringList args = m_parser.positionalArguments();
//
setMinimumSize(400, 200);
QLabel *label_1 = new QLabel();
label_1->setText("Application Name: " + appName);
QLabel *label_2 = new QLabel();
label_2->setText("Command Line Arguements: "
+ commandLine);
QLabel *label_3 = new QLabel();
label_3->setText("Extra Positional Argument 1: "
+ args[0]);
QLabel *label_4 = new QLabel();
label_4->setText("Extra Positional Argument 2: "
+ args[1]);
QPushButton *close_pb = new QPushButton();
close_pb->setText("Close");
QVBoxLayout *layout1 = new QVBoxLayout();
layout1->setContentsMargins(20, 20, 20, 20);
layout1->addWidget(label_1);
layout1->addSpacing(20);
layout1->addWidget(label_2);
layout1->addSpacing(20);
layout1->addWidget(label_3);
layout1->addSpacing(20);
layout1->addWidget(label_4);
layout1->addStretch();
QHBoxLayout *layout2 = new QHBoxLayout();
layout2->addStretch();
layout2->addWidget(close_pb);
layout2->addStretch();
QVBoxLayout *layoutMain = new QVBoxLayout(this);
layoutMain->setContentsMargins(50, 55, 80, 15);
layoutMain->addLayout(layout1);
layoutMain->addSpacing(75);
layoutMain->addLayout(layout2);
connect(close_pb, &QPushButton::clicked,
this, &QWidget::close);
}
On line 3 in the main block of code is a call to the static method arguments(). This method returns a string list containing the values entered on the command line by the user. Line 5 removes the first element in the string list, which is the full path name to the application.
On the next line the join() method concatenates the remaining elements of argList into one string.
Each value is separated by the passed string, which happens to be a comma followed by two spaces. The result contains the actual values which were passed on the command line. This example does not do anything with the result, however a future example will provide additional code and show how to process the command line values.
Options and Positional Arguments
Options can either have short names or long names and are preceded by either one single dash or two dashes. An option can be marked as requiring a value or simply to set some condition.
Positional Arguments are not preceded by a dash. These are typically used to pass something like a file name or directory. They must be supplied in the right order according to the application requirements.
Line 8 calls prepareCommandLine() which is a method in MainWindow. This method shows how to add two extra positional arguments and then calls process().
User Interface
The remander of this code which starts on line 12 is the user interface and will display the program name, the command line arguments passed in by the users, and the two extra positional arguments. The call to connect() on line 54 sets up a Signal / Slot connection to close the program when the user clicks the Close push button.
prepareCommandLine()
This method adds two extra positional arguments. The passed strings on lines 4 and 5 are used when displaying help text. This will be shown in a future example.
The call to process() on line 7 will read the values in arguments and store them in the QCommnadLineParse object, as if they were passed on the command line by the user.
void MainWindow::prepareCommandLine(
const QStringList &arguments)
{
m_parser.addPositionalArgument("extra_arg_1", "");
m_parser.addPositionalArgument("extra_arg_2", "");
m_parser.process(arguments);
}
Main Function
Since the source code for main() has not changed there is no need to show it again. Refer to example 3 or download the full source for this example.
Running the Example
To build and run this example use the same CMake build file and commands as we showed for the first example. Line 2 of the CMakeLists.txt file needs to be changed “example_1” to “example_42”.
For this example, when you start the application additional values must be passed on the command line. The following is an example of what to pass.
example_42 --format=short -w=all userValue1 userValue2