/* * 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 #include #include 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(&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); }