I am looking for a command line parser for Qt4.
I did a small google search, and found this: http://www.froglogic.com/pg?id=PublicationsFreeware&category=getopt however it lacks support for "--enable-foo" and "--disable-foo" switches. Besides that, it looks like a real winner.
EDIT:
It seems Frologic removed this. So the best options I see are using Boost (which is not API nor ABI stable) or forking the support for kdelibs. Yay...
Since Qt 5.2 you can finally find a solution in QtCore itself: I contributed QCommandLineParser there.
QCoreApplication
's constructors require(int &argc, char **argv)
(andQApplication
inherits fromQCoreApplication
). As the documentation states, it is highly recommended thatAnd if you're letting Qt get the first pass at handling arguments anyways, it would also be a good idea to use
QStringList QCoreApplication::arguments()
instead of walking throughargv
;QApplication
may remove some of the arguments that it has taken for its own use.This doesn't lend itself to being very compatible with other argument-parsing libraries...
However, kdelibs does come with a nice argument parser,
KCmdLineArgs
. It is LGPL and can be used withoutKApplication
if you really want (callKCmdLineArgs::init
).Untested (who ever tests what they post on S.O.?).
This is more or less the same answer as ephemient, but with a simple regexp to help parse the args. (This way could be useful if you only need a handful of args)
Run with this:
And the code:
Look at this: http://code.google.com/p/qtargparser/
Does it have to be Qt4 specific? If not, GNU Getopt is really nice, although licensing may be a problem if you are not doing open source software.