Command line parser for Qt4

前端 未结 11 1737
离开以前
离开以前 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: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.?).

提交回复
热议问题