Ionică Bizău
Ionică Bizău

Reputation: 113385

Pass multiple parsing values to addPositionalArgument function

I was looking on how to parse the command line arguments. I found this:

// A boolean option with multiple names (-f, --force)
QCommandLineOption forceOption(QStringList() << "f" << "force",
        QCoreApplication::translate("main", "Overwrite existing files."));
parser.addOption(forceOption);

This works fine. But how can I add two values for a string value? For example, foo --source ... should be the same with foo -s ....

I tried:

parser.addPositionalArgument(
   QStringList() << "s" << "source",
   QCoreApplication::translate("main", "...")
);

But this throws an error:

error: no matching function for call to 
'QCommandLineParser::addPositionalArgument(QStringList&, QString)'
parser.addPositionalArgument(QStringList() << "t" << "title",
QCoreApplication::translate("main", "...."));

That's probably addPositionalArgument expects a string instead of a string list.

But how can I alias two values?

Upvotes: 0

Views: 2069

Answers (1)

msrd0
msrd0

Reputation: 8370

You don't use a positional argument for that. Positional arguments are arguments that need to appear in a specific order, and can have any value. They are no parameters introduced with something like -s or --source.

As you already found out, you can alias the two using a QStringList in QCommandLineOption. If you want an argument to follow, just specify this in the constructor:

QCommandLineOption sourceOption(
    QStringList() << "s" << "source",
    "Specify the source", // better translate that string
    "source", // the name of the following argument
    "something" // the default value of the following argument, not required
);
parser.addOption(sourceOption);

Upvotes: 1

Related Questions