Qt : QCommandLineParser 注意事项

QCommandLineParser
注意事项:
1.不支持可选项,例如 -i 不加内容
The parser does not support optional values - if an option is set to require a value, one must be present. If such an option is placed last and has no value, the option will be treated as if it had not been specified.
2. parse 解析出错返回 false,不会导致程序退出,可使用errorText() 查看错误。
3. process 解析出错,直接导致程序退出。

bool parse(const QStringList &arguments)
void process(const QStringList &arguments)
void process(const QCoreApplication &app)

4.QCommandLineOption
支持三种格式:
// A boolean option with a single name (-p)
// A boolean option with multiple names (-f, --force)
// An option with a value

想要使用parser.value(ipOptionName);的方式获取参数值,需要使用
// An option with a value 的方式添加。
好了。实例代码如下:

QString g_strServerIP;

void cmdLineParse()
{
   QCommandLineParser parser;
   parser.setApplicationDescription("hmi command helper");
   parser.addHelpOption();
   parser.addVersionOption();

   static const QString ipOptionName = "i";
   static const QString dbOptionName = "d";
   parser.addOptions({
      // An option with a value
      {{ipOptionName, "ip-address"},
          QCoreApplication::translate("main", "Set the web server IP. Have to be specified <IP>."),
          QCoreApplication::translate("main", "90.0.0.1")},

   });

   if (!parser.parse(qApp->arguments()))
       parser.showHelp();

   auto tmpIP = parser.value(ipOptionName);


   if (tmpIP.isEmpty())
       parser.showHelp();

   g_strServerIP = tmpIP;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值