use option-driven command line interface
This commit is contained in:
parent
4e67ffa9ee
commit
2debafe5d6
1 changed files with 35 additions and 29 deletions
64
src/main.cpp
64
src/main.cpp
|
@ -3,6 +3,8 @@
|
||||||
#include <variant>
|
#include <variant>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
|
|
||||||
|
#include <boost/program_options.hpp>
|
||||||
|
|
||||||
#include "game_state.h"
|
#include "game_state.h"
|
||||||
#include "download.h"
|
#include "download.h"
|
||||||
#include "cli_interface.h"
|
#include "cli_interface.h"
|
||||||
|
@ -53,37 +55,41 @@ namespace Hanabi {
|
||||||
|
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
if(argc == 3 or argc == 4) {
|
int turn;
|
||||||
std::string game_str(argv[1]);
|
std::optional<int> score;
|
||||||
std::string turn_str (argv[2]);
|
|
||||||
|
|
||||||
int turn = 0;
|
boost::program_options::options_description desc("Allowed options");
|
||||||
try {
|
desc.add_options()
|
||||||
turn = std::stoi(turn_str);
|
("help", "print this help message")
|
||||||
} catch(std::invalid_argument&) {
|
("id,g", boost::program_options::value<int>(), "Game ID from hanab.live")
|
||||||
std::cout << "Could not parse turn number " << turn << std::endl;
|
("file,f", boost::program_options::value<std::string>(), "Input file containing game in hanab.live json format")
|
||||||
return EXIT_FAILURE;
|
("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 (vm.count("help")) {
|
||||||
if (argc == 4) {
|
std::cout << desc << std::endl;
|
||||||
std::string score_goal_str(argv[3]);
|
return EXIT_SUCCESS;
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue