aboutsummaryrefslogtreecommitdiff
path: root/lib/configuration/configuration.h
blob: 5bb9f5d2ebe6c802a31cce69d8fa800fe5bbf88a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
 * This file is part of smolbote. It's copyrighted by the contributors recorded
 * in the version control history of the file, available from its original
 * location: https://neueland.iserlohn-fortress.net/smolbote.hg
 *
 * SPDX-License-Identifier: GPL-3.0
 */

#ifndef SMOLBOTE_CONFIGURATION_H
#define SMOLBOTE_CONFIGURATION_H

#include <QString>
#include <QStringList>
#include <QVariant>
#include <boost/program_options.hpp>
#include <optional>
#include <string>
#include <vector>

class Configuration
{
public:
    explicit Configuration();
    ~Configuration();

    bool parseConfigFile(const std::string &path);
    bool parseCommandLine(int argc, char **argv);

    template <typename T>
    std::optional<T> value(const char *path) const
    {
        // if setting doesn't exist, we crash
        // in debug builds, check if setting exists
#ifdef QT_DEBUG
        if(vm.count(path) == 0) {
            qWarning("value(%s) does not exist, probably crashing now", path);
        }
#endif

        if constexpr(std::is_same_v<T, std::string>) {
            std::string r;
            try {
                r = vm[path].as<std::string>();
            } catch(boost::bad_any_cast &) {
                // try int
                try {
                    r = std::to_string(vm[path].as<int>());
                } catch(boost::bad_any_cast &) {

                    // try bool, and crash if not that either
                    r = vm[path].as<bool>() ? "true" : "false";
                }
            }

            // check if it's a path
            if(r.front() == '~') {
                r.replace(0, 1, m_homePath);
            }

            return std::optional<std::string>(r);
        } else
            return std::optional<T>(vm[path].as<T>());
    }

    const std::vector<boost::shared_ptr<boost::program_options::option_description>> &options()
    {
        return configuration_desc.options();
    }

    QHash<QString, QString> section(const std::string &prefix) const;
    QStringList positionalArguments() const
    {
        QStringList l;
        if(vm.count("url")) {
            std::vector<std::string> urls = vm["url"].as<std::vector<std::string>>();
            for(const std::string &s : urls) {
                l.append(QString::fromStdString(s));
            }
        }
        return l;
    }

private:
    boost::program_options::options_description commandLine_desc;
    boost::program_options::options_description configuration_desc;
    boost::program_options::positional_options_description arguments_desc;
    boost::program_options::variables_map vm;

    std::string m_homePath;
};

#endif // SMOLBOTE_CONFIGURATION_H