config.cc 1.87 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#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) {
18
        std::cout << "m_hostname = " << m_hostname << std::endl;
19
20
21
22
23
24
25
26
27
        m_parser.exit(e);
        throw FIPhoboServerException(
            "Returning after command line parsing. --help or invalid argument(s)");
    }
    return handle_arguments();
}

void Config::define_options()
{
28
29
30
31
32
33
34
    m_parser.require_subcommand(1, 1);
    m_start = m_parser.add_subcommand("start", "start the server daemon");
    m_stop  = m_parser.add_subcommand("stop", "stop the server daemon");

    m_start
        ->add_option(
            "hostname,--hostname", m_hostname, "IP/Hostname to bind to")
35
        ->required();
36
    m_start->add_option(
37
        "--http", m_http_port, "port to listen on with HTTP protocol");
38
    m_start->add_option(
39
        "--http2", m_http2_port, "port to listen on with HTTP2 protocol");
40
    m_start->add_option(
41
42
        "-t,--threads", m_thread_count, "number of threads to listen on");

Ciarán Ó Rourke's avatar
Ciarán Ó Rourke committed
43
44
45
    m_start->add_option(
        "-l,--logfile", m_log_filename, "name of file to write logs to");

46
    m_parser.set_config("--config");
47
    m_start->add_option(
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
        "-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();
    }

Ciarán Ó Rourke's avatar
Ciarán Ó Rourke committed
64
65
    return {m_http_port, m_http2_port, m_hostname, m_thread_count,
            m_log_filename};
66
67
68
69
}

}  // namespace util
}  // namespace fiphoboserver