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;
}