mirror of
https://github.com/hyprwm/hyprlock.git
synced 2024-11-16 23:05:58 +01:00
core: Inproved error handling and arg parsing (#380)
* Inproved error handling and arg parsing * Removed duplicated lines * Remove curly braces from short ifs, used c-style casts, used contains instead of find to parse exception, applied clang formating * Fix Formatting
This commit is contained in:
parent
210b456c15
commit
d31e600f14
1 changed files with 60 additions and 22 deletions
82
src/main.cpp
82
src/main.cpp
|
@ -1,6 +1,7 @@
|
||||||
|
|
||||||
#include "config/ConfigManager.hpp"
|
#include "config/ConfigManager.hpp"
|
||||||
#include "core/hyprlock.hpp"
|
#include "core/hyprlock.hpp"
|
||||||
|
#include <cstddef>
|
||||||
|
|
||||||
void help() {
|
void help() {
|
||||||
std::cout << "Usage: hyprlock [options]\n\n"
|
std::cout << "Usage: hyprlock [options]\n\n"
|
||||||
|
@ -12,13 +13,26 @@ void help() {
|
||||||
" --immediate - Lock immediately, ignoring any configured grace period\n"
|
" --immediate - Lock immediately, ignoring any configured grace period\n"
|
||||||
" -h, --help - Show this help message\n";
|
" -h, --help - Show this help message\n";
|
||||||
}
|
}
|
||||||
int main(int argc, char** argv, char** envp) {
|
|
||||||
std::string configPath;
|
|
||||||
std::string wlDisplay;
|
|
||||||
bool immediate = false;
|
|
||||||
|
|
||||||
for (int i = 1; i < argc; ++i) {
|
std::optional<std::string> parseArg(const std::vector<std::string>& args, const std::string& flag, std::size_t& i) {
|
||||||
std::string arg = argv[i];
|
if (i + 1 < args.size()) {
|
||||||
|
return args[++i];
|
||||||
|
} else {
|
||||||
|
std::cerr << "Error: Missing value for " << flag << " option.\n";
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char** argv, char** envp) {
|
||||||
|
std::string configPath;
|
||||||
|
std::string wlDisplay;
|
||||||
|
bool immediate = false;
|
||||||
|
bool showHelp = false;
|
||||||
|
|
||||||
|
std::vector<std::string> args(argv, argv + argc);
|
||||||
|
|
||||||
|
for (std::size_t i = 1; i < args.size(); ++i) {
|
||||||
|
const std::string arg = argv[i];
|
||||||
|
|
||||||
if (arg == "--verbose" || arg == "-v")
|
if (arg == "--verbose" || arg == "-v")
|
||||||
Debug::verbose = true;
|
Debug::verbose = true;
|
||||||
|
@ -26,33 +40,57 @@ int main(int argc, char** argv, char** envp) {
|
||||||
else if (arg == "--quiet" || arg == "-q")
|
else if (arg == "--quiet" || arg == "-q")
|
||||||
Debug::quiet = true;
|
Debug::quiet = true;
|
||||||
|
|
||||||
else if ((arg == "--config" || arg == "-c") && i + 1 < argc)
|
else if ((arg == "--config" || arg == "-c") && i + 1 < (std::size_t)argc) {
|
||||||
configPath = argv[++i];
|
if (auto value = parseArg(args, arg, i); value)
|
||||||
|
configPath = *value;
|
||||||
|
else
|
||||||
|
return 1;
|
||||||
|
|
||||||
else if (arg == "--display" && i + 1 < argc) {
|
} else if (arg == "--display" && i + 1 < (std::size_t)argc) {
|
||||||
wlDisplay = argv[i + 1];
|
if (auto value = parseArg(args, arg, i); value)
|
||||||
i++;
|
wlDisplay = *value;
|
||||||
} else if (arg == "--immediate") {
|
else
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
} else if (arg == "--immediate")
|
||||||
immediate = true;
|
immediate = true;
|
||||||
} else if (arg == "--help" || arg == "-h") {
|
|
||||||
|
else if (arg == "--help" || arg == "-h") {
|
||||||
|
showHelp = true;
|
||||||
|
break;
|
||||||
|
|
||||||
|
} else {
|
||||||
|
std::cerr << "Unknown option: " << arg << "\n";
|
||||||
help();
|
help();
|
||||||
return 0;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (showHelp) {
|
||||||
|
help();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
g_pConfigManager = std::make_unique<CConfigManager>(configPath);
|
auto configManager = std::make_unique<CConfigManager>(configPath);
|
||||||
g_pConfigManager->init();
|
configManager->init();
|
||||||
} catch (const char* err) {
|
g_pConfigManager = std::move(configManager);
|
||||||
Debug::log(CRIT, "ConfigManager threw: {}", err);
|
} catch (const std::exception& ex) {
|
||||||
std::string strerr = err;
|
Debug::log(CRIT, "ConfigManager threw: {}", ex.what());
|
||||||
if (strerr.contains("File does not exist"))
|
if (std::string(ex.what()).contains("File does not exist"))
|
||||||
Debug::log(NONE, " Make sure you have a config.");
|
Debug::log(NONE, " Make sure you have a config.");
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
g_pHyprlock = std::make_unique<CHyprlock>(wlDisplay, immediate);
|
try {
|
||||||
g_pHyprlock->run();
|
auto hyprlock = std::make_unique<CHyprlock>(wlDisplay, immediate);
|
||||||
|
g_pHyprlock = std::move(hyprlock);
|
||||||
|
g_pHyprlock->run();
|
||||||
|
} catch (const std::exception& ex) {
|
||||||
|
Debug::log(CRIT, "Hyprlock threw: {}", ex.what());
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue