Expanding upon the prior example, let’s look at how to receive and parse user command line arguments. This application will show how to use a GUI window to display the auto generated help text or the program version number, based on the passed arguments from the user.
class MainWindow : public QWidget
{
public:
enum ParseArgs {
Help,
Version,
Ok,
};
MainWindow();
private:
ParseArgs prepareCommandLine(const QStringList &arguments);
void showHelp();
void showVersion(QString appName);
QCommandLineParser m_parser;
};
The class declaration for MainWindow contains a public enum which will be used as the return value when parsing the command line arguments. The private section has three method declarations which are used in this demo. The variable m_parser is declared in the class instead of passing to our private methods.
MainWindow::MainWindow()
{
QApplication::setApplicationVersion("1.2.6");
QStringList argList = QCoreApplication::arguments();
QString appName = argList.first();
QString commandLine = argList.join(", ");
argList.append("other_A");
argList.append("other_B");
ParseArgs parseResult = prepareCommandLine(argList);
const QStringList args = m_parser.positionalArguments();
if (parseResult == ParseArgs::Help) {
showHelp();
std::exit(0);
} else if (parseResult == ParseArgs::Version) {
showVersion(appName);
std::exit(0);
}
//
setMinimumSize(400, 200);
QLabel *label_1 = new QLabel();
label_1->setText(
"Command Line Arguments and Positional Arguments");
label_1->setAlignment(Qt::AlignHCenter);
QLabel *label_2 = new QLabel();
label_2->setText(commandLine);
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(25);
layout1->addWidget(label_2);
layout1->addStretch();
QHBoxLayout *layout2 = new QHBoxLayout();
layout2->addStretch();
layout2->addWidget(close_pb);
layout2->addStretch();
QVBoxLayout *layoutMain = new QVBoxLayout(this);
layoutMain->addLayout(layout1);
layoutMain->addSpacing(20);
layoutMain->addLayout(layout2);
connect(close_pb, &QPushButton::clicked,
this, &QWidget::close);
}
On line 3 in the block shown above, we set the version number of this program so it can be retrieved later and displayed when the user wants version information.
The next few lines set up the argList variable which is passed to the private parseCommandLine() method. The enum value returned from this method will be used on lines 17 through 24 to display the help text or the version text. If neither of this options were requested the normal GUI window is displayed with the passed command line arguments.
prepareCommandLine()
In the following block of code lines 4 and 7 are used to add support for Help and Version. We are assigning the return values so we can test the QCommandLineOption at the end of this method.
This method adds two positional arguments, just like we did in Journal 42. In this code we have added help text as shown on lines 19 and 22.
In our prior example we used the QCommandLineParser method process(). In this code we are calling parse() instead so the caller can decide what actions to take and when. If the user passes --help
and you call process(), then the help text is displayed immediately in the console window. This is not the effect we wanted.
The remaining lines of this method test if the user passed
or --help
--version
on the command line. The enum value returned from this method can be tested by the caller and then the program can display the appropriate information or simply continue on with the application.
MainWindow::ParseArgs MainWindow::prepareCommandLine(
const QStringList &arguments)
{
const QCommandLineOption helpOption =
m_parser.addHelpOption();
const QCommandLineOption versionOption =
m_parser.addVersionOption();
QCommandLineOption formatItem("format",
"Description about the format option");
m_parser.addOption(formatItem);
QCommandLineOption warningItem("w",
"Info about warning option");
m_parser.addOption(warningItem);
m_parser.addPositionalArgument("extra_arg_1",
"Information about extra_arg_1");
m_parser.addPositionalArgument("extra_arg_2",
"Information about extra_arg_2");
m_parser.parse(arguments);
if (m_parser.isSet(helpOption)) {
return ParseArgs::Help;
} else if (m_parser.isSet(versionOption)) {
return ParseArgs::Version;
}
return ParseArgs::Ok;
}
Display Help or Version
If the user passes --help
on the command line the showHelp() method will be called and if --version
is passed then showVersion() is called. Using a QMessageBox is a nice way to display HTML formatted text which is readable for your users.
void MainWindow::showHelp()
{
QString text;
text.append("<html><head/><body><pre>");
text.append(m_parser.helpText().toHtmlEscaped());
text.append("</pre></body></html>");
QMessageBox msgBox(QMessageBox::Information,
QApplication::applicationName(), text, QMessageBox::Ok);
msgBox.exec();
}
void MainWindow::showVersion(QString appName)
{
QString text;
text.append("<html><body><pre>");
text.append(appName + "<br>");
text.append("Version # " +
QApplication::applicationVersion() + "<br>");
text.append("</pre></body></html>");
QMessageBox msgBox(QMessageBox::Information,
QApplication::applicationName(), text, QMessageBox::Ok);
msgBox.exec();
}
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. The only suggested modification is on line 2 of the CMakeLists.txt file, change “example_1” to “example_43”.