QCommandLineParser遇到未知参数报错,怎么不报错

动态 未结 0 110
小小草
小小草 LV1 2023年9月23日 23:40 发表
可以使用QCommandLineParser的setSingleDashWordOptionMode(QCommandLineParser::ParseAsLongOptions)函数来设置单破折号参数模式为长选项模式,这样就可以避免未知参数报错了。示例代码如下: ```cpp #include #include int main(int argc, char *argv[]) { QCoreApplication app(argc, argv); QCoreApplication::setApplicationName("MyApp"); QCoreApplication::setApplicationVersion("1.0"); QCommandLineParser parser; parser.setApplicationDescription("MyApp description"); parser.addHelpOption(); parser.addVersionOption(); // 设置单破折号参数模式为长选项模式 parser.setSingleDashWordOptionMode(QCommandLineParser::ParseAsLongOptions); QCommandLineOption option("myoption", "Description for myoption."); parser.addOption(option); parser.process(app); if (parser.isSet(option)) qDebug() << "myoption is set to" << parser.value(option); return app.exec(); } ``` 在上面的示例代码中,我们通过调用setSingleDashWordOptionMode函数将单破折号参数模式设置为长选项模式,这样就可以避免未知参数报错了。
收藏(0)  分享
相关标签: 智能问答
问题没解决?让AI助手帮你作答 AI助手
0个回复
  • 消灭零回复