1 #ifndef __COMMAND_OPTION_H
2 #define __COMMAND_OPTION_H
12 * Class responsible for handling commandline options.
13 * Options are named parameters of program.
16 * - ./myprog OptionName=OptionValue
17 * - ./myprog -f=/some/file
18 * - ./myprog --level=15
20 template<typename ParameterType>
22 : public Parameter, public Callable<ParameterType> {
24 typedef std::string OptionName;
29 const OptionName name;
32 * Current Option value
38 * Default constructor.
40 * @param name Name of the current Option
41 * @param description Description of current Option
42 * @param function Function used to handle current Option.
44 Option(const std::string & name, const std::string & description, void (*function)(ParameterType))
45 : Parameter(description), Callable<ParameterType>(function), name(name) {
56 virtual void handle() {
61 * Method used for checking if Option understands given user value.
62 * If so current Option is flagged as used and no more checks against
63 * it will be done in future.
65 * Passed value should be in form of:
66 * OptionName=OptionValue
68 * If no equal sign is after OptionName part,
69 * std::invalid_argument exception with appropriate message is thrown
71 * If conversion of OptionValue part to ParameterType failed,
72 * std::invalid_argument exception with appropriate message is thrown
74 * @param argv command line value against which test will be made.
75 * User value should be in format: OptionName=OptionValue.
77 * @return If passed argv succesfully detected OptionName part as a
78 * current option and its OptionValue part has been succesfully
79 * converted to ParameterType, returns true and Option is set as used one.
80 * Otherwise returns false and can be used to check against next value.
82 * @throw std::invalid_argument when OptionName part has no equal sign
84 * @throw std::invalid_argument when OptionValue part failed conversion
87 virtual bool understand(const std::string & argv)
88 throw(std::invalid_argument) {
90 if ((!isUsed()) && (argv.find(name) == 0)) {
91 std::size_t pos = argv.find("=");
93 if (pos != name.size()) {
94 throw std::invalid_argument("Option: " + name + " requires value but no one has been provided");
98 ss << argv.substr(pos + 1);
99 ss >> value;// memory leak? when uncommented and exception is
100 // thrown, valgrind shows e.g.:
101 // possibly lost: 380 bytes in 7 blocks
104 throw std::invalid_argument("Value for option: " + name + " failed conversion to the required type");
115 * Template class responsible for handling commandline options.
116 * Options are non-required, named parameters of program.
117 * This template specialization allows Options to work like switches.
118 * It means that just named parameter is needed to invoke command. No value
122 * ./myprog OptionName
128 : public Parameter, public Callable<void> {
130 typedef std::string OptionName;
133 * Current Option name
135 const OptionName name;
138 * Default constructor.
140 * @param name Name of the current Option
141 * @param description Description of current Option
142 * @param function Function used to handle current Option.
144 Option(const std::string & name, const std::string & description, void (*function)(void))
145 : Parameter(description), Callable<void>(function), name(name) {
151 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) {
181 #endif /* __COMMAND_OPTION_H */