From e6dc808cee451e17add091bf54594dcaaf07db1c Mon Sep 17 00:00:00 2001 From: Steffen Winter Date: Sat, 19 Apr 2025 13:02:18 +0200 Subject: [PATCH] Add CLI flag to specify a different config file Signed-off-by: Steffen Winter --- src/btop.cpp | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/btop.cpp b/src/btop.cpp index bc2a3d3..9dd5cd6 100644 --- a/src/btop.cpp +++ b/src/btop.cpp @@ -19,6 +19,8 @@ tab-size = 4 #include #include #include +#include +#include #include #ifdef __FreeBSD__ #include @@ -116,6 +118,8 @@ namespace Global { bool arg_low_color{}; int arg_preset = -1; int arg_update = 0; + + static std::optional config_file {}; } static void print_version() { @@ -146,6 +150,7 @@ static void print_help() { " {0}+t, --tty_off {2}force (OFF) tty mode\n" " {0}-p, --preset {2}start with preset, integer value between 0-9\n" " {0}-u, --update {2}set the program update rate in milliseconds\n" + " {0}-c, --config {2}path to a config file\n" " {0} --utf-force {2}force start even if no UTF-8 locale was detected\n" " {0} --debug {2}start in DEBUG mode: shows microsecond timer for information collect\n" " {0} {2}and screen draw functions and sets loglevel to DEBUG", @@ -222,6 +227,22 @@ void argumentParser(const int argc, char **argv) { Global::utf_force = true; else if (argument == "--debug") Global::debug = true; + else if (argument == "-c" || argument == "--config") { + if (++i > argc) { + fmt::println(stderr, "{0}error:{1} Config option requires a file name\n", "\033[1;31m", "\033[0m"); + print_usage(); + print_help_hint(); + exit(1); + } + auto file = std::filesystem::path(argv[i]); + if (std::filesystem::is_directory(file)) { + fmt::println(stderr, "{0}error:{1} Config file can't be a directory\n", "\033[1;31m", "\033[0m"); + print_usage(); + print_help_hint(); + exit(1); + } + Global::config_file = std::make_optional(file); + } else { fmt::println("{0}error:{2} unexpected argument '{1}{3}{2}' found\n", "\033[1;31m", "\033[33m", "\033[0m", argument); print_usage(); @@ -925,7 +946,11 @@ int main(int argc, char **argv) { const auto config_dir = Config::get_config_dir(); if (config_dir.has_value()) { Config::conf_dir = config_dir.value(); - Config::conf_file = Config::conf_dir / "btop.conf"; + if (Global::config_file.has_value()) { + Config::conf_file = Global::config_file.value(); + } else { + Config::conf_file = Config::conf_dir / "btop.conf"; + } Logger::logfile = Config::get_log_file(); Theme::user_theme_dir = Config::conf_dir / "themes";