Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to handle unknown arguments with QCommandLineParser



  • Hello,

    I recently needed to parse CLI arguments with QCommandLineParser (Qt 5.15) at two different functions.

    The first function is not aware of the arguments of the second function and it is also the other way around. The second function is also a part of a pre-processed conditional group and is only compiled at Windows.

    What is the preferred way to handle unknown arguments when calling parse() which haven't been added with addOption()?

    The current solution I chose is to ignore the false returned from parse(), but I am not sure if this is the preferred approach.

    How do you handle this situation?

    Is there a way to tell the parser to look only at the arguments added with addOption()? Or maybe did you happen to remove any unknown arguments beforehand. Can you point me to examples at maybe Github implementing such parsing across multiple functions?

    Is there anything new related to command line argument parsing coming in Qt 6.x?

    Thanks in advance.


  • Lifetime Qt Champion

    You can get the unparsed options with QCommandLinParser::unknownOptionNames(). What you do with them is up to you.



  • Thanks, @Christian-Ehrlicher. I wasn't aware of this function, I am going to try to remove all unknown arguments before running parse() and avoid having to ignore a return value on dubious purpose.


Log in to reply