我正在寻找适用于Qt4的命令行解析器。
我进行了一次小型的谷歌搜索,并发现这个网址:http://www.froglogic.com/pg?id=PublicationsFreeware&category=getopt,然而它不支持"--enable-foo"和"--disable-foo"开关。除此之外,它看起来像是一个真正的胜利者。
编辑:
看来Frologic已经删除了该项目。所以我最好的选择是使用Boost(它不是API或ABI稳定的),或者为kdelibs提供支持。耶...
我正在寻找适用于Qt4的命令行解析器。
我进行了一次小型的谷歌搜索,并发现这个网址:http://www.froglogic.com/pg?id=PublicationsFreeware&category=getopt,然而它不支持"--enable-foo"和"--disable-foo"开关。除此之外,它看起来像是一个真正的胜利者。
编辑:
看来Frologic已经删除了该项目。所以我最好的选择是使用Boost(它不是API或ABI稳定的),或者为kdelibs提供支持。耶...
QCoreApplication
的构造函数需要(int &argc, char **argv)
(QApplication
从QCoreApplication
继承而来)。正如文档所述,强烈建议在应用程序本身对argv
进行解释或修改之前,先创建 QApplication ,因为它还处理常见的命令行参数。
由于 QApplication 还处理常见的命令行参数,所以通常最好在应用程序本身对
argv
进行解释或修改之前创建它。
如果你让Qt来处理参数的第一步,那么最好使用QStringList QCoreApplication::arguments()
而不是遍历argv
。因为QApplication
可能会移除一些它自己使用的参数。
这不太适用于其他参数解析库...
不过,kdelibs附带了一个很好的参数解析器KCmdLineArgs
。它是LGPL许可证,如果你真的想要,可以不使用KApplication
(调用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;
未经测试(谁会测试他们在S.O.上发布的内容?)。
这个回答和ephemient的回答差不多,但是使用了一个简单的正则表达式来帮助解析参数。(如果你只需要少数几个参数,这种方法可能很有用)
使用以下命令运行:
./QArgTest --pid=45 --enable-foo
代码如下:
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;
}
还有来自http://www.libqxt.org/的QxtCommandOptions。
这个软件包确实支持通过 opts.addSwitch("disable-foo", &foo_disabled); 和 opts.addSwitch("enable-foo", &foo_enabled); 来禁用和启用 foo。你需要处理两种情况的检查,并处理有人同时指定两种情况(糟糕)。
我不明白这与 QT4 有什么关系...
#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
文件小于1页,Python相关内容不超过10行。
每个人都有自己喜欢的选项解析器;而这个是最简单的之一。
值得重提的是:无论您如何指定选项,请将它们回显到输出文件中--
"我认识的每个科学家都很难跟踪上次运行脚本时使用的参数"。
为了使QPoints等工作,当然需要一个QString -> QPoint解析器。 有没有人知道为什么这在Qt 4.4.3中不起作用?
QPoint pt(0,0);
QDataStream s( "QPoint(1,2)" );
s >> pt;
qDebug() << "pt:" << pt; // QPoint(1364225897,1853106225) ??
新增 11月25日 --
// 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
时光荏苒,现在已经是2013年了,但依然没有“第一方”命令行参数解析器。无论如何...如果有人和我一样遇到了同样的问题,并想要避免学习cmd解析器库所需要面对的学习曲线,那么这里有一个“快速&脏”的解决方案:-
QString QArgByKey(QString key, QChar sep = QChar('\0') ) //prototype usually in separate header
QString QArgByKey(QString key, QChar sep )
{
bool sepd=sep!=QChar('\0');
int pos=sepd?qApp->arguments().indexOf(QRegExp('^'+key+sep+"\\S*")):qApp->arguments().indexOf(QRegExp(key));
return pos==-1?QString::null:
(sepd?qApp->arguments().at(pos).split(sep).at(1):(++pos<qApp->arguments().size()?qApp->arguments().at(pos):QString::null));
}
例子:
user@box:~$ ./myApp firstKey=Value1 --secondKey Value2 thirdKey=val3.1,val3.2,val3.3 --enable-foo
用法:
QString param1 = QArgByKey("firstkey",'='); // Returns `Value1` from first pair
QString param2 = QArgByKey("--secondkey"); // Returns `Value2` from second pair
QString param3-1 = QArgByKey("thirdkey",'=').split(',').at(0); // Returns `val3.1`
bool fooEnabled = qApp->arguments().contains("--enable-foo"); //To check for `--enable-foo`
参数可以以任何顺序传递
编辑:此代码片段的更新将在此处找到
它必须是Qt4特定的吗?如果不是,GNU Getopt非常好用,尽管许可证可能会成为问题,如果您不是在开源软件中使用。