How to use QCommandLineParser for multi-argument arguments?

I wonder how I can use multiple or sub-arguments with QCommandLineParser ? For instance:

/home/my_app --my_option_with_two_params first_param second_param --my-option-with-one-param param? 
+5
source share
1 answer

Try this, which has an analogy with -I /my/include/path1 -I /my/include/path2 :

  --my_option_with_two_params first_param --my_option_with_two_params second_param 

... and then you can use this method to access the values:

QStringList QCommandLineParser :: values ​​(const QString and optionName) const

Returns a list of parameter values ​​found for the given parameter name optionName, or an empty list if it is not found.

The name provided may be any long or short name of any option added using addOption ().

Here you can find a simple test case that works:

main.cpp

 #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; } 

main.pro

 TEMPLATE = app TARGET = main QT = core SOURCES += main.cpp 

Assembly and launch

 qmake && make 

Start and exit

 ./main -t foo -t bar -> ("foo", "bar") ./main -t foo bar -> ("foo") 
+7
source

Source: https://habr.com/ru/post/1205610/


All Articles