1 #ifndef __COMMAND_OPTION_H
2 #define __COMMAND_OPTION_H
12 * Class responsible for handling commandline options.
13 * Options are non-required, named parameters of program.
16 * ./myprog OptionName=OptionValue
17 * ./myprog -f=/some/file
20 template<typename OptionType>
22 : public Parameter, public Callable<OptionType> {
24 typedef std::string OptionName;
29 const OptionName name;
32 * Current Option value
36 /** Variable indicating if current Option was already used or not */
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)(OptionType))
48 : Parameter(description), Callable<OptionType>(function), name(name) {
59 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 OptionType 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 OptionType, returns true and Option is set as used one.
83 * Otherwise returns false and can be used to check against next value.
85 * @throw std::invalid_argument when OptionName part has no equal sign
87 * @throw std::invalid_argument when OptionValue part failed conversion
90 virtual bool understand(const std::string & argv)
91 throw(std::invalid_argument) {
93 if ((!used) && (argv.find(name) == 0)) {
94 std::size_t pos = argv.find("=");
96 if (pos != name.size()) {
97 throw std::invalid_argument("Option: " + name + " requires value but no one has been provided");
100 std::stringstream ss;
101 ss << argv.substr(pos + 1);
102 ss >> value;// memory leak? when uncommented and exception is
103 // thrown, valgrind shows e.g.:
104 // possibly lost: 380 bytes in 7 blocks
107 throw std::invalid_argument("Value for option: " + name + " failed conversion to the required type");
118 * Template class responsible for handling commandline options.
119 * Options are non-required, named parameters of program.
120 * This template specialization allows Options to work like switches.
121 * It means that just named parameter is needed to invoke command. No value
125 * ./myprog OptionName
131 : public Parameter, public Callable<void> {
133 typedef std::string OptionName;
136 * Current Option name
138 const OptionName name;
140 /** Variable indicating if current Option was already used or not */
145 * Default constructor.
147 * @param name Name of the current Option
148 * @param description Description of current Option
149 * @param function Function used to handle current Option.
151 Option(const std::string & name, const std::string & description, void (*function)(void))
152 : Parameter(description), Callable<void>(function), name(name) {
158 virtual void handle() {
163 * Method used for checking if Option understands given user value.
164 * If so, current Option is flagged as used and no more checks against
165 * it will be done in future.
167 * Passed value should be in form of:
170 * @param argv command line value against which test will be made.
171 * User value should be in format: OptionName.
173 * @return If passed argv succesfully detected OptionName returns true
174 * and Option is set as used one. Otherwise returns false and can be
175 * used to check against next value.
177 virtual bool understand(const std::string & argv) {
188 #endif /* __COMMAND_OPTION_H */