looper/main.cpp

86 lines
No EOL
2.9 KiB
C++

#include "options.hpp"
#include "backend.hpp"
#include "log.hpp"
#include "thirdparty/CLI11.hpp"
#include "data.h"
using namespace Looper;
using namespace Looper::Options;
using namespace Looper::Log;
int main(int argc, char **argv) {
std::vector<std::string> args;
for (int i = 1; i < argc; i++) {
args.push_back(std::string(argv[i]));
}
CLI::App app{DESCRIPTION};
std::string filename = "";
app.allow_extras();
int log_level;
std::string ui_backend_option = "";
bool full_help = false;
app.add_option("-l, --log-level", LogStream::log_level, "Sets the minimum log level to display in the logs.");
app.add_option("-u, --ui-backend", ui_backend_option, "Specifies which UI backend to use.");
try {
app.parse(args);
} catch (const CLI::ParseError &e) {
if (app.get_help_ptr()->get_callback_run()) {
full_help = true;
} else {
exit(app.exit(e));
}
}
args.clear();
args = app.remaining(false);
int new_argc = args.size();
char **new_argv = (char**)malloc(new_argc * sizeof(char*));
init_logging();
DEBUG.writeln("Command line arguments after first parse:");
for (size_t i = 0; i < new_argc; i++) {
auto &arg = args[i];
DEBUG.writefln(" - '%s'", arg.c_str());
new_argv[i] = strdup(arg.c_str());
}
DEBUG.writeln("Initializing frontends...");
init_backends();
DEBUG.writeln("Loaded frontends: ");
for (auto kv : UIBackend::backends) {
DEBUG.writefln(" - '%s'", kv.first.c_str());
}
DEBUG.writeln("Loading options file...");
load_options();
std::string backend_id = get_option<std::string>("ui.frontend", "imgui");
UIBackend *backend = UIBackend::get_backend(ui_backend_option).value_or(UIBackend::get_backend(backend_id).value_or(UIBackend::get_first_backend()));
int output = 0;
if (backend == nullptr) {
ERROR.writeln("No UI backend could be found.");
return -1;
} else {
DEBUG.writefln("Using backend: '%s'...", backend->get_id().c_str());
if (full_help) {
args.clear();
args.push_back("--help");
}
try {
output = backend->run(args, new_argc, new_argv);
} catch (int return_code) {
if (full_help) {
std::string helpstr = app.help();
helpstr = helpstr.substr(helpstr.find("\n") + 1);
helpstr = helpstr.substr(helpstr.find("\n") + 1);
helpstr = helpstr.substr(helpstr.find("\n") + 1);
helpstr = helpstr.substr(helpstr.find("\n") + 1);
helpstr = helpstr.substr(helpstr.find("\n") + 1);
printf(helpstr.c_str());
}
output = return_code;
}
}
DEBUG.writeln("Exiting...");
UIBackend::deinit_backends();
for (int i = 0; i < new_argc; i++) {
free(new_argv[i]);
}
free(new_argv);
save_options();
return output;
}