10

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?
László Papp
  • 51,870
  • 39
  • 111
  • 135
VALOD9
  • 566
  • 2
  • 6
  • 22

1 Answers1

13

Try this which has the analogy of -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 have access to the values:

QStringList QCommandLineParser::values(const QString & optionName) const

Returns a list of option values found for the given option name optionName, or an empty list if not found.

The name provided can be any long or short name of any option that was added with 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

Build

qmake && make

Output when --help is used

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>.

Run and Output

./main -t foo -t bar -> ("foo", "bar")
./main -t foo bar    -> ("foo")
Ganton
  • 208
  • 2
  • 10
László Papp
  • 51,870
  • 39
  • 111
  • 135
  • Don't become angry:) I understand, what you are talking about. I just don't want my virtual users to write `command --option arg1 --option arg2`, but `command --option arg1 arg2`. Are there some inbox solutions or I should write something specific for it? – VALOD9 Oct 27 '14 at 15:22
  • @VALOD9: since when can you write that for instance in case of clang for include paths or in any other commonly run software? Now, something like a separator would be nice so that you could write "foo,bar" maybe, but as long as you add such things, the complexity starts increasing exponentially. When I was working on this class with David Faure, we strived for simplicity with the most common use cases in mind. It is nearly impossible to write a generic parser for everyone's taste the API of which remains simple for the majority. – László Papp Oct 27 '14 at 15:25
  • I'm not a pro in command line interfaces:) I've just viewed what is new in Qt 5.2, Qt 5.3 and Qt 5.4 beta and tried to understand new features. So, you are saying that in the most of projects with CLI we do not have such a possibility? – VALOD9 Oct 27 '14 at 15:29
  • @VALOD9: my experience shows that separator is used in such cases, e.g. in gcc, but it is also non-standard if I can say that and confusing as well because -I (include path) does not work like that so they are inconsistent. This class was meant to be very simple that can be extended later if the need arises. Just a quick question: how would you know whether -t foo bar means -t foo + positional argument? But if you write -t foo,bar how will you know whether "foo,bar" is one string or multiple? It is getting complex easily as you can see. – László Papp Oct 27 '14 at 15:30