I wonder, how can I use multiple- or sub-arguments with QCommandLineParser? For example:
/home/my_app --my_option_with_two_params first_param second_param --my-option-with-one-param param?
I wonder, how can I use multiple- or sub-arguments with QCommandLineParser? For example:
/home/my_app --my_option_with_two_params first_param second_param --my-option-with-one-param param?
试试这个类似于-I /my/include/path1 -I /my/include/path2
:
--my_option_with_two_params first_param --my_option_with_two_params second_param
...然后您可以使用此方法访问这些值:
QStringList QCommandLineParser::values(const QString & optionName) const
返回为给定选项名称 optionName 找到的选项值列表,如果未找到,则返回空列表。
提供的名称可以是使用 addOption() 添加的任何选项的任何长名称或短名称。
在这里,您可以找到一个有效的简单测试用例:
#include <QCoreApplication>
#include <QCommandLineParser>
#include <QCommandLineOption>
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
QCoreApplication::setApplicationName("multiple-values-program");
QCoreApplication::setApplicationVersion("1.0");
QCommandLineParser parser;
parser.setApplicationDescription("Test helper");
parser.addHelpOption();
parser.addVersionOption();
QCommandLineOption targetDirectoryOption(QStringList() << "t" << "target-directory",
QCoreApplication::translate("main", "Copy all source files into <directory>."),
QCoreApplication::translate("main", "directory"));
parser.addOption(targetDirectoryOption);
parser.process(app);
qDebug() << parser.values(targetDirectoryOption);
return 0;
}
TEMPLATE = app
TARGET = main
QT = core
SOURCES += main.cpp
qmake && make
--help
使用时输出Usage: main [options]
Test helper
Options:
-h, --help Displays this help.
-v, --version Displays version information.
-t, --target-directory <directory> Copy all source files into <directory>.
./main -t foo -t bar -> ("foo", "bar")
./main -t foo bar -> ("foo")