1 #ifndef __COMMAND_OPTION_H
2 #define __COMMAND_OPTION_H
9 #include "exception/missingOptionValue.h"
10 #include "exception/optionFailedConversion.h"
11 #include "exception/optionValueNotSpecified.h"
15 * Class responsible for handling commandline options.
16 * Options are named parameters of program.
19 * - ./myprog OptionName=OptionValue
20 * - ./myprog -f=/some/file
21 * - ./myprog --level=15
23 template<typename ParameterType>
25 : public Parameter, public Callable<ParameterType> {
27 typedef std::string OptionName;
32 const OptionName name;
35 * Current Option value
41 * Default constructor.
43 * @param name Name of the current Option
44 * @param description Description of current Option
45 * @param function Function used to handle current Option.
47 Option(const std::string & name, const std::string & description, void (*function)(ParameterType))
48 : Parameter(description), Callable<ParameterType>(function), name(name) {
59 virtual void handle() {
65 * Method used for checking if Option understands given user value.
66 * If so current Option is flagged as used and no more checks against
67 * it will be done in future.
69 * Passed value should be in form of:
70 * OptionName=OptionValue
72 * If no equal sign is after OptionName part,
73 * std::invalid_argument exception with appropriate message is thrown
75 * If conversion of OptionValue part to ParameterType failed,
76 * std::invalid_argument exception with appropriate message is thrown
78 * @param argv command line value against which test will be made.
79 * User value should be in format: OptionName=OptionValue.
81 * @return If passed argv succesfully detected OptionName part as a
82 * current option and its OptionValue part has been succesfully
83 * converted to ParameterType, returns true and Option is set as used one.
84 * Otherwise returns false and can be used to check against next value.
86 * @throw MissingOptionValue when OptionValue part is missing after
88 * @throw OptionFailedConversion when OptionValue part failed conversion
91 virtual bool understand(const std::string & argv) {
93 if (argv.find(name) == 0) {
94 std::size_t pos = this->valuePosition(argv);
96 if (pos != name.size()) {
97 throw MissingOptionValue("Option: " + name + " requires value but no one has been provided");
100 std::stringstream ss;
101 ss << argv.substr(pos + 1);
105 throw OptionFailedConversion("Value for option: " + name + " failed conversion to the required type");
116 virtual unsigned int valuePosition(const std::string & value) {
117 std::size_t pos = value.find("=");
119 if (pos == std::string::npos) {
120 throw OptionValueNotSpecified("Option: " + name + " requires value to be specified using equal sign");
128 * Template class responsible for handling commandline options.
129 * Options are non-required, named parameters of program.
130 * This template specialization allows Options to work like switches.
131 * It means that just named parameter is needed to invoke command. No value
135 * ./myprog OptionName
141 : public Parameter, public Callable<void> {
143 typedef std::string OptionName;
146 * Current Option name
148 const OptionName name;
151 * Default constructor.
153 * @param name Name of the current Option
154 * @param description Description of current Option
155 * @param function Function used to handle current Option.
157 Option(const std::string & name, const std::string & description, void (*function)(void))
158 : Parameter(description), Callable<void>(function), name(name) {
164 virtual void handle() {
170 * Method used for checking if Option understands given user value.
171 * If so, current Option is flagged as used and no more checks against
172 * it will be done in future.
174 * Passed value should be in form of:
177 * @param argv command line value against which test will be made.
178 * User value should be in format: OptionName.
180 * @return If passed argv succesfully detected OptionName returns true
181 * and Option is set as used one. Otherwise returns false and can be
182 * used to check against next value.
184 virtual bool understand(const std::string & argv) {
194 virtual unsigned int valuePosition(const std::string & ) {
195 throw new std::invalid_argument(this->describe() + " is void Option, so it does not have value part");
200 #endif /* __COMMAND_OPTION_H */