aboutsummaryrefslogtreecommitdiff
path: root/src/main/command_line_helper.cpp
blob: 67bf7ffa52ca0b0f08631a5cbd65d6b13f81c668 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
//
// Created by thecharlesblake on 10/16/17.
//

#include <tuple>
#include <vector>
#include <iostream>

#include <boost/program_options.hpp>

#include "command_line_helper.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;
    }

    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;
}

int command_line_helper::get_random_deal() {
    return random_deal;
}

bool command_line_helper::get_help() {
    return help;
}