Command  0.3
option.h
1#ifndef __COMMAND_OPTION_H
2#define __COMMAND_OPTION_H
3
4#include <string>
5#include <sstream>
6#include <stdexcept>
7
8#include "parameter.h"
9#include "exception/missingOptionValue.h"
10#include "exception/optionFailedConversion.h"
11#include "exception/optionValueNotSpecified.h"
12
13namespace command {
23 template<typename ParameterType>
24 class Option
25 : public Parameter, public Callable<ParameterType> {
26 public:
27 typedef std::string OptionName;
28 protected:
32 const OptionName name;
33
37 ParameterType value;
38
39 public:
47 Option(const std::string & name, const std::string & description, std::function<void(ParameterType)> function)
48 : Parameter(description), Callable<ParameterType>(function), name(name) {
49 }
50
54 virtual ~Option() { }
55
59 virtual void handle() {
60 this->call(value);
61 used = true;
62 }
63
91 virtual bool understand(const std::string & argv) {
92 if (this->hasName(argv)) {
93 std::size_t pos = this->valuePosition(argv);
94
95 if (pos != name.size()) {
96 throw MissingOptionValue("Option: " + name + " requires value but no one has been provided");
97 }
98
99 std::stringstream ss;
100 ss << std::fixed << argv.substr(pos + 1);
101 ss >> value;
102
103 if (ss.fail()) {
104 throw OptionFailedConversion("Option: " + name + " failed value conversion to the required type");
105 }
106
107 return true;
108 }
109 return false;
110 }
111
115 virtual unsigned int valuePosition(const std::string & value) {
116 std::size_t pos = value.find("=");
117
118 if ((this->hasName(value)) && (pos == std::string::npos)) {
119 throw OptionValueNotSpecified("Option: " + name + " requires value to be specified after equal sign, but no equal sign was found");
120 }
121
122 return pos;
123 }
124
125 protected:
126 bool hasName(const std::string & argv) {
127 return argv.find(name) == 0;
128 }
129 };
130
143 template<>
144 class Option<void>
145 : public Parameter, public Callable<void> {
146 public:
147 typedef std::string OptionName;
148 protected:
152 const OptionName name;
153 public:
161 Option(const std::string & name, const std::string & description, std::function<void(void)> function)
162 : Parameter(description), Callable<void>(function), name(name) {
163 }
164
168 virtual void handle() {
169 this->call();
170 used = true;
171 }
172
188 virtual bool understand(const std::string & argv) {
189 if (argv == name) {
190 return true;
191 }
192 return false;
193 }
194
198 virtual unsigned int valuePosition(const std::string & ) {
199 throw new std::invalid_argument(this->describe() + " is void Option, so it does not have value part");
200 }
201 };
202}
203
204#endif /* __COMMAND_OPTION_H */
Definition: callable.h:12
void call(ParameterType value)
Definition: callable.h:37
const std::string & describe()
Definition: descriptive.h:29
Definition: missingOptionValue.h:12
Definition: optionFailedConversion.h:15
Definition: optionValueNotSpecified.h:13
virtual unsigned int valuePosition(const std::string &)
Definition: option.h:198
virtual bool understand(const std::string &argv)
Definition: option.h:188
const OptionName name
Definition: option.h:152
virtual void handle()
Definition: option.h:168
Option(const std::string &name, const std::string &description, std::function< void(void)> function)
Definition: option.h:161
Definition: option.h:25
virtual bool understand(const std::string &argv)
Definition: option.h:91
virtual unsigned int valuePosition(const std::string &value)
Definition: option.h:115
Option(const std::string &name, const std::string &description, std::function< void(ParameterType)> function)
Definition: option.h:47
ParameterType value
Definition: option.h:37
const OptionName name
Definition: option.h:32
virtual void handle()
Definition: option.h:59
Definition: parameter.h:17
bool used
Definition: parameter.h:20