-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommandsoption.hpp
107 lines (80 loc) · 2.85 KB
/
commandsoption.hpp
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
#pragma once
#include "abstractcommandoption.h"
#include <exception>
#include <iostream>
template <typename T>
class CommandsOption final : public AbstractCommandOption
{
public:
explicit CommandsOption(const std::string &key,
const std::string &alternativeKey,
const std::string &description)
: AbstractCommandOption(key, alternativeKey, description)
{
}
T getValue() const {
return m_value;
}
std::string toString() {
return (typeid(m_value) == typeid(std::string)) ? m_value : std::to_string(m_value);
}
protected:
bool tryParse() override {
try {
m_value = this->parse(m_commandArguments, m_value);
}
catch (const std::exception &e) {
std::cout << e.what() << std::endl;
return false;
}
return true;
}
static int parse(const std::vector<std::string> &args, const int &) {
if (args.size() != 1)
throw std::bad_cast();
return std::stoi(args.at(first));
}
static bool parse(const std::vector<std::string> &args, const bool &b = true) {
if (args.size() != 0)
throw std::runtime_error("A boolean command line parameter cannot have any arguments.");
return b;
}
static double parse(const std::vector<std::string> &args, const double &) {
if (args.size() != 1)
throw std::bad_cast();
return std::stod(args.at(first));
}
static float parse(const std::vector<std::string> &args, const float &) {
if (args.size() != 1)
throw std::bad_cast();
return std::stof(args.at(first));
}
static long double parse(const std::vector<std::string> &args, const long double &) {
if (args.size() != 1)
throw std::bad_cast();
return std::stold(args.at(first));
}
static unsigned int parse(const std::vector<std::string> &args, const unsigned int &) {
if (args.size() != 1)
throw std::bad_cast();
return static_cast<unsigned int>(std::stoul(args.at(first)));
}
static unsigned long parse(const std::vector<std::string> &args, const unsigned long &) {
if (args.size() != 1)
throw std::bad_cast();
return std::stoul(args.at(first));
}
static long parse(const std::vector<std::string> &args, const long &) {
if (args.size() != 1)
throw std::bad_cast();
return std::stol(args.at(first));
}
static std::string parse(const std::vector<std::string> &args, const std::string &) {
if (args.size() != 1)
throw std::bad_cast();
return args.at(first);
}
private:
T m_value;
enum { first = 0 };
};