aboutsummaryrefslogtreecommitdiff
path: root/src/main/input-output/command_line_helper.cpp
diff options
context:
space:
mode:
authorCharlie Blake2017-11-14 02:24:07 +0000
committerGitHub2017-11-14 02:24:07 +0000
commit995ef52047458e1d59c396df7148ae55b7b93a3c (patch)
treee2d76d46bb435d01760bb01db9e4123f33063a2d /src/main/input-output/command_line_helper.cpp
parentMerge pull request #20 from thecharlesblake/initial-code-cleanup (diff)
parentAdd logic for reading preset sols and file json (diff)
downloadmetasol-995ef52047458e1d59c396df7148ae55b7b93a3c.tar.gz
metasol-995ef52047458e1d59c396df7148ae55b7b93a3c.zip

Merge pull request #21 from thecharlesblake/json-rules

Json rules

Diffstat (limited to 'src/main/input-output/command_line_helper.cpp')
-rw-r--r--src/main/input-output/command_line_helper.cpp55
1 files changed, 34 insertions, 21 deletions
diff --git a/src/main/input-output/command_line_helper.cpp b/src/main/input-output/command_line_helper.cpp
index e61c546..49bb300 100644
--- a/src/main/input-output/command_line_helper.cpp
+++ b/src/main/input-output/command_line_helper.cpp
@@ -6,6 +6,7 @@
#include <vector>
#include <boost/program_options.hpp>
+#include <input-output/sol_preset_types.h>
#include "command_line_helper.h"
#include "game/sol_rules.h"
@@ -21,9 +22,14 @@ command_line_helper::command_line_helper()
main_options.add_options()
("help", "produce help message")
("type", po::value<string>(),
- "specify the type of the solitaire game to be solved (REQUIRED)")
+ "specify the type of the solitaire game to be solved. Must supply "
+ "either 'type' or 'rules' file")
+ ("rules", po::value<string>(),
+ "the path to a JSON file describing the rules of the solitaire "
+ "to be solved. Must supply either 'type' or 'rules' file")
("random", po::value<int>(), "create and solve a random solitaire "
- "deal based on a seed");
+ "deal based on a seed. Must supply either 'random' or list "
+ "of deals to be sold.");
po::options_description hidden_options("Hidden options");
hidden_options.add_options()
@@ -62,6 +68,10 @@ bool command_line_helper::parse(int argc, const char* argv[]) {
solitaire_type = vm["type"].as<string>();
}
+ if (vm.count("rules")) {
+ rules_file = vm["rules"].as<string>();
+ }
+
if (vm.count("random")) {
random_deal = vm["random"].as<int>();
} else {
@@ -78,14 +88,6 @@ bool command_line_helper::assess_errors() {
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;
-
// The user must either supply input files or a random seed
if (random_deal != -1 && !input_files.empty()) {
@@ -97,22 +99,28 @@ bool command_line_helper::assess_errors() {
print_no_input_error();
return false;
}
- return true;
+
+ // The user must supply either a solitaire type or a rules file
+ if ((solitaire_type.empty() && rules_file.empty())
+ || (!solitaire_type.empty() && !rules_file.empty())) {
+ print_sol_type_rules_error();
+ return false;
+ }
+
+ if (!solitaire_type.empty()) {
+ return assess_sol_type();
+ } else {
+ return true;
+ }
}
// Checks if the supplied solitaire type is in the list of valid solitaires
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()) {
+ if (sol_preset_types::is_valid_preset(solitaire_type)) {
return true;
} else {
- LOG_ERROR ("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";
+ LOG_ERROR ("Error: Solitaire type is not valid: " << solitaire_type);
print_help();
return false;
}
@@ -129,8 +137,9 @@ void command_line_helper::print_no_input_error() {
print_help();
}
-void command_line_helper::print_no_sol_type_error() {
- LOG_ERROR ("Error: User must supply a solitaire type");
+void command_line_helper::print_sol_type_rules_error() {
+ LOG_ERROR ("Error: User must supply either a solitaire type, or a 'rules' "
+ "file");
print_help();
}
@@ -148,6 +157,10 @@ const string command_line_helper::get_solitaire_type() {
return solitaire_type;
}
+const string command_line_helper::get_rules_file() {
+ return rules_file;
+}
+
int command_line_helper::get_random_deal() {
return random_deal;
}