From 2debafe5d659d104f09249e22c07762569997fc5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Ke=C3=9Fler?= Date: Fri, 10 Nov 2023 14:08:36 +0100 Subject: [PATCH] use option-driven command line interface --- src/main.cpp | 64 ++++++++++++++++++++++++++++------------------------ 1 file changed, 35 insertions(+), 29 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 69fa8fe..d61cde9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,6 +3,8 @@ #include #include +#include + #include "game_state.h" #include "download.h" #include "cli_interface.h" @@ -53,37 +55,41 @@ namespace Hanabi { int main(int argc, char *argv[]) { - if(argc == 3 or argc == 4) { - std::string game_str(argv[1]); - std::string turn_str (argv[2]); + int turn; + std::optional score; - int turn = 0; - try { - turn = std::stoi(turn_str); - } catch(std::invalid_argument&) { - std::cout << "Could not parse turn number " << turn << std::endl; - return EXIT_FAILURE; - } + boost::program_options::options_description desc("Allowed options"); + desc.add_options() + ("help", "print this help message") + ("id,g", boost::program_options::value(), "Game ID from hanab.live") + ("file,f", boost::program_options::value(), "Input file containing game in hanab.live json format") + ("turn,t", boost::program_options::value(&turn)->default_value(1), "Turn number of state to analyze. Turn 1 means no actions have been taken.") + ("score,s", boost::program_options::value(), "Score that counts as a win, i.e. is optimized for achieving.") + ("interactive,i", "Drop into interactive shell to explore game") + ; + boost::program_options::variables_map vm; + boost::program_options::store(boost::program_options::parse_command_line(argc, argv, desc), vm); + boost::program_options::notify(vm); - std::optional score_goal = std::nullopt; - if (argc == 4) { - std::string score_goal_str(argv[3]); - try { - score_goal = std::stoi(score_goal_str); - } catch(std::invalid_argument&) { - std::cout << "Could not parse score goal number " << score_goal_str; - return EXIT_FAILURE; - } - } - - try { - Hanabi::analyze_game_and_start_cli(std::stoi(game_str), turn, score_goal); - } catch(std::invalid_argument&) { - Hanabi::analyze_game_and_start_cli(game_str.c_str(), turn, score_goal); - } + if (vm.count("help")) { + std::cout << desc << std::endl; + return EXIT_SUCCESS; } - else { - Hanabi::print_usage(argv[0]); + + if (vm.count("file") + vm.count("id") != 1) { + std::cout << "Exactly one option of 'file' and 'id' has to be given." << std::endl; + std::cout << "Use '--help' to print a help message." << std::endl; + return EXIT_SUCCESS; } - return 0; + + if (vm.count("score")) { + score = vm["score"].as(); + } + + if (vm.count("file")) { + Hanabi::analyze_game_and_start_cli(vm["file"].as().c_str(), turn, score); + } else { + Hanabi::analyze_game_and_start_cli(vm["id"].as(), turn, score); + } + return EXIT_SUCCESS; }