Simple example of QCommandLineParser



  • Hi All,

    I would like to write (for self learning) very simple command application with options using QCommandLineParser and QCommandLineOption.

    Example:
    app.exe -a1 -a2

    Options will do some operations like number operations and print to console.
    Let's say c=a+b and then print c.

    I am beginner of QT and won't able to understand how using QCommandLineParser tell application to run code for a1 and/or a2 options.
    Could you please tell me how to do this?

    Btw. I've read good article:
    http://www.ics.com/blog/whats-new-qt-52-qcommandlineparser#.VMlcjk10zBR

    But I am not sure if there is explained how to run some code/functionality for specified option.

    Thanks,


  • Lifetime Qt Champion

    Hi,

    Yes it does explain that however did you took a look a the example provided "here":http://doc.qt.io/qt-5/qcommandlineparser.html#details ?



  • Thank you SGaist!

    After reading the blog and your link I have managed to create console application with arguments.

    Depends on the argument, I can run for example calculation functionality.
    I am posting my code and maybe it might be useful for somebody:

    @#include <QCoreApplication>
    #include <QCommandLineOption>
    #include <QCommandLineParser>
    #include <QTextStream>
    #include <QDebug>
    #include <QString>
    #include <QStringList>

    int main(int argc, char *argv[])
    {
    QCoreApplication a(argc, argv);
    QCoreApplication::setApplicationName("Application Name");
    QCoreApplication::setApplicationVersion("Version: 1");

    QCommandLineParser parser;
    parser.setApplicationDescription("Description: This is example of console application");
    parser.addHelpOption();
    parser.addVersionOption();
    
    QCommandLineOption showCalculation(QStringList() << "o" << "opt", QCoreApplication::translate("main", "Option is: fr") \
                                       ,QCoreApplication::translate("main", "opt"), "1");
    parser.addOption(showCalculation);
    
    parser.process(a);
    
    const QStringList args = parser.optionNames();
    if (args.size() < 1) {
        fprintf(stderr, "%s\n", qPrintable(QCoreApplication::translate("main", "Error: Must specify an argument.")));
        parser.showHelp(1);
    }
    
    QString opt1 = parser.value(showCalculation);
    
    if (opt1 != "fr") {
        fprintf(stderr, "%s\n", qPrintable(QCoreApplication::translate("main", "Error: Invalid format argument. Must be fr")));
        parser.showHelp(1);
    }
    
    if (opt1 == "fr")
    {
        int a =4, b = 5, c=0;
        c=a+b;
        qDebug() << "Sum: " << c;
    }
    
    //return a.exec&#40;&#41;;
    return 0;
    

    }
    @

    Example from command line:

    c:\untitled2.exe
    Error: Must specify an argument.
    Usage: untitled2.exe [options]
    This is example of console application

    Options:
    -?, -h, --help Displays this help.
    -v, --version Displays version information.
    -o, --opt <opt> Option is: fr

    c:\untitled2.exe -o abc
    Error: Invalid format argument. Must be fr
    Usage: untitled2.exe [options]
    This is example of console application

    Options:
    -?, -h, --help Displays this help.
    -v, --version Displays version information.
    -o, --opt <opt> Option is: fr

    c:\untitled2.exe -o fr
    Sum: 9


  • Lifetime Qt Champion

    Why don't you use the "isSet":http://doc.qt.io/qt-5/qcommandlineparser.html#isSet-2 function ?

    Also why two ifs ? Using else would make more sense


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.