How does QCommandLineOption::setDefaultValue work?
-
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.
-
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.
@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." -
@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."@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-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?
@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.
-
@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.
@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-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.@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.
-
@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.
@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 extensionThis would work/apply if the syntax requires
-t100
, I agree not-t 100
. -
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.
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 likemy_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.
-
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
-