Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Simple example of QCommandLineParser
Forum Updated to NodeBB v4.3 + New Features

Simple example of QCommandLineParser

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 29.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nagardd
    wrote on 28 Jan 2015, 22:13 last edited by
    #1

    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,

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 28 Jan 2015, 23:29 last edited by
      #2

      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 ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • N Offline
        N Offline
        nagardd
        wrote on 30 Jan 2015, 16:54 last edited by
        #3

        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

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 31 Jan 2015, 21:07 last edited by
          #4

          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

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0

          1/4

          28 Jan 2015, 22:13

          • Login

          • Login or register to search.
          1 out of 4
          • First post
            1/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved