views:

3322

answers:

10

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...

+1  A: 

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.

Zifre
+2  A: 

That package does support --disable-foo and --enable-foo via opts.addSwitch("disable-foo", &foo_disabled); and opts.addSwitch("enable-foo", &foo_enabled);. You need handle checking both, and dealing with someone specifying both (oops).

What I don't understand is how this has anything to do with QT4...

jesup
This means that for every bool I need to add two rules. Not the best practice. Besides, I want to have a "native" Qt4 solution. For example, when adding a switch with multiple values, I want to get a QList, or when saying that I want a point, I want to get a QPoint. opts.addPoint("location", myQPointVariable)
elcuco
Yes, that's true, you need two "rules" - but you also have two options; it's --disable-foo not --foo=disabled. And if you have both, you need to detect and at least error on enable and disable together (though that could be done in the getopt parser). ephemient's answer has more QT-specific info, but the library he suggests has the same general restrictions as the froglogic one. The QT arguments() stuff doesn't handle any of the sort of parsing you want.
jesup
+5  A: 

QCoreApplication's constructors require (int &argc, char **argv) (and QApplication inherits from QCoreApplication). As the documentation states, it is highly recommended that

Since QApplication also deals with common command line arguments, it is usually a good idea to create it before any interpretation or modification of argv is done in the application itself.

And 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 through argv; 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 without KApplication if you really want (call KCmdLineArgs::init).

KCmdLineOptions options;
options.add("enable-foo", ki18n("enables foo"));
options.add("nodisable-foo", ki18n("disables foo"));
// double negatives are confusing, but this makes disable-foo enabled by default

KCmdLineArgs::addCmdLineOptions(options);
KApplication app;
KCmdLineArgs *args = KCmdLineArgs::parsedArgs();

if (args->isSet("enable-foo") && !args->isSet("disable-foo"))
    cout << "foo enabled" << endl;
else
    cout << "foo disabled" << endl;

Untested (who ever tests what they post on S.O.?).

ephemient
ripping off kdelibs... what a nice idea, how didn't I think about it? I will have to test this!
elcuco
+1  A: 

A really simple method is to scan "key=value" args,
put them in a table say zz.map: QString -> QVariant,
and get their values with zz.map.value( key, default ). An example:

#include "ztest.h"
Ztest zz;  
int main( int argc, char* argv[] )
{
    zz.eqargs( ++ argv );  // scan  test=2 x=str ... to zz.map

    QString xx = zz.map.value( "xx", "" );
    if( Zint( Size, 10 ))  // a #def -> zz.map.value( "Size", 10 )
        ...

ztest.h is < 1 page, below; same for Python ~ 10 lines.

(Everybody has his/her favorite options parser; this one's about the simplest.
Worth repeating: however you specify options, echo them to output files --
"every scientist I know has trouble keeping track of what parameters they used last time they ran a script".)

To make QPoints etc work one of course needs a QString -> QPoint parser. Anyone know offhand why this doesn't work (in Qt 4.4.3) ?

QPoint pt(0,0);
QDataStream s( "QPoint(1,2)" );
s >> pt;
qDebug() << "pt:" << pt;  // QPoint(1364225897,1853106225) ??

Added 25nov --

// ztest.h: scan args x=2 s=str ... to a key -> string table
// usage:
// Ztest ztest;
// int main( int argc, char* argv[] )
// {
//     QApplication app( argc, argv );
//     ztest.eqargs( ++ argv );  // scan leading args name=value ...
//     int x = Zint( x, 10 );  // arg x= or default 10
//     qreal ff = Zreal( ff, 3.14 );
//     QString s = Zstr( s, "default" );
// care: int misspelled = Zint( misspellled ) -- you lose
//version: 2009-06-09 jun denis

#ifndef ztest_h
#define ztest_h

#include <QHash>
#include <QString>
#include <QVariant>
#include <QRegExp>

//------------------------------------------------------------------------------
class Ztest {
public:
  QHash< QString, QVariant > map;
  int test;  // arg test=num,  if( ztest.test )

  Ztest() : test( 0 ) {}

  QVariant val( const QString& key, const QVariant& default_ = 0 )
  {
    return map.value( key, default_ );
  }

  void setval( const QString& key, const QVariant& val )
  {
    map[key] = val;
    if( key == "test"  ||  key == "Test" )
        test = val.toInt();
  }

//------------------------------------------------------------------------------
    // ztest.eqargs( ++ argv )  scans test=2 x=3 ... -> ztest table
  void eqargs( char** argv )
  {
    char** argv0 = argv;
    char *arg;
    QRegExp re( "(\\w+)=(.*)" );  // name= anything, but not ./file=name
    for( ; (arg = *argv) && re.exactMatch( arg );  argv ++ ){
        setval( re.cap(1), re.cap(2) );
    }
        // change argv[0..] -> args after all name=values
    while(( *argv0++ = *argv++) != 0 ) {}
  }
};

extern Ztest ztest;

    // macros: int x = Zint( x, 10 ): x= arg or default 10
#define Zstr( key, default )    ztest.val( #key, default ).toString()
#define Zint( key, default )    ztest.val( #key, default ).toInt()
#define Zreal( key, default )   ztest.val( #key, default ).toDouble()

#endif
Denis
Denis, where can I obtain that "ztest" class? See Muby's answer
elcuco
A: 

Indeed i'm interessed by your ztest class.

could you post it ? :)

thanks

A: 

Also for some fancy options parsing you can try gperf.

IBM has a nice tutorial on it.

HabarNam
GPL, and not LGPL/MIT/X11.... and no integration with Qt4 "primitives" see my comment to jesup
elcuco
I'm not very sure that the licence applies to code generated by gperf and to the best of my knowledge you are not linking against gperf. It is just an external tool to build a look-up function/object.In all fairness, I have yet to use it for an actual project.
HabarNam
A: 

Look at this: http://code.google.com/p/qtargparser/

Igor
SO NICE!!! seems like BSD'ish license, pure Qt... however it's kinda verbose. Look for example, the amount of code needed to process the command line agruments: http://code.google.com/p/qtargparser/source/browse/trunk/samples/help/main.cpp I do need to find time and play with it. Thanks!
elcuco
A: 

Another option I ran across while looking to do this, too:

http://code.google.com/p/qgetopts/

I haven't used it though.

dwj
+1  A: 

There is also QxtCommandOptions from http://www.libqxt.org/

KillerWabbit
A: 

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:

./QArgTest --pid=45

And the code:

int main(int argc, char *argv[])
{
    QApplication app(argc, argv, false);
    qDebug() << "QApp arg test app"; 

    QStringList args = app.arguments();

    int index;
    int pid;
    QRegExp rxArgPid("--pid=([0-9]{1,})");

    for (int i = 1; i < args.size(); ++i)
    {
        index = rxArgPid.indexIn(args.at(i));
        if (index != -1 ) {   
            pid =  rxArgPid.cap(1).toInt();
            qDebug() << i << ":" << args.at(i) << rxArgPid.cap(1) << pid;
        } else {
            qDebug() << "Uknown arg:" << args.at(i);
        }

    }
}
Johan