mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-27 05:40:32 +01:00
Improve command line parser.
This commit is contained in:
@@ -2,9 +2,11 @@ cmake_minimum_required(VERSION 3.15)
|
|||||||
|
|
||||||
project(aoc2019)
|
project(aoc2019)
|
||||||
|
|
||||||
|
find_package(Boost REQUIRED COMPONENTS program_options)
|
||||||
|
|
||||||
add_library(AoCSolutions src/solutions.hpp src/solutions.cpp src/day01.cpp)
|
add_library(AoCSolutions src/solutions.hpp src/solutions.cpp src/day01.cpp)
|
||||||
target_compile_features(AoCSolutions PUBLIC cxx_std_17)
|
target_compile_features(AoCSolutions PUBLIC cxx_std_17)
|
||||||
|
|
||||||
add_executable(runner src/runner.cpp)
|
add_executable(runner src/runner.cpp)
|
||||||
target_compile_features(runner PUBLIC cxx_std_17)
|
target_compile_features(runner PUBLIC cxx_std_17)
|
||||||
target_link_libraries(runner AoCSolutions)
|
target_link_libraries(runner AoCSolutions Boost::program_options)
|
||||||
|
|||||||
@@ -1,29 +1,37 @@
|
|||||||
#include "solutions.hpp"
|
#include "solutions.hpp"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <charconv>
|
#include <boost/program_options.hpp>
|
||||||
#include <cstring>
|
|
||||||
|
|
||||||
template<typename Int, int Radix = 10>
|
namespace po = boost::program_options;
|
||||||
Int int_from_cstr(Int &value, const char *begin) {
|
|
||||||
const char *end = begin + std::strlen(begin);
|
|
||||||
auto result = std::from_chars(begin, end, value, Radix);
|
|
||||||
if (result.ec != std::errc()) {
|
|
||||||
throw std::invalid_argument("Unparseable integer");
|
|
||||||
}
|
|
||||||
|
|
||||||
return value;
|
struct AoCOptions {
|
||||||
|
int day;
|
||||||
|
bool part2;
|
||||||
|
};
|
||||||
|
|
||||||
|
AoCOptions parse_options(const int argc, const char* argv[]) {
|
||||||
|
AoCOptions options{};
|
||||||
|
po::options_description desc("Allowed options");
|
||||||
|
desc.add_options()
|
||||||
|
("day", po::value<int>(&options.day)->required(), "The day to run.")
|
||||||
|
("part2,2", po::bool_switch(&options.part2), "Whether to run part 2.");
|
||||||
|
|
||||||
|
po::positional_options_description positionals;
|
||||||
|
positionals.add("day", 1);
|
||||||
|
|
||||||
|
po::variables_map vm;
|
||||||
|
|
||||||
|
po::store(po::command_line_parser(argc, argv).options(desc).positional(positionals).run(), vm);
|
||||||
|
po::notify(vm);
|
||||||
|
|
||||||
|
return options;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, const char *argv[]) {
|
int main(int argc, const char *argv[]) {
|
||||||
if (argc < 2) {
|
try {
|
||||||
std::cerr << "Specify a day to run.\n";
|
const auto options = parse_options(argc, argv);
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
int day;
|
const aoc2019::solution_t solution = aoc2019::get_implementation(options.day, options.part2);
|
||||||
int_from_cstr(day, argv[1]);
|
|
||||||
|
|
||||||
const aoc2019::solution_t solution = aoc2019::get_implementation(day);
|
|
||||||
if (solution != nullptr) {
|
if (solution != nullptr) {
|
||||||
solution(std::cin, std::cout);
|
solution(std::cin, std::cout);
|
||||||
return 0;
|
return 0;
|
||||||
@@ -31,4 +39,10 @@ int main(int argc, const char *argv[]) {
|
|||||||
std::cerr << "Unimplemented.\n";
|
std::cerr << "Unimplemented.\n";
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
} catch (po::required_option &ignored) {
|
||||||
|
return 1;
|
||||||
|
} catch (std::out_of_range &ignored) {
|
||||||
|
std::cerr << "Invalid day.\n";
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user