版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010168781/article/details/84667601
参考下大神的命令行参数解析是如何是实现的
//使用const char []代替宏定义字符串,我以前都是用const QString,想想好傻
const char SETTINGS_OPTION[] = "-settingspath";
const char INSTALL_SETTINGS_OPTION[] = "-installsettingspath";
const char PLUGINPATH_OPTION[] = "-pluginpath";
const char TEST_OPTION[] = "-test";
//保存参数的结构体
struct Options
{
QString settingsPath;
QString installSettingsPath;
QStringList customPluginPaths;
std::vector<char *> appArguments;
bool hasTestOption = false;
};
Options parseCommandLine(int argc, char *argv[])
{
Options options;
auto it = argv;
const auto end = argv + argc;
while (it != end) {
const auto arg = QString::fromLocal8Bit(*it);
const bool hasNext = it + 1 != end;
const auto nextArg = hasNext ? QString::fromLocal8Bit(*(it + 1)) : QString();
if (arg == SETTINGS_OPTION && hasNext) {
++it;
options.settingsPath = QDir::fromNativeSeparators(nextArg);
} else if (arg == INSTALL_SETTINGS_OPTION && hasNext) {
++it;
options.installSettingsPath = QDir::fromNativeSeparators(nextArg);
} else if (arg == PLUGINPATH_OPTION && hasNext) {
++it;
options.customPluginPaths += QDir::fromNativeSeparators(nextArg);
} else { // arguments that are still passed on to the application
if (arg == TEST_OPTION)
options.hasTestOption = true;
options.appArguments.push_back(*it);
}
++it;
}
return options;
}