aboutsummaryrefslogtreecommitdiff
path: root/src/commandline.cpp
blob: 6f227c08395e70fb4ba9cafc3b23f8e7797f0961 (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
118
119
120
121
122
123
124
125
126
127
128
/*
 * 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 <configuration.h>
#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;
}

constexpr const char *socketPath()
{
#ifdef Q_OS_UNIX
    // could be a path such as "/tmp/foo"
    return "/tmp/smolbote.socket";
#elif Q_OS_WIN32
    // could be a pipe path such as "\\.\pipe\foo"
    return = "\\\\.\\pipe\\smolbote_socket";
#endif
}

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

    addOption(buildOption);
    addOption(configOption);
    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();
    }

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

    if(isSet(buildOption)) {
        printBuild();
    }

    // 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");
        unknownOptions.append(o);
    }

    // add list and reparse to set the new options
    addOptions(unknownOptions);
    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(buildOption);
    printOption(configOption);
    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(exitCode);
}

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

void CommandLine::printBuild(int exitCode)
{
    std::cout << SMOLBOTE_BRANCH << ":" << SMOLBOTE_COMMIT << std::endl;
    exit(exitCode);
}