QCommandLineParser: Can't get command line arguments
-
wrote on 27 Nov 2023, 17:42 last edited by ademmler
Hi there,
I try the first time <QCommandLineParser>. I have read the documentation and created an example. See below. By some unknown reason I have 2 issues.
-
The given parameters are not recognized when I do:
-i /path/to/inputfile
-o /path/to/directory -
When I try to use an "=" with the option like in
-i=/path/to/inputfile"
-o=/path/to/directroy
I get this error: Unexpected value after '-r'.
#include <QCoreApplication> #include <QCommandLineParser> int main(int argc, char *argv[]) { //Set application infos QCoreApplication app(argc, argv); //Set commandline paratmeters QCommandLineParser parser; parser.setApplicationDescription("My cmdline application."); //Add related argument options QCommandLineOption inputFileOption("i", QCoreApplication::translate("main", "Path to input pdf file")); parser.addOption(inputFileOption); QCommandLineOption outputDirectoryOption(QStringList() << "o" << "output-directory", QCoreApplication::translate("main", "Save all output files into <directory>.")); parser.addOption(outputDirectoryOption); // Process the actual command line arguments given by the user parser.process(app); QString inputFile = parser.value(inputFileOption); qInfo() << "Input: " << inputFile; QString outputDirectory = parser.value(outputDirectoryOption); qInfo() << "Output: " << outputDirectory; qInfo() << parser.optionNames(); return app.exec(); QCoreApplication::quit(); }
-
-
wrote on 27 Nov 2023, 18:07 last edited by
Hi, maybe you forgot to add the value name, say like this:
,.. //Add spectraproof related argument options QCommandLineOption inputFileOption("i", QCoreApplication::translate("main", "Path to input pdf file"),"file"); parser.addOption(inputFileOption); QCommandLineOption outputDirectoryOption(QStringList() << "o" << "output-directory", QCoreApplication::translate("main", "Save all output files into <directory>."),"dir"); parser.addOption(outputDirectoryOption); ...
-
Hi, maybe you forgot to add the value name, say like this:
,.. //Add spectraproof related argument options QCommandLineOption inputFileOption("i", QCoreApplication::translate("main", "Path to input pdf file"),"file"); parser.addOption(inputFileOption); QCommandLineOption outputDirectoryOption(QStringList() << "o" << "output-directory", QCoreApplication::translate("main", "Save all output files into <directory>."),"dir"); parser.addOption(outputDirectoryOption); ...
wrote on 27 Nov 2023, 19:16 last edited by ademmler@hskoglund I tried your idea, but it did not change anything.
If I do it like this it does work:
QCommandLineOption outputResolutionOption("r", QCoreApplication::translate("main", "Rip output resolution in dpi")); outputResolutionOption.setValueName("resolution"); outputResolutionOption.setDefaultValue("200"); parser.addOption(outputResolutionOption);
-
Hi there,
I try the first time <QCommandLineParser>. I have read the documentation and created an example. See below. By some unknown reason I have 2 issues.
-
The given parameters are not recognized when I do:
-i /path/to/inputfile
-o /path/to/directory -
When I try to use an "=" with the option like in
-i=/path/to/inputfile"
-o=/path/to/directroy
I get this error: Unexpected value after '-r'.
#include <QCoreApplication> #include <QCommandLineParser> int main(int argc, char *argv[]) { //Set application infos QCoreApplication app(argc, argv); //Set commandline paratmeters QCommandLineParser parser; parser.setApplicationDescription("My cmdline application."); //Add related argument options QCommandLineOption inputFileOption("i", QCoreApplication::translate("main", "Path to input pdf file")); parser.addOption(inputFileOption); QCommandLineOption outputDirectoryOption(QStringList() << "o" << "output-directory", QCoreApplication::translate("main", "Save all output files into <directory>.")); parser.addOption(outputDirectoryOption); // Process the actual command line arguments given by the user parser.process(app); QString inputFile = parser.value(inputFileOption); qInfo() << "Input: " << inputFile; QString outputDirectory = parser.value(outputDirectoryOption); qInfo() << "Output: " << outputDirectory; qInfo() << parser.optionNames(); return app.exec(); QCoreApplication::quit(); }
wrote on 27 Nov 2023, 19:34 last edited by@ademmler said in QCommandLineParser: Can't get command line arguments:
When I try to use an "=" with the option like in
-i=/path/to/inputfile"
-o=/path/to/directroyI get this error: Unexpected value after '-r'
The error you are getting and the code you show doesnt match. In your example code there is no
-r
option which produces the error.
Only-r
doesnt work or is everything else also crashing? -
-
@hskoglund I tried your idea, but it did not change anything.
If I do it like this it does work:
QCommandLineOption outputResolutionOption("r", QCoreApplication::translate("main", "Rip output resolution in dpi")); outputResolutionOption.setValueName("resolution"); outputResolutionOption.setDefaultValue("200"); parser.addOption(outputResolutionOption);
wrote on 27 Nov 2023, 19:47 last edited by JoeCFD@ademmler this is what I did and it works.
-w width=950QCommandLineOption widthOption(QStringList() << "w" << "width", QCoreApplication::translate("main", "Defines the width of the main window."), QCoreApplication::translate("main", "width")); parser.addOption(widthOption);
-
@ademmler said in QCommandLineParser: Can't get command line arguments:
When I try to use an "=" with the option like in
-i=/path/to/inputfile"
-o=/path/to/directroyI get this error: Unexpected value after '-r'
The error you are getting and the code you show doesnt match. In your example code there is no
-r
option which produces the error.
Only-r
doesnt work or is everything else also crashing? -
@ademmler this is what I did and it works.
-w width=950QCommandLineOption widthOption(QStringList() << "w" << "width", QCoreApplication::translate("main", "Defines the width of the main window."), QCoreApplication::translate("main", "width")); parser.addOption(widthOption);
-
@JoeCFD Hi thx, I will try your apporach.
But I guess you use either "-w 950" or "--width=950" and not "-w width=950"?
Correct?
1/8