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. QCommandLineParser booleans
QtWS25 Last Chance

QCommandLineParser booleans

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 6 Posters 946 Views
  • 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 Offline
    W Offline
    Wolosocu
    wrote on last edited by
    #1

    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?

    jsulmJ 1 Reply Last reply
    0
    • Kent-DorfmanK Offline
      Kent-DorfmanK Offline
      Kent-Dorfman
      wrote on last edited by Kent-Dorfman
      #2

      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
      4
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #3

        QParser::isSet() also takes a QString.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        2
        • W Wolosocu

          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?

          jsulmJ Online
          jsulmJ Online
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4
          This post is deleted!
          1 Reply Last reply
          1
          • W Offline
            W Offline
            Wolosocu
            wrote on last edited by Wolosocu
            #5

            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.

            KroMignonK Christian EhrlicherC 2 Replies Last reply
            0
            • W 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.

              KroMignonK Offline
              KroMignonK Offline
              KroMignon
              wrote on last edited by KroMignon
              #6

              @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
              1
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                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
                4
                • W 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.

                  Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @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 Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  W 1 Reply Last reply
                  0
                  • Christian EhrlicherC Christian Ehrlicher

                    @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())

                    W Offline
                    W Offline
                    Wolosocu
                    wrote on last edited by
                    #9

                    @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
                    2

                    • Login

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