0

I have this code:

parser.addOption(requiredValueOption);
parser.addOption(booleanOption);
if (!parser.parse(app->arguments())) {
    qDebug() << parser.errorText();
    parser.showHelp(1);
}

When I call it like this:

./app --required-value-option value

It works.

When I call it like this:

./app --required-value-option

I see an error message "missing value after...", which is expected

But when I call it like this:

./app --required-value-option --boolean-option

The string --boolean-option is interpreted as a value for --required-value-option!!! This is no what I expect. This should trigger an error.

How to correctly parse the options with required values using QCommandLineParser?

UPD: If you use some required values for options in your Qt applications - You can test it.

UPD2: Current behaviour for example: --no-gui boolean option and --output-file required file option. Run it as monkey:

app --output-file --no-gui

Wow! The application starts with gui and show "--no-gui file does not exist". Good job man!

UPD3: Ok. I want to interpret the registered options as options and not as values for previous options. Example: --no-gui can be valid filename, but since this is registered option, the parser shouldn't use it as value.

Basile Perrenoud
  • 4,039
  • 3
  • 29
  • 52
Deep
  • 2,472
  • 2
  • 15
  • 25
  • How is Qt supposed to know that `--boolean-option` isn't a valid value argument for the `--required-value-option` option? You need to check that the option contains a valid value, and if not, print a meaningful error message and quit. – peppe Apr 08 '17 at 21:20
  • @peppe --boolean-option this is OPTION because started with single or double dash. Why I must check for all boolean options? e.g. --boolean-option1 --boolean-option2 -b -c -def --boolean-optionN. This is joke? This is work for parser. – Deep Apr 08 '17 at 21:30
  • @peppe and!!! This parser eat boolean option and say is not set! I can't check it right after parse. – Deep Apr 08 '17 at 21:38
  • Again: if all double dashes always started options, how could you then pass the string `--foo` as an *argument* of an option? – peppe Apr 08 '17 at 21:43
  • The point isn't the boolean option, the point is that you can check that the value set for `--required-value-option` isn't legal, and emit an error in that case. (Which is exactly what you're supposed to do anyhow. Say you want an integer: you need to check that the argument *is* an integer). – peppe Apr 08 '17 at 21:45
  • @peppe agait too: For example. --no-gui boolean option and --output-file required file option. Run it as monkey: app --output-file --no-gui Wow! application start gui and show "--no-gui no file exists". Good job man! – Deep Apr 08 '17 at 21:47

1 Answers1

1

Ok, I answer for self question.

For first you must add and parse boolean options only. Store founded flags (I store its as bit mask and check it later).

Then add other options with values and parse one more.

Now You have parsed boolean options and parsed options with values.

Complete!

Deep
  • 2,472
  • 2
  • 15
  • 25