1 #ifndef __COMMAND_OPTION_H
2 #define __COMMAND_OPTION_H
9 #include "exception/missingOptionValue.h"
10 #include "exception/optionFailedConversion.h"
14 * Class responsible for handling commandline options.
15 * Options are named parameters of program.
18 * - ./myprog OptionName=OptionValue
19 * - ./myprog -f=/some/file
20 * - ./myprog --level=15
22 template<typename ParameterType>
24 : public Parameter, public Callable<ParameterType> {
26 typedef std::string OptionName;
31 const OptionName name;
34 * Current Option value
40 * Default constructor.
42 * @param name Name of the current Option
43 * @param description Description of current Option
44 * @param function Function used to handle current Option.
46 Option(const std::string & name, const std::string & description, void (*function)(ParameterType))
47 : Parameter(description), Callable<ParameterType>(function), name(name) {
58 virtual void handle() {
64 * Method used for checking if Option understands given user value.
65 * If so current Option is flagged as used and no more checks against
66 * it will be done in future.
68 * Passed value should be in form of:
69 * OptionName=OptionValue
71 * If no equal sign is after OptionName part,
72 * std::invalid_argument exception with appropriate message is thrown
74 * If conversion of OptionValue part to ParameterType failed,
75 * std::invalid_argument exception with appropriate message is thrown
77 * @param argv command line value against which test will be made.
78 * User value should be in format: OptionName=OptionValue.
80 * @return If passed argv succesfully detected OptionName part as a
81 * current option and its OptionValue part has been succesfully
82 * converted to ParameterType, returns true and Option is set as used one.
83 * Otherwise returns false and can be used to check against next value.
85 * @throw MissingOptionValue when OptionValue part is missing after
87 * @throw OptionFailedConversion when OptionValue part failed conversion
90 virtual bool understand(const std::string & argv) {
92 if (argv.find(name) == 0) {
93 std::size_t pos = argv.find("=");
95 if (pos != name.size()) {
96 throw MissingOptionValue("Option: " + name + " requires value but no one has been provided");
100 ss << argv.substr(pos + 1);
104 throw OptionFailedConversion("Value for option: " + name + " failed conversion to the required type");
114 * Template class responsible for handling commandline options.
115 * Options are non-required, named parameters of program.
116 * This template specialization allows Options to work like switches.
117 * It means that just named parameter is needed to invoke command. No value
121 * ./myprog OptionName
127 : public Parameter, public Callable<void> {
129 typedef std::string OptionName;
132 * Current Option name
134 const OptionName name;
137 * Default constructor.
139 * @param name Name of the current Option
140 * @param description Description of current Option
141 * @param function Function used to handle current Option.
143 Option(const std::string & name, const std::string & description, void (*function)(void))
144 : Parameter(description), Callable<void>(function), name(name) {
150 virtual void handle() {
156 * Method used for checking if Option understands given user value.
157 * If so, current Option is flagged as used and no more checks against
158 * it will be done in future.
160 * Passed value should be in form of:
163 * @param argv command line value against which test will be made.
164 * User value should be in format: OptionName.
166 * @return If passed argv succesfully detected OptionName returns true
167 * and Option is set as used one. Otherwise returns false and can be
168 * used to check against next value.
170 virtual bool understand(const std::string & argv) {
179 #endif /* __COMMAND_OPTION_H */