aboutsummaryrefslogtreecommitdiff
path: root/src/commandline.cpp
blob: adfdf521f26747a03b811f9176c0962d48c56374 (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
 * 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
 */

#include "commandline.h"
#include "version.h"
#include <QStandardPaths>
#include <iomanip>
#include <iostream>

void printOption(const QCommandLineOption &option)
{
    std::cout << "- "
              << std::setw(20) << std::left << option.names().join(", ").toStdString()
              << std::setw(40) << std::left << option.description().toStdString()
              << std::setw(30) << std::left << option.defaultValues().join(", ").toStdString()
              << std::endl;
}

QString defaultUserConfigLocation()
{
    // try to locate an existing config
    QString path = QStandardPaths::locate(QStandardPaths::AppConfigLocation, "smolbote.cfg");

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

    return path;
}

CommandLine::CommandLine()
    : QCommandLineParser()
    , helpOption(addHelpOption())
    , versionOption(addVersionOption())
    , configOption({ "c", "config" }, "Set configuration file.", "path", defaultUserConfigLocation())
    , defaultConfigOption("default-config", "Set the default configuration file.", "path", "")
    , profileOption({ "p", "profile" }, "Use this profile.", "profile", "")
    , socketOption("socket", "Local server socket", "name", "")
    , newWindowOption("in-new-window", "Open URL in new window")
{
    setApplicationDescription("yet another no-frills browser");

    addOption(configOption);
    addOption(defaultConfigOption);
    addOption(profileOption);
    addOption(socketOption);
    addOption(newWindowOption);

    addPositionalArgument("URL", "URL(s) to open");
}

void CommandLine::parseCommandLine(const QCoreApplication &app)
{
    QCommandLineParser::parse(app.arguments());

    application = const_cast<QCoreApplication *>(&app);

    if(isSet(helpOption)) {
        printHelp(0);
    }

    if(isSet(versionOption)) {
        printVersion();
    }

    // create a list of unknown QCommandLineOption's
    // parser.addOptions() takes a list, so this is a QList

    for(const QString &opt : unknownOptionNames()) {
        QCommandLineOption o(opt, "dummy desc", "dummy value");
        opts.append(o);
    }

    // add list and reparse to set the new options
    addOptions(opts);
    parse(app.arguments());
}
void CommandLine::printHelp(int exitCode)
{
    std::cout << "Usage: " << application->arguments().at(0).toStdString() << " [options] URL" << std::endl;
    std::cout << application->applicationName().toStdString() << " " << SMOLBOTE_DESCRIBE << ": "
              << applicationDescription().toStdString() << std::endl
              << std::endl;

    std::cout << "Options: " << std::endl;
    printOption(helpOption);
    printOption(versionOption);
    printOption(configOption);
    printOption(defaultConfigOption);
    printOption(profileOption);
    printOption(socketOption);
    printOption(newWindowOption);
    std::cout << std::endl;

    std::cout << "You can also overwrite configuration options using the syntax: " << std::endl
              << "--browser.setting.path=value" << std::endl
              << std::endl;

    std::cout << "Arguments: " << std::endl;
    std::cout << "- "
              << std::setw(20) << std::left << "URL"
              << std::setw(40) << std::left << "URL(s) to open"
              << std::endl
              << std::endl;

    exit(0);
}
void CommandLine::printVersion()
{
    std::cout << application->applicationName().toStdString() << " " << SMOLBOTE_DESCRIBE << std::endl;
    exit(0);
}