use option-driven command line interface

This commit is contained in:
Maximilian Keßler 2023-11-10 14:08:36 +01:00
parent 4e67ffa9ee
commit 2debafe5d6
Signed by: max
GPG Key ID: BCC5A619923C0BA5

View File

@ -3,6 +3,8 @@
#include <variant>
#include <chrono>
#include <boost/program_options.hpp>
#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<int> 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<int>(), "Game ID from hanab.live")
("file,f", boost::program_options::value<std::string>(), "Input file containing game in hanab.live json format")
("turn,t", boost::program_options::value<int>(&turn)->default_value(1), "Turn number of state to analyze. Turn 1 means no actions have been taken.")
("score,s", boost::program_options::value<int>(), "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<uint8_t> 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<int>();
}
if (vm.count("file")) {
Hanabi::analyze_game_and_start_cli(vm["file"].as<std::string>().c_str(), turn, score);
} else {
Hanabi::analyze_game_and_start_cli(vm["id"].as<int>(), turn, score);
}
return EXIT_SUCCESS;
}