diff options
| author | thecharlesblake | 2017-11-11 11:36:27 +0000 |
|---|---|---|
| committer | thecharlesblake | 2017-11-11 11:37:10 +0000 |
| commit | bba8775ce7c63783303240470d4a4b8404c9498b (patch) | |
| tree | a3f6e5dc405ecb4637eca6c54e1261b5630a058c /src/main/input-output/command_line_helper.cpp | |
| parent | Merge pull request #19 from thecharlesblake/canfield (diff) | |
| download | metasol-bba8775ce7c63783303240470d4a4b8404c9498b.tar.gz metasol-bba8775ce7c63783303240470d4a4b8404c9498b.zip | |
Add src file directory structure
Diffstat (limited to 'src/main/input-output/command_line_helper.cpp')
| -rw-r--r-- | src/main/input-output/command_line_helper.cpp | 146 |
1 files changed, 146 insertions, 0 deletions
diff --git a/src/main/input-output/command_line_helper.cpp b/src/main/input-output/command_line_helper.cpp new file mode 100644 index 0000000..b31761a --- /dev/null +++ b/src/main/input-output/command_line_helper.cpp @@ -0,0 +1,146 @@ +// +// Created by thecharlesblake on 10/16/17. +// + +#include <tuple> +#include <vector> +#include <iostream> + +#include <boost/program_options.hpp> + +#include "command_line_helper.h" +#include "game/sol_rules.h" + +using namespace std; + +namespace po = boost::program_options; + +command_line_helper::command_line_helper() + : main_options("options"), random_deal(-1) { + + main_options.add_options() + ("help", "produce help message") + ("type", po::value<string>(), + "specify the type of the solitaire game to be solved (REQUIRED)") + ("random", po::value<int>(), "create and solve a random solitaire " + "deal based on a seed"); + + po::options_description hidden_options("Hidden options"); + hidden_options.add_options() + ("input-files", po::value<vector<string>>(), + "the file containing the deal to be solved"); + + cmdline_options.add(main_options).add(hidden_options); + + p.add("input-files", -1); +} + +bool command_line_helper::parse(int argc, const char* argv[]) { + po::variables_map vm; + try { + store(po::command_line_parser(argc, argv).options(cmdline_options) + .positional(p).run(), vm); + } catch(po::error& e) { + cerr << "Error: " << e.what() << "\n"; + return false; + } + + notify(vm); + + help = vm.count("help"); + + if (vm.count("input-files")) { + input_files = vm["input-files"].as<vector<string>>(); + } + + if (vm.count("type")) { + solitaire_type = vm["type"].as<string>(); + } + + if (vm.count("random")) { + random_deal = vm["random"].as<int>(); + } + + // Handle logic error scenarios + return assess_errors(); +} + +bool command_line_helper::assess_errors() { + if (help) { + print_help(); + return false; + } + + if (solitaire_type.empty()) { + print_no_sol_type_error(); + return false; + } + + bool valid_sol_type = assess_sol_type(); + if (!valid_sol_type) return false; + + if (random_deal != -1 && !input_files.empty()) { + print_rand_plus_input_err(); + return false; + } + + if (input_files.empty() && random_deal == -1) { + print_no_input_error(); + return false; + } + return true; +} + +bool command_line_helper::assess_sol_type() { + if (find(sol_rules::valid_sol_strs.begin(), + sol_rules::valid_sol_strs.end(), + solitaire_type) != sol_rules::valid_sol_strs.end()) { + return true; + } else { + cerr << "Error: Solitaire type is not valid: " << solitaire_type + << "\nValid solitaire types are: "; + copy(begin(sol_rules::valid_sol_strs), end(sol_rules::valid_sol_strs), + ostream_iterator<string>(cerr, ", ")); + cerr << "\n"; + print_help(); + return false; + } +} + +void command_line_helper::print_help() { + cerr << "Usage: solvitaire [options] input-file1 input-file2 ...\n" + << main_options << "\n"; +} + +void command_line_helper::print_no_input_error() { + cerr << "Error: User must supply input file(s) or the '--random' " + "option\n"; + print_help(); +} + +void command_line_helper::print_no_sol_type_error() { + cerr << "Error: User must supply a solitaire type\n"; + print_help(); +} + +void command_line_helper::print_rand_plus_input_err() { + cerr << "Error: User must supply input file(s) or the '--random' option, " + "not both\n"; + print_help(); +} + +const vector<string> command_line_helper::get_input_files() { + return input_files; +} + +const string command_line_helper::get_solitaire_type() { + return solitaire_type; +} + +int command_line_helper::get_random_deal() { + return random_deal; +} + +bool command_line_helper::get_help() { + return help; +} |
