aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorthecharlesblake2017-10-16 22:56:16 +0100
committerthecharlesblake2017-10-26 14:51:33 +0100
commitdc01ab729c749fbfd2421943198a038d126aa66c (patch)
tree7be21afce1e4cf67607a098e48a861256d69c159
parentAdd demo command line parsing (diff)
downloadmetasol-dc01ab729c749fbfd2421943198a038d126aa66c.tar.gz
metasol-dc01ab729c749fbfd2421943198a038d126aa66c.zip

Add simple black hole cmdline parse & err handling

-rw-r--r--.idea/codeStyleSettings.xml35
-rw-r--r--CMakeLists.txt2
-rw-r--r--src/main/command_line_helper.cpp121
-rw-r--r--src/main/command_line_helper.h24
-rw-r--r--src/main/log_helper.h17
-rw-r--r--src/main/main.cpp28
6 files changed, 202 insertions, 25 deletions
diff --git a/.idea/codeStyleSettings.xml b/.idea/codeStyleSettings.xml
new file mode 100644
index 0000000..8c27bb1
--- /dev/null
+++ b/.idea/codeStyleSettings.xml
@@ -0,0 +1,35 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+ <component name="ProjectCodeStyleSettingsManager">
+ <option name="PER_PROJECT_SETTINGS">
+ <value>
+ <Objective-C-extensions>
+ <file>
+ <option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Import" />
+ <option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Macro" />
+ <option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Typedef" />
+ <option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Enum" />
+ <option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Constant" />
+ <option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Global" />
+ <option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Struct" />
+ <option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="FunctionPredecl" />
+ <option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Function" />
+ </file>
+ <class>
+ <option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Property" />
+ <option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Synthesize" />
+ <option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="InitMethod" />
+ <option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="StaticMethod" />
+ <option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="InstanceMethod" />
+ <option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="DeallocMethod" />
+ </class>
+ <extensions>
+ <pair source="cpp" header="h" />
+ <pair source="c" header="h" />
+ </extensions>
+ </Objective-C-extensions>
+ </value>
+ </option>
+ <option name="PREFERRED_PROJECT_CODE_STYLE" value="Default" />
+ </component>
+</project> \ No newline at end of file
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 9fa09c3..86d5232 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -10,7 +10,7 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
## source files ###############################################################
###############################################################################
-set(sources src/main/main.cpp src/main/command_line_helper.cpp src/main/command_line_helper.h)
+set(sources src/main/main.cpp src/main/command_line_helper.cpp src/main/command_line_helper.h src/main/log_helper.h)
set(sources_test src/test/main_test.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 <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;
}