diff --git a/drivebrain_app/include/DriveBrainApp.hpp b/drivebrain_app/include/DriveBrainApp.hpp index ee8bba7..c90b9ff 100644 --- a/drivebrain_app/include/DriveBrainApp.hpp +++ b/drivebrain_app/include/DriveBrainApp.hpp @@ -37,7 +37,7 @@ struct DriveBrainSettings { class DriveBrainApp { public: - DriveBrainApp(int argc, char* argv[], const DriveBrainSettings& settings = DriveBrainSettings{}); + DriveBrainApp(const std::string& param_path, const std::string& dbc_path, const DriveBrainSettings& settings = DriveBrainSettings{}); ~DriveBrainApp(); void run(); @@ -45,8 +45,6 @@ class DriveBrainApp { private: // Private member functions - std::string _get_param_path_from_args(int argc, char* argv[]); - void _parse_command_line(int argc, char* argv[]); void _process_loop(); private: diff --git a/drivebrain_app/main.cpp b/drivebrain_app/main.cpp index dfe2180..c12449f 100644 --- a/drivebrain_app/main.cpp +++ b/drivebrain_app/main.cpp @@ -54,17 +54,46 @@ void signal_handler(int signal) stop_signal.store(true); // Set running to false to exit the main loop or gracefully terminate } + +std::pair parse_arguments(int argc, char* argv[]) { + namespace po = boost::program_options; + po::options_description desc("Allowed options"); + std::string param_path = "config/drivebrain_config.json"; + std::string dbc_path; + + desc.add_options() + ("help,h", "produce help message") + ("param-path,p", po::value(¶m_path), "Path to the parameter JSON file") + ("dbc-path,d", po::value(&dbc_path), "Path to the DBC file (optional)"); + + po::variables_map vm; + po::store(po::parse_command_line(argc, argv, desc), vm); + po::notify(vm); + + if (vm.count("help")) { + std::cout << desc << std::endl; + std::exit(0); + } + + return {param_path, dbc_path}; +} + int main(int argc, char *argv[]) { std::signal(SIGINT, signal_handler); try { + + auto [param_path, dbc_path] = parse_arguments(argc, argv); + DriveBrainSettings settings{ .run_db_service = true, .run_io_context = true, .run_process_loop = true }; - DriveBrainApp app(argc, argv, settings); + + DriveBrainApp app(param_path, dbc_path, settings); + app.run(); } catch (const std::exception& e) { spdlog::error("Error in main: {}", e.what()); diff --git a/drivebrain_app/src/DriveBrainApp.cpp b/drivebrain_app/src/DriveBrainApp.cpp index 63e5ddf..73cce9a 100644 --- a/drivebrain_app/src/DriveBrainApp.cpp +++ b/drivebrain_app/src/DriveBrainApp.cpp @@ -3,48 +3,9 @@ std::atomic DriveBrainApp::_stop_signal{false}; -std::string DriveBrainApp::_get_param_path_from_args(int argc, char* argv[]) { - namespace po = boost::program_options; - po::options_description desc("Allowed options"); - std::string param_path; - - desc.add_options() - ("param-path,p", po::value(¶m_path)->default_value("config/drivebrain_config.json"), - "Path to the parameter JSON file"); - - po::variables_map vm; - po::store(po::command_line_parser(argc, argv).options(desc).allow_unregistered().run(), vm); - po::notify(vm); - - return param_path; -} - -void DriveBrainApp::_parse_command_line(int argc, char* argv[]) { - namespace po = boost::program_options; - po::options_description desc("Allowed options"); - - desc.add_options() - ("help,h", "produce help message") - ("dbc-path,d", po::value(), "Path to the DBC file (optional)"); - - po::variables_map vm; - po::store(po::command_line_parser(argc, argv).options(desc).allow_unregistered().run(), vm); - po::notify(vm); - - if (vm.count("help")) { - std::stringstream ss; - ss << desc; - spdlog::info("{}", ss.str()); - throw std::runtime_error("Help requested"); - } - - if (vm.count("dbc-path")) { - _dbc_path = vm["dbc-path"].as(); - } -} - -DriveBrainApp::DriveBrainApp(int argc, char* argv[], const DriveBrainSettings& settings) - : _param_path(_get_param_path_from_args(argc, argv)) +DriveBrainApp::DriveBrainApp(const std::string& param_path, const std::string& dbc_path, const DriveBrainSettings& settings) + : _param_path(param_path) + , _dbc_path(dbc_path) , _logger(core::LogLevel::INFO) , _config(_param_path) , _settings(settings) @@ -73,7 +34,7 @@ DriveBrainApp::DriveBrainApp(int argc, char* argv[], const DriveBrainSettings& s _process_loop(); }) { - _parse_command_line(argc, argv); + spdlog::set_level(spdlog::level::warn); _mcap_logger = std::make_unique("temp");