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: Can't get command line arguments
Forum Updated to NodeBB v4.3 + New Features

QCommandLineParser: Can't get command line arguments

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 4 Posters 788 Views 2 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.
  • A Offline
    A Offline
    ademmler
    wrote on 27 Nov 2023, 17:42 last edited by ademmler
    #1

    Hi there,

    I try the first time <QCommandLineParser>. I have read the documentation and created an example. See below. By some unknown reason I have 2 issues.

    1. The given parameters are not recognized when I do:
      -i /path/to/inputfile
      -o /path/to/directory

    2. When I try to use an "=" with the option like in
      -i=/path/to/inputfile"
      -o=/path/to/directroy

    I get this error: Unexpected value after '-r'.

    #include <QCoreApplication>
    #include <QCommandLineParser>
    
    int main(int argc, char *argv[])
    {
        //Set application infos
        QCoreApplication app(argc, argv);
    
        //Set commandline paratmeters
        QCommandLineParser parser;
        parser.setApplicationDescription("My cmdline application.");
    
        //Add related argument options
        QCommandLineOption inputFileOption("i", QCoreApplication::translate("main", "Path to input pdf file"));
        parser.addOption(inputFileOption);
    
        QCommandLineOption outputDirectoryOption(QStringList() << "o" << "output-directory",
                                                 QCoreApplication::translate("main", "Save all output files into <directory>."));
        parser.addOption(outputDirectoryOption);
    
        // Process the actual command line arguments given by the user
        parser.process(app);
    
        QString inputFile = parser.value(inputFileOption);
        qInfo() << "Input: " << inputFile;
    
        QString outputDirectory = parser.value(outputDirectoryOption);
        qInfo() << "Output: " << outputDirectory;
    
        qInfo() << parser.optionNames();
    
        return app.exec();
        
        QCoreApplication::quit();
    }
    
    P 1 Reply Last reply 27 Nov 2023, 19:34
    0
    • H Offline
      H Offline
      hskoglund
      wrote on 27 Nov 2023, 18:07 last edited by
      #2

      Hi, maybe you forgot to add the value name, say like this:

      ,..
      //Add spectraproof related argument options
          QCommandLineOption inputFileOption("i", QCoreApplication::translate("main", "Path to input pdf file"),"file");
          parser.addOption(inputFileOption);
      
          QCommandLineOption outputDirectoryOption(QStringList() << "o" << "output-directory",
                                                   QCoreApplication::translate("main", "Save all output files into <directory>."),"dir");
          parser.addOption(outputDirectoryOption);
      ...
      
      
      A 1 Reply Last reply 27 Nov 2023, 19:16
      0
      • H hskoglund
        27 Nov 2023, 18:07

        Hi, maybe you forgot to add the value name, say like this:

        ,..
        //Add spectraproof related argument options
            QCommandLineOption inputFileOption("i", QCoreApplication::translate("main", "Path to input pdf file"),"file");
            parser.addOption(inputFileOption);
        
            QCommandLineOption outputDirectoryOption(QStringList() << "o" << "output-directory",
                                                     QCoreApplication::translate("main", "Save all output files into <directory>."),"dir");
            parser.addOption(outputDirectoryOption);
        ...
        
        
        A Offline
        A Offline
        ademmler
        wrote on 27 Nov 2023, 19:16 last edited by ademmler
        #3

        @hskoglund I tried your idea, but it did not change anything.

        If I do it like this it does work:

        QCommandLineOption outputResolutionOption("r", QCoreApplication::translate("main", "Rip output resolution in dpi"));
            outputResolutionOption.setValueName("resolution");
            outputResolutionOption.setDefaultValue("200");
            parser.addOption(outputResolutionOption);
        
        J 1 Reply Last reply 27 Nov 2023, 19:47
        0
        • A ademmler
          27 Nov 2023, 17:42

          Hi there,

          I try the first time <QCommandLineParser>. I have read the documentation and created an example. See below. By some unknown reason I have 2 issues.

          1. The given parameters are not recognized when I do:
            -i /path/to/inputfile
            -o /path/to/directory

          2. When I try to use an "=" with the option like in
            -i=/path/to/inputfile"
            -o=/path/to/directroy

          I get this error: Unexpected value after '-r'.

          #include <QCoreApplication>
          #include <QCommandLineParser>
          
          int main(int argc, char *argv[])
          {
              //Set application infos
              QCoreApplication app(argc, argv);
          
              //Set commandline paratmeters
              QCommandLineParser parser;
              parser.setApplicationDescription("My cmdline application.");
          
              //Add related argument options
              QCommandLineOption inputFileOption("i", QCoreApplication::translate("main", "Path to input pdf file"));
              parser.addOption(inputFileOption);
          
              QCommandLineOption outputDirectoryOption(QStringList() << "o" << "output-directory",
                                                       QCoreApplication::translate("main", "Save all output files into <directory>."));
              parser.addOption(outputDirectoryOption);
          
              // Process the actual command line arguments given by the user
              parser.process(app);
          
              QString inputFile = parser.value(inputFileOption);
              qInfo() << "Input: " << inputFile;
          
              QString outputDirectory = parser.value(outputDirectoryOption);
              qInfo() << "Output: " << outputDirectory;
          
              qInfo() << parser.optionNames();
          
              return app.exec();
              
              QCoreApplication::quit();
          }
          
          P Offline
          P Offline
          Pl45m4
          wrote on 27 Nov 2023, 19:34 last edited by
          #4

          @ademmler said in QCommandLineParser: Can't get command line arguments:

          When I try to use an "=" with the option like in
          -i=/path/to/inputfile"
          -o=/path/to/directroy

          I get this error: Unexpected value after '-r'

          The error you are getting and the code you show doesnt match. In your example code there is no -r option which produces the error.
          Only -r doesnt work or is everything else also crashing?


          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

          ~E. W. Dijkstra

          A 1 Reply Last reply 27 Nov 2023, 19:50
          1
          • A ademmler
            27 Nov 2023, 19:16

            @hskoglund I tried your idea, but it did not change anything.

            If I do it like this it does work:

            QCommandLineOption outputResolutionOption("r", QCoreApplication::translate("main", "Rip output resolution in dpi"));
                outputResolutionOption.setValueName("resolution");
                outputResolutionOption.setDefaultValue("200");
                parser.addOption(outputResolutionOption);
            
            J Offline
            J Offline
            JoeCFD
            wrote on 27 Nov 2023, 19:47 last edited by JoeCFD
            #5

            @ademmler this is what I did and it works.
            -w width=950

                QCommandLineOption widthOption(QStringList() << "w" << "width",
                        QCoreApplication::translate("main", "Defines the width of the main window."),
                        QCoreApplication::translate("main", "width"));
                parser.addOption(widthOption);
            
            A 1 Reply Last reply 27 Nov 2023, 19:52
            0
            • P Pl45m4
              27 Nov 2023, 19:34

              @ademmler said in QCommandLineParser: Can't get command line arguments:

              When I try to use an "=" with the option like in
              -i=/path/to/inputfile"
              -o=/path/to/directroy

              I get this error: Unexpected value after '-r'

              The error you are getting and the code you show doesnt match. In your example code there is no -r option which produces the error.
              Only -r doesnt work or is everything else also crashing?

              A Offline
              A Offline
              ademmler
              wrote on 27 Nov 2023, 19:50 last edited by
              #6

              @Pl45m4 the Hi, thx

              the other option "-o" crashed also.
              But as I wrote before I found a way how to do it.

              1 Reply Last reply
              0
              • J JoeCFD
                27 Nov 2023, 19:47

                @ademmler this is what I did and it works.
                -w width=950

                    QCommandLineOption widthOption(QStringList() << "w" << "width",
                            QCoreApplication::translate("main", "Defines the width of the main window."),
                            QCoreApplication::translate("main", "width"));
                    parser.addOption(widthOption);
                
                A Offline
                A Offline
                ademmler
                wrote on 27 Nov 2023, 19:52 last edited by
                #7

                @JoeCFD Hi thx, I will try your apporach.

                But I guess you use either "-w 950" or "--width=950" and not "-w width=950"?
                Correct?

                J 1 Reply Last reply 27 Nov 2023, 19:54
                0
                • A ademmler
                  27 Nov 2023, 19:52

                  @JoeCFD Hi thx, I will try your apporach.

                  But I guess you use either "-w 950" or "--width=950" and not "-w width=950"?
                  Correct?

                  J Offline
                  J Offline
                  JoeCFD
                  wrote on 27 Nov 2023, 19:54 last edited by JoeCFD
                  #8

                  @ademmler I use

                   "-w width=950"
                  

                  You see the stringlist match.

                  1 Reply Last reply
                  0

                  1/8

                  27 Nov 2023, 17:42

                  • Login

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