Skip to content

Commit

Permalink
parsing command line fix
Browse files Browse the repository at this point in the history
  • Loading branch information
happyCupcake committed Jan 12, 2025
1 parent 98431c6 commit b5a64d0
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 47 deletions.
4 changes: 1 addition & 3 deletions drivebrain_app/include/DriveBrainApp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,14 @@ 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();
void stop();

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:
Expand Down
31 changes: 30 additions & 1 deletion drivebrain_app/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string, std::string> 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<std::string>(&param_path), "Path to the parameter JSON file")
("dbc-path,d", po::value<std::string>(&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());
Expand Down
47 changes: 4 additions & 43 deletions drivebrain_app/src/DriveBrainApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,9 @@

std::atomic<bool> 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<std::string>(&param_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<std::string>(), "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<std::string>();
}
}

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)
Expand Down Expand Up @@ -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<common::MCAPProtobufLogger>("temp");
Expand Down

0 comments on commit b5a64d0

Please sign in to comment.