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. parse command line options outside of main()?
Forum Updated to NodeBB v4.3 + New Features

parse command line options outside of main()?

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 4.9k Views 3 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.
  • C Offline
    C Offline
    cdwijs
    wrote on last edited by
    #1

    Hi All,
    I am writing a program that does command line parsing. For now the lines that deal with this are in main.cpp, in the constructor. I would like to move this to it's own function, so re-using should be easier, and the function will have less lines. I encounter the following obstacles in my attempt:
    ->QCommandLineParser requires access to argc and *argv[], so I have to pass those to my function. This is not a big problem, I can just give them as arguments.
    ->QCommandLineParser requires access to the QCoreApplication instance. In a new empty Qt console application, this is a local variable. I think it's not a good idea to define this variable outside the main constructor.
    Is there a solution to break up the main function into smaller functions?
    My versions:
    Windows 7 enterprise SP1 64 bit
    Qt Creator 3.6.0 Based on Qt 5.5.1 (MSVC 2013, 32 bit)
    QT5.5, mingw492_32
    Cheers,
    Cedric

    1 Reply Last reply
    0
    • kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by
      #2

      Hello,

      ->QCommandLineParser requires access to argc and *argv[], so I have to pass those to my function. This is not a big problem, I can just give them as arguments.
      ->QCommandLineParser requires access to the QCoreApplication instance. In a new empty Qt console application, this is a local variable. I think it's not a good idea to define

      It requires neither of those. It requires a list of strings (QStringList) containing the command line (application path + arguments).

      outside the main constructor.

      The main constructor?

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      0
      • C Offline
        C Offline
        cdwijs
        wrote on last edited by
        #3

        Hi kshegunov,
        Thanks for your quick reply. I have made a minimal program that works:

        #include <QCoreApplication>
        #include <QCommandLineParser>
        #include <QDebug>
        
        int main(int argc, char *argv[])
        {
            QCoreApplication a(argc, argv);
            QCommandLineParser parser;
            parser.setApplicationDescription("Explaining text");
            parser.addHelpOption();
        
            //An option with a value
            QCommandLineOption targetDirectoryOption(QStringList() << "t" << "target-directory",
                    QCoreApplication::translate("main", "Copy all source files into <directory>."),
                    QCoreApplication::translate("main", "directory"));
            parser.addOption(targetDirectoryOption);
            // Process the actual command line arguments given by the user
            parser.process(a);
        
            QString target = parser.value(targetDirectoryOption);
            qDebug()<<target;
        
            return a.exec();
        }
        

        This version gives the following error when I uncomment
        parser.process(a);
        error: 'a' was not declared in this scope
        Without this line, I get the following run-time errors:
        QCommandLineParser: call process() or parse() before value
        QCommandLineParser: call process() or parse() before values

        #include <QCoreApplication>
        #include <QCommandLineParser>
        #include <QDebug>
        
        void parseCommandLine();
        
        int main(int argc, char *argv[])
        {
            QCoreApplication a(argc, argv);
            parseCommandLine();
            return a.exec();
        }
        
        void parseCommandLine()
        {
            QCommandLineParser parser;
            parser.setApplicationDescription("Explaining text");
            parser.addHelpOption();
        
            //An option with a value
            QCommandLineOption targetDirectoryOption(QStringList() << "t" << "target-directory",
                    QCoreApplication::translate("main", "Copy all source files into <directory>."),
                    QCoreApplication::translate("main", "directory"));
            parser.addOption(targetDirectoryOption);
            // Process the actual command line arguments given by the user
            //parser.process(a); //error: 'a' was not declared in this scope
        
            QString target = parser.value(targetDirectoryOption);
            qDebug()<<target;
        }
        

        I see the following page has an example, that passes a reference to the parser object.
        http://doc.qt.io/qt-5/qcommandlineparser.html
        I will try that and report back.
        Cheers,
        Cedric

        1 Reply Last reply
        0
        • C Offline
          C Offline
          cdwijs
          wrote on last edited by
          #4

          Hi All,
          The following works, so my problem is fixed:

          #include <QCoreApplication>
          #include <QCommandLineParser>
          #include <QDebug>
          
          void parseCommandLine(QCommandLineParser &parser);
          
          int main(int argc, char *argv[])
          {
              QCoreApplication a(argc, argv);
              QCommandLineParser parser;
              parseCommandLine(parser);
              return a.exec();
          }
          
          void parseCommandLine(QCommandLineParser &parser)
          {
          
              parser.setApplicationDescription("Explaining text");
              parser.addHelpOption();
          
              //An option with a value
              QCommandLineOption targetDirectoryOption(QStringList() << "t" << "target-directory",
                      QCoreApplication::translate("main", "Copy all source files into <directory>."),
                      QCoreApplication::translate("main", "directory"));
              parser.addOption(targetDirectoryOption);
              // Process the actual command line arguments given by the user
              parser.process(QCoreApplication::arguments());
          
              QString target = parser.value(targetDirectoryOption);
              qDebug()<<target;
          }
          

          Cheers,
          Cedric

          kshegunovK 1 Reply Last reply
          1
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #5

            also you can easily get the application instance wherever you want using QCoreApplication::instance() static method

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            1 Reply Last reply
            1
            • C cdwijs

              Hi All,
              The following works, so my problem is fixed:

              #include <QCoreApplication>
              #include <QCommandLineParser>
              #include <QDebug>
              
              void parseCommandLine(QCommandLineParser &parser);
              
              int main(int argc, char *argv[])
              {
                  QCoreApplication a(argc, argv);
                  QCommandLineParser parser;
                  parseCommandLine(parser);
                  return a.exec();
              }
              
              void parseCommandLine(QCommandLineParser &parser)
              {
              
                  parser.setApplicationDescription("Explaining text");
                  parser.addHelpOption();
              
                  //An option with a value
                  QCommandLineOption targetDirectoryOption(QStringList() << "t" << "target-directory",
                          QCoreApplication::translate("main", "Copy all source files into <directory>."),
                          QCoreApplication::translate("main", "directory"));
                  parser.addOption(targetDirectoryOption);
                  // Process the actual command line arguments given by the user
                  parser.process(QCoreApplication::arguments());
              
                  QString target = parser.value(targetDirectoryOption);
                  qDebug()<<target;
              }
              

              Cheers,
              Cedric

              kshegunovK Offline
              kshegunovK Offline
              kshegunov
              Moderators
              wrote on last edited by
              #6

              @cdwijs
              Why not pass the actual arguments instead? E.g:

              #include <QCoreApplication>
              #include <QCommandLineParser>
              #include <QDebug>
              
              void parseCommandLine(const QStringList &);
              
              int main(int argc, char *argv[])
              {
                  QCoreApplication a(argc, argv);
              
                  parseCommandLine(QCoreApplication::arguments());
              
                  return QCoreApplication::exec();
              }
              
              void parseCommandLine(const QStringList & arguments)
              {
                  QCommandLineParser parser;
                  // ...
                  parser.process(arguments); //< I advise QCommandLineParser::parse instead, since it doesn't call exit() on error.
                  // ...
              }
              

              Read and abide by the Qt Code of Conduct

              1 Reply Last reply
              2
              • C Offline
                C Offline
                cdwijs
                wrote on last edited by
                #7

                @kshegunov
                QStringList is new for me, that's the reason :-)
                Your solution is clean, as the main function now doesn't have to know anything about parsing.
                Now I can create a class that does nothing more than parsing the strings, and put them into a QSettings object.

                Thanks for your help,
                Cedric

                kshegunovK 1 Reply Last reply
                0
                • C cdwijs

                  @kshegunov
                  QStringList is new for me, that's the reason :-)
                  Your solution is clean, as the main function now doesn't have to know anything about parsing.
                  Now I can create a class that does nothing more than parsing the strings, and put them into a QSettings object.

                  Thanks for your help,
                  Cedric

                  kshegunovK Offline
                  kshegunovK Offline
                  kshegunov
                  Moderators
                  wrote on last edited by
                  #8

                  You're welcome. Happy coding!

                  Read and abide by the Qt Code of Conduct

                  1 Reply Last reply
                  1

                  • Login

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