config.cc 1.46 KB
Newer Older
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
#include "config.h"

#include "../fiphoboserver_exception.h"

#include <fstream>
#include <unistd.h>

namespace fiphoboserver {
namespace util {

cli_options Config::parse(int argc, char* argv[])
{
    define_options();
    try {
        m_parser.parse(argc, argv);
    }
    catch (const CLI::ParseError& e) {
        m_parser.exit(e);
        throw FIPhoboServerException(
            "Returning after command line parsing. --help or invalid argument(s)");
    }
    return handle_arguments();
}

void Config::define_options()
{
    m_parser.add_option("hostname,--hostname", m_ip, "IP/Hostname to bind to")
        ->required();
    m_parser.add_option(
        "--http", m_http_port, "port to listen on with HTTP protocol");
    m_parser.add_option(
        "--http2", m_http2_port, "port to listen on with HTTP2 protocol");
    m_parser.add_option(
        "-t,--threads", m_thread_count, "number of threads to listen on");

    m_parser.set_config("--config");
    m_parser.add_option(
        "-w,--write-config", m_config_filename,
        "write configuration options to file");
}

cli_options Config::handle_arguments()
{
    if (m_thread_count <= 0) {
        m_thread_count = sysconf(_SC_NPROCESSORS_ONLN);
    }

    if (!m_config_filename.empty()) {
        std::ofstream file(m_config_filename);
        file << m_parser.config_to_str();
        file.close();
    }

    return {m_http_port, m_http2_port, m_ip, m_thread_count};
}

}  // namespace util
}  // namespace fiphoboserver