From 9101c98f951e7b104e6969fd5984379ca04745ea Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Mon, 16 Oct 2017 14:15:25 +0100 Subject: Add demo command line parsing --- src/main/command_line_helper.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 src/main/command_line_helper.cpp (limited to 'src/main/command_line_helper.cpp') diff --git a/src/main/command_line_helper.cpp b/src/main/command_line_helper.cpp new file mode 100644 index 0000000..38b9af0 --- /dev/null +++ b/src/main/command_line_helper.cpp @@ -0,0 +1,18 @@ +// +// Created by thecharlesblake on 10/16/17. +// + +#include "command_line_helper.h" +#include + +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() + ("help", "produce help message") + ("compression", po::value(), "set compression level"); + + return desc; +} \ No newline at end of file -- cgit v1.2.1 From dc01ab729c749fbfd2421943198a038d126aa66c Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Mon, 16 Oct 2017 22:56:16 +0100 Subject: Add simple black hole cmdline parse & err handling --- src/main/command_line_helper.cpp | 121 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 113 insertions(+), 8 deletions(-) (limited to 'src/main/command_line_helper.cpp') 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 +#include +#include + #include +#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(), "set compression level"); + ("type", po::value(), + "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>(), + "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>(); + } + + if (vm.count("type")) { + solitaire_type = vm["type"].as(); + } + + 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 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; +} -- cgit v1.2.1 From a841befd0ecdc0b469d6bc46b209f259358778ec Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Fri, 27 Oct 2017 23:25:50 +0100 Subject: Add random game generation to bh --- src/main/command_line_helper.cpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'src/main/command_line_helper.cpp') diff --git a/src/main/command_line_helper.cpp b/src/main/command_line_helper.cpp index 9e389fd..67bf7ff 100644 --- a/src/main/command_line_helper.cpp +++ b/src/main/command_line_helper.cpp @@ -15,13 +15,14 @@ using namespace std; namespace po = boost::program_options; command_line_helper::command_line_helper() - : main_options("options") { + : main_options("options"), random_deal(-1) { main_options.add_options() ("help", "produce help message") ("type", po::value(), "specify the type of the solitaire game to be solved (REQUIRED)") - ("random", "create and solve a random solitaire deal"); + ("random", po::value(), "create and solve a random solitaire " + "deal based on a seed"); po::options_description hidden_options("Hidden options"); hidden_options.add_options() @@ -55,7 +56,9 @@ bool command_line_helper::parse(int argc, const char* argv[]) { solitaire_type = vm["type"].as(); } - random_deal = vm.count("random"); + if (vm.count("random")) { + random_deal = vm["random"].as(); + } // Handle logic error scenarios return assess_errors(); @@ -114,7 +117,7 @@ const string command_line_helper::get_solitaire_type() { return solitaire_type; } -bool command_line_helper::get_random_deal() { +int command_line_helper::get_random_deal() { return random_deal; } -- cgit v1.2.1 From 70522b28586ba117ff9e1e27c8f4b183f8414fd1 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Sat, 28 Oct 2017 10:13:13 +0100 Subject: Add simple and regular bh types --- src/main/command_line_helper.cpp | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) (limited to 'src/main/command_line_helper.cpp') diff --git a/src/main/command_line_helper.cpp b/src/main/command_line_helper.cpp index 67bf7ff..fdf6c9c 100644 --- a/src/main/command_line_helper.cpp +++ b/src/main/command_line_helper.cpp @@ -75,18 +75,35 @@ bool command_line_helper::assess_errors() { return false; } - if (random_deal && !input_files.empty()) { + bool valid_sol_type = assess_sol_type(); + if (!valid_sol_type) return false; + + if (random_deal >= 0 && !input_files.empty()) { print_rand_plus_input_err(); return false; } - if (input_files.empty() && !random_deal) { + if (input_files.empty() && random_deal >= 0) { print_no_input_error(); return false; } return true; } +bool command_line_helper::assess_sol_type() { + if (solitaire_type == "black-hole") { + return true; + } else if (solitaire_type == "simple-black-hole") { + return true; + } else { + cerr << "Error: Solitaire type is not valid: " << solitaire_type + << "\nValid solitaire types are: 'black-hole' and " + "'simple-black-hole'"; + print_help(); + return false; + } +} + void command_line_helper::print_help() { cout << "Usage: solvitaire [options] input-file1 input-file2 ...\n" << main_options << "\n"; -- cgit v1.2.1