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) {
92 if (this->hasName(argv)) {
93 std::size_t pos = this->valuePosition(argv);
95 if (pos != name.size()) {
96 throw MissingOptionValue("Option: " + name + " requires value but no one has been provided");
100 ss << std::fixed << argv.substr(pos + 1);
104 throw OptionFailedConversion("Option: " + name + " failed value conversion to the required type");
115 virtual unsigned int valuePosition(const std::string & value) {
116 std::size_t pos = value.find("=");
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");
126 bool hasName(const std::string & argv) {
127 return argv.find(name) == 0;
132 * Template class responsible for handling commandline options.
133 * Options are non-required, named parameters of program.
134 * This template specialization allows Options to work like switches.
135 * It means that just named parameter is needed to invoke command. No value
139 * ./myprog OptionName
145 : public Parameter, public Callable<void> {
147 typedef std::string OptionName;
150 * Current Option name
152 const OptionName name;
155 * Default constructor.
157 * @param name Name of the current Option
158 * @param description Description of current Option
159 * @param function Function used to handle current Option.
161 Option(const std::string & name, const std::string & description, void (*function)(void))
162 : Parameter(description), Callable<void>(function), name(name) {
168 virtual void handle() {
174 * Method used for checking if Option understands given user value.
175 * If so, current Option is flagged as used and no more checks against
176 * it will be done in future.
178 * Passed value should be in form of:
181 * @param argv command line value against which test will be made.
182 * User value should be in format: OptionName.
184 * @return If passed argv succesfully detected OptionName returns true
185 * and Option is set as used one. Otherwise returns false and can be
186 * used to check against next value.
188 virtual bool understand(const std::string & argv) {
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");
204 #endif /* __COMMAND_OPTION_H */