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. How does QCommandLineOption::setDefaultValue work?
Forum Updated to NodeBB v4.3 + New Features

How does QCommandLineOption::setDefaultValue work?

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 4 Posters 944 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.
  • S Offline
    S Offline
    StudentDev9923
    wrote on last edited by
    #1

    I have this test option:

    QCommandLineOption testOption(QStringList() << "t" << "test", "This is a test", "int");

    I want to set a default value of 0 so I have tried to do it like this:
    testOption.setDefaultValue(1);

    but when I call it on the command line I get Missing value after '-test'.

    I'm new to command line stuff so I probably misunderstood something.

    Christian EhrlicherC W 2 Replies Last reply
    0
    • S StudentDev9923

      I have this test option:

      QCommandLineOption testOption(QStringList() << "t" << "test", "This is a test", "int");

      I want to set a default value of 0 so I have tried to do it like this:
      testOption.setDefaultValue(1);

      but when I call it on the command line I get Missing value after '-test'.

      I'm new to command line stuff so I probably misunderstood something.

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

      @StudentDev9923 said in How does QCommandLineOption::setDefaultValue work?:

      but when I call it on the command line I get Missing value after '-test'.

      Yes because when you set the option on the command line you also have to set a value. The documentation is clear about it:
      "The default value is used if the user of the application does not specify the option on the command line."

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

      S 1 Reply Last reply
      1
      • Christian EhrlicherC Christian Ehrlicher

        @StudentDev9923 said in How does QCommandLineOption::setDefaultValue work?:

        but when I call it on the command line I get Missing value after '-test'.

        Yes because when you set the option on the command line you also have to set a value. The documentation is clear about it:
        "The default value is used if the user of the application does not specify the option on the command line."

        S Offline
        S Offline
        StudentDev9923
        wrote on last edited by
        #3

        @Christian-Ehrlicher Thx for the reply. I'm still a little confused about when the default value comes into play.

        If i call my program: "testProgram.exe -t" and don't specify a value to go with t it doesn't use the default value?

        When does the the default value come into play then?

        Is there an other option I can use that makes it so if a user doesn't specify a value after a command it goes to a set default value instead?

        Christian EhrlicherC 1 Reply Last reply
        0
        • S StudentDev9923

          @Christian-Ehrlicher Thx for the reply. I'm still a little confused about when the default value comes into play.

          If i call my program: "testProgram.exe -t" and don't specify a value to go with t it doesn't use the default value?

          When does the the default value come into play then?

          Is there an other option I can use that makes it so if a user doesn't specify a value after a command it goes to a set default value instead?

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

          @StudentDev9923 said in How does QCommandLineOption::setDefaultValue work?:

          When does the the default value come into play then?

          When you don't specify the option on the command line. "testProgram.exe -t" specifies the option on the command line without a param.

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

          JonBJ 1 Reply Last reply
          2
          • Christian EhrlicherC Christian Ehrlicher

            @StudentDev9923 said in How does QCommandLineOption::setDefaultValue work?:

            When does the the default value come into play then?

            When you don't specify the option on the command line. "testProgram.exe -t" specifies the option on the command line without a param.

            JonBJ Online
            JonBJ Online
            JonB
            wrote on last edited by
            #5

            @Christian-Ehrlicher
            I believe @StudentDev9923 wants an option which takes an optional value after it (which may be omitted(. Like:

            command                # No option `t` specified at all, that's (probably) OK
            command -t 100         # `-t` specified with parameter `100` (with or without space before)
            command -t             # `-t` specified, no parameter, code should default it to value `50`, say
            

            I don't think QCommandLineOption supports this (the last two cases). I think ti requires an option to be either followed by a value or not, it can't handle handle optional when parsing.

            Christian EhrlicherC 1 Reply Last reply
            0
            • JonBJ JonB

              @Christian-Ehrlicher
              I believe @StudentDev9923 wants an option which takes an optional value after it (which may be omitted(. Like:

              command                # No option `t` specified at all, that's (probably) OK
              command -t 100         # `-t` specified with parameter `100` (with or without space before)
              command -t             # `-t` specified, no parameter, code should default it to value `50`, say
              

              I don't think QCommandLineOption supports this (the last two cases). I think ti requires an option to be either followed by a value or not, it can't handle handle optional when parsing.

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

              @JonB I'm aware of it but how should a parser parse such a thing? How does it know the next arg is an option or an argument (accidentally starting with a '-') ? I've never seen a program which is using such a strange convention.

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

              JonBJ 1 Reply Last reply
              0
              • Christian EhrlicherC Christian Ehrlicher

                @JonB I'm aware of it but how should a parser parse such a thing? How does it know the next arg is an option or an argument (accidentally starting with a '-') ? I've never seen a program which is using such a strange convention.

                JonBJ Online
                JonBJ Online
                JonB
                wrote on last edited by JonB
                #7

                @Christian-Ehrlicher
                I do not disagree with you. I am just guessing what I think the OP is saying he wants, but can't have from Qt.

                Example which probably implements what OP wants, man getopt:

                Two colons
                mean an option takes an optional arg; if there is text in the
                current argv-element (i.e., in the same word as the option name
                itself, for example, "-oarg"), then it is returned in optarg,
                otherwise optarg is set to zero. This is a GNU extension

                This would work/apply if the syntax requires -t100, I agree not -t 100.

                1 Reply Last reply
                0
                • S StudentDev9923

                  I have this test option:

                  QCommandLineOption testOption(QStringList() << "t" << "test", "This is a test", "int");

                  I want to set a default value of 0 so I have tried to do it like this:
                  testOption.setDefaultValue(1);

                  but when I call it on the command line I get Missing value after '-test'.

                  I'm new to command line stuff so I probably misunderstood something.

                  W Offline
                  W Offline
                  wrosecrans
                  wrote on last edited by
                  #8

                  @StudentDev9923

                  Basically, what you want would make parsing your command line syntax ambiguous so it's not really supported with any command line parsing library. A typical command line utility works on some file like

                  my_cool_utility some_file.txt
                  

                  where it does something useful with "positional arguments," i.e. some arguments don't have dash something before them. So if you also have some numbered tests you can run on the file...

                  my_cool_utility --test 42 some_file.txt
                  

                  Okay, cool, run test number 42. But...

                  my_cool_utility --test some_file.txt
                  

                  Uh, oh. Now it's hard to tell if you want to run a test named "some_file.txt" or if that's the input file! You've specified that the test number is supposed to be an integer, so in yoru simple example you can kinda sorta infer the intent rather than throwing an error message and giving up. But in the general case "the thing after --test may or may not apply to test" is hard to formalize. So the convention is that those sort of parameters must be fully supplied, or not at all. That guarantees the syntax is logically internally consistent, even if it's occasionally slightly annoying. Add a separate flag that never takes a number like

                  my_cool_utility   --default-test   some_file.txt
                  

                  And make it mutually exclusive so you can not say both --default-test and --test=42 in the same invocation because that would be confusing about which test the user actually wants to run. In documentation you'll sometimes see something like

                  my_cool_utility   [--default-test|--test=<id>]   <filename>
                  

                  Where the "two things in brackets divided by an or symbol" means you can only pass one or the other but not both. The "set Default" in the Qt API is so the user doesn't have to specify that flag at all, not so they can half specify it.

                  1 Reply Last reply
                  3
                  • S Offline
                    S Offline
                    StudentDev9923
                    wrote on last edited by StudentDev9923
                    #9

                    Thx a lot for all the replies ^^ I now see why you were confused why I wanted a default value for an option.

                    @wrosecrans Thanks for the in debt explanation. That really made the bricks fall into place about why a default value would be a bad idea for a command that has an option for an argument from the user :D

                    Gonna mark this as solved

                    1 Reply Last reply
                    0
                    • S StudentDev9923 has marked this topic as solved on

                    • Login

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