diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/main/command_line_helper.cpp | 121 | ||||
| -rw-r--r-- | src/main/command_line_helper.h | 24 | ||||
| -rw-r--r-- | src/main/log_helper.h | 17 | ||||
| -rw-r--r-- | src/main/main.cpp | 28 |
4 files changed, 166 insertions, 24 deletions
diff --git a/src/main/command_line_helper.cpp b/src/main/command_line_helper.cpp index 38b9af0..9e389fd 100644 --- a/src/main/command_line_helper.cpp +++ b/src/main/command_line_helper.cpp @@ -2,17 +2,122 @@ // Created by thecharlesblake on 10/16/17. // -#include "command_line_helper.h" +#include <tuple> +#include <vector> +#include <iostream> + #include <boost/program_options.hpp> +#include "command_line_helper.h" + +using namespace std; + namespace po = boost::program_options; -po::options_description get_options_desc() { - // Creates the command line options - po::options_description desc("Allowed options"); - desc.add_options() +command_line_helper::command_line_helper() + : main_options("options") { + + main_options.add_options() ("help", "produce help message") - ("compression", po::value<int>(), "set compression level"); + ("type", po::value<string>(), + "specify the type of the solitaire game to be solved (REQUIRED)") + ("random", "create and solve a random solitaire deal"); + + 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>(); + } + + random_deal = vm.count("random"); + + // 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; + } + + if (random_deal && !input_files.empty()) { + print_rand_plus_input_err(); + return false; + } + + if (input_files.empty() && !random_deal) { + print_no_input_error(); + return false; + } + return true; +} + +void command_line_helper::print_help() { + cout << "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; +} + +bool command_line_helper::get_random_deal() { + return random_deal; +} - return desc; -}
\ No newline at end of file +bool command_line_helper::get_help() { + return help; +} diff --git a/src/main/command_line_helper.h b/src/main/command_line_helper.h index dc3b2b6..53a5a5e 100644 --- a/src/main/command_line_helper.h +++ b/src/main/command_line_helper.h @@ -7,6 +7,28 @@ #include <boost/program_options.hpp> -boost::program_options::options_description get_options_desc(); +class command_line_helper { +public: + command_line_helper(); + bool parse(int argc, const char* argv[]); + void print_help(); + void print_no_input_error(); + void print_no_sol_type_error(); + void print_rand_plus_input_err(); + const std::vector<std::string> get_input_files(); + const std::string get_solitaire_type(); + bool get_random_deal(); + bool get_help(); + bool assess_errors(); +private: + boost::program_options::options_description cmdline_options; + boost::program_options::options_description main_options; + boost::program_options::positional_options_description p; + + std::vector<std::string> input_files; + std::string solitaire_type; + bool random_deal; + bool help; +}; #endif //SOLVITAIRE_COMMAND_LINE_HELPER_H diff --git a/src/main/log_helper.h b/src/main/log_helper.h new file mode 100644 index 0000000..8e634a7 --- /dev/null +++ b/src/main/log_helper.h @@ -0,0 +1,17 @@ +// +// Created by thecharlesblake on 10/16/17. +// + +#ifndef SOLVITAIRE_LOG_HELPER_H +#define SOLVITAIRE_LOG_HELPER_H + +#if defined NDEBUG + #define LOG_DEBUG(msg) +#else + #define LOG_DEBUG(msg) BOOST_LOG_TRIVIAL(debug) << msg +#endif +#define LOG_INFO(msg) BOOST_LOG_TRIVIAL(info) << msg +#define LOG_WARNING(msg) BOOST_LOG_TRIVIAL(warning) << msg +#define LOG_ERROR(msg) BOOST_LOG_ERROR(error) << msg + +#endif //SOLVITAIRE_LOG_HELPER_H diff --git a/src/main/main.cpp b/src/main/main.cpp index 8c07d1f..642b27c 100644 --- a/src/main/main.cpp +++ b/src/main/main.cpp @@ -1,29 +1,27 @@ #include <boost/program_options.hpp> -#include <iostream> #include "command_line_helper.h" +#include <iostream> using namespace std; namespace po = boost::program_options; int main(int argc, const char* argv[]) { - // Creates the command line options - po::options_description desc = get_options_desc(); - - po::variables_map vm; - po::store(po::parse_command_line(argc, argv, desc), vm); - po::notify(vm); - - if (vm.count("help")) { - cout << desc << "\n"; + // Parses the command-line options + command_line_helper clh; + bool success = clh.parse(argc, argv); + if (!success) { return 1; } - if (vm.count("compression")) { - cout << "Compression level was set to " - << vm["compression"].as<int>() << ".\n"; - } else { - cout << "Compression level was not set.\n"; + cout << "Random deal: " << clh.get_random_deal() << "\n"; + cout << "Solitaire type: " << clh.get_solitaire_type() << "\n"; + cout << "Filenames: "; + for (auto filename : clh.get_input_files()) { + cout << filename << ", "; } + cout << "\n"; + + return 0; } |
