30 #ifndef CONFIGLOADER_HPP 31 #define CONFIGLOADER_HPP 33 #include <QCommandLineParser> 34 #include <QJsonObject> 35 #include <QJsonDocument> 48 ConfigLoader(
const QCommandLineParser & parser): m_parser(parser) {
50 const QStringList positionalArguments = m_parser.positionalArguments();
52 foreach (
const QString &str, positionalArguments) {
55 file.setFileName(str);
56 if(file.open(QIODevice::ReadOnly | QIODevice::Text)){
57 QString configStr = file.readAll();
58 QJsonDocument doc = QJsonDocument::fromJson(configStr.toUtf8());
59 m_config_files.push_back(doc.object());
67 QString
getParam(
const QCommandLineOption & option)
const {
69 if(m_parser.isSet(option))
70 return m_parser.value(option);
72 QStringList names = option.names();
74 foreach (
const QString &name, names) {
76 if(name.length() == 1)
continue;
78 foreach (
const QJsonObject &obj, m_config_files) {
80 if(obj.contains(name) && obj.value(name).isString())
81 return obj.value(name).toString();
92 bool isSet(
const QCommandLineOption & option)
const {
94 if(m_parser.isSet(option))
97 QStringList names = option.names();
99 foreach (
const QString &name, names) {
101 if(name.length() == 1)
continue;
103 foreach (
const QJsonObject &obj, m_config_files) {
104 if(obj.contains(name) && obj.value(name).isString())
116 const QCommandLineParser & m_parser;
117 QList<QJsonObject> m_config_files;
A QT based command line options loader/parser.
Definition: configloader.hpp:43
ConfigLoader(const QCommandLineParser &parser)
Constructs a ConfigLoader with given QCommandLineParser.
Definition: configloader.hpp:48
bool isSet(const QCommandLineOption &option) const
Returns true when parameter is set, either on command line, on in a json config file.
Definition: configloader.hpp:92
QString getParam(const QCommandLineOption &option) const
Returns a string parameter value, command-line has priority over json config file.
Definition: configloader.hpp:67