Command line parser for Qt4

前端 未结 11 1740
离开以前
离开以前 2021-01-30 23:23

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 how

相关标签:
11条回答
  • 2021-01-30 23:44

    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 --enable-foo
    

    And the code:

    int main(int argc, char *argv[]) {
        QApplication app(argc, argv, false);
        qDebug() << "QApp arg test app"; 
    
        QStringList args = app.arguments();
    
        int pid = 0;
    
        QRegExp rxArgPid("--pid=([0-9]{1,})");
        QRegExp rxArgFooEna("--enable-foo");
        QRegExp rxArgFooDis("--disable-foo");
    
        for (int i = 1; i < args.size(); ++i) {
            if (rxArgPid.indexIn(args.at(i)) != -1 ) {   
                pid =  rxArgPid.cap(1).toInt();
                qDebug() << i << ":" << args.at(i) << rxArgPid.cap(1) << pid;
            }
            else if (rxArgFooEna.indexIn(args.at(i)) != -1 ) {   
                qDebug() << i << ":" << args.at(i) << "Enable Foo";
            } 
            else if (rxArgFooDis.indexIn(args.at(i)) != -1 ) {   
                qDebug() << i << ":" << args.at(i) << "Disable Foo";
            } 
            else {
                qDebug() << "Uknown arg:" << args.at(i);
            }
        }
        return 0;
    }
    
    0 讨论(0)
  • 2021-01-30 23:44

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

    0 讨论(0)
  • 2021-01-30 23:50

    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
    
    0 讨论(0)
  • 2021-01-30 23:51

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

    0 讨论(0)
  • 2021-01-30 23:51

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

    0 讨论(0)
提交回复
热议问题