Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    Unsolved QCommandLineParser booleans

    General and Desktop
    6
    9
    297
    Loading More Posts
    • 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.
    • W
      Wolosocu last edited by

      In the details section for QCommandLineParser (https://doc.qt.io/qt-5/qcommandlineparser.html#details) the following example is given for C++11 compilers:

          parser.addOptions({
              // A boolean option with a single name (-p)
              {"p",
                  QCoreApplication::translate("main", "Show progress during copy")},
              // A boolean option with multiple names (-f, --force)
              {{"f", "force"},
                  QCoreApplication::translate("main", "Overwrite existing files.")},
              // An option with a value
              {{"t", "target-directory"},
                  QCoreApplication::translate("main", "Copy all source files into <directory>."),
                  QCoreApplication::translate("main", "directory")},
          });
      

      However the documentation does not tell you how to check for p or f when using this method. The examples on the page only show you how to check booleans when using a QCommandLineOption object, but you obviously don't have one handy when using the above syntax.

      How can this be done?

      jsulm 1 Reply Last reply Reply Quote 0
      • Kent-Dorfman
        Kent-Dorfman last edited by Kent-Dorfman

        I've never used the class, but it looks like you just check the member function isSet() after running the parse() method.

        parser.parse()
        if (parser.isSet("p")) { /* "-p" was passed */ }
        

        OK...I guess it's process(), not parse()

        1 Reply Last reply Reply Quote 4
        • Christian Ehrlicher
          Christian Ehrlicher Lifetime Qt Champion last edited by

          QParser::isSet() also takes a QString.

          Qt has to stay free or it will die.

          1 Reply Last reply Reply Quote 2
          • jsulm
            jsulm Lifetime Qt Champion @Wolosocu last edited by

            This post is deleted!
            1 Reply Last reply Reply Quote 1
            • W
              Wolosocu last edited by Wolosocu

              I should have clarified, the QCommandLineParser::isSet(const QString&) method uses the option's name, which is not set in the construction I showed. In other words, that doesn't work.

              In the example above if you try parser.isSet('p') it always returns false.

              KroMignon Christian Ehrlicher 2 Replies Last reply Reply Quote 0
              • KroMignon
                KroMignon @Wolosocu last edited by KroMignon

                @Wolosocu said in QCommandLineParser booleans:

                parser.isSet('p') i

                Did you try parser.isSet('p') or parser.isSet("p") ?

                For me this should work:

                int main(int argc, char *argv[])
                {
                    // Setting up QCoreApplication
                    QCoreApplication app(argc, argv);
                
                    // Setting up command line options
                    QCommandLineParser parser;
                    parser.addOptions({
                        // A boolean option with a single name (-p)
                        {"p",
                            QCoreApplication::translate("main", "Show progress during copy")},
                        // A boolean option with multiple names (-f, --force)
                        {{"f", "force"},
                            QCoreApplication::translate("main", "Overwrite existing files.")},
                        // An option with a value
                        {{"t", "target-directory"},
                            QCoreApplication::translate("main", "Copy all source files into <directory>."),
                            QCoreApplication::translate("main", "directory")},
                    });
                
                    // Process the actual command line arguments given by the user
                    parser.process(app);
                
                    if(parser.isSet("p"))
                    {
                        qDebug() << "Found";
                    }
                    ...
                }
                

                It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                1 Reply Last reply Reply Quote 1
                • SGaist
                  SGaist Lifetime Qt Champion last edited by

                  Hi,

                  parser.isSet('p') won't build as there's no overload for a char.

                  parser.isSet("p") works as expected provided you called parser.process(app); before checking for p.

                  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 Reply Quote 4
                  • Christian Ehrlicher
                    Christian Ehrlicher Lifetime Qt Champion @Wolosocu last edited by

                    @Wolosocu said in QCommandLineParser booleans:

                    method uses the option's name, which is not set in the construction I showed

                    You're wrong: QCommandLineOption::QCommandLineOption(const QString &name, const QString &description, const QString &valueName = QString(), const QString &defaultValue = QString())

                    Qt has to stay free or it will die.

                    W 1 Reply Last reply Reply Quote 0
                    • W
                      Wolosocu @Christian Ehrlicher last edited by

                      @Christian-Ehrlicher

                      Ah yes.

                      And of course today, using the isSet("p"); works even though I swear it wasn't last night. Ah well. Thanks for the answers everyone.

                      1 Reply Last reply Reply Quote 2
                      • First post
                        Last post