aboutsummaryrefslogtreecommitdiff
path: root/lib/configuration/commandline.cpp
blob: 4581c84f88b37f128e5ff85050f4547b8d70fac3 (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
/*
 * 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/gitea/aqua/smolbote
 *
 * SPDX-License-Identifier: GPL-3.0
 */

#include "commandline.h"
#include "config.h"
#include <QStandardPaths>

namespace po = boost::program_options;

inline std::string defaultUserConfigLocation()
{
#ifdef CONFIG_PATH_CONFIG
    return CONFIG_PATH_CONFIG;
#else
    // try to locate an existing config
    QString path = QStandardPaths::locate(QStandardPaths::ConfigLocation, "smolbote/smolbote.cfg");

    // it's possible there is no config, so set the path properly
    if(path.isEmpty())
        path = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + "/smolbote/smolbote.cfg";

    return path.toStdString();
#endif
}

CommandLine::CommandLine(int argc, char **argv)
    : m_homePath(QStandardPaths::writableLocation(QStandardPaths::HomeLocation).toStdString())
{
    m_description.add_options()
        ("help,h", "Display command-line options list.")
        ("version,v", "Display version information.")
        ("build", "Display build commit.")

        ("config,c", po::value<std::string>()->default_value(defaultUserConfigLocation()), "Set the configuration file.")
        ("no-remote", "Do not accept or send remote commands.")

        ("session,s", po::value<std::string>(), "Open the selected session.")
        ("pick-session", "Show all available sessions and select which one to open.")

        ("args", po::value<std::vector<std::string>>(), "Command(s) and/or URL(s).")
        ;

    m_arguments.add("args", -1);

    try {
        auto cmd = po::command_line_parser(argc, argv);
        cmd.allow_unregistered();
        cmd.options(m_description);
        cmd.positional(m_arguments);
        po::store(cmd.run(), vm);
    } catch(const po::error &e) {
        qWarning("Error parsing cmd: %s", e.what());
    }
}