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
18 template<typename OptionType>
20 : public Parameter, public Callable<OptionType> {
22 typedef std::string OptionName;
30 * Current Option value
34 /** Variable indicating if current Option was already used or not */
39 * Default constructor.
41 * @param name Name of the current Option
42 * @param description Description of current Option
43 * @param function Function used to handle current Option.
45 Option(std::string name, const std::string & description, void (*function)(OptionType))
46 : Parameter(description), Callable<OptionType>(function), name(name) {
57 virtual void handle() {
62 * Method used for checking if Option understands given user value.
63 * If so current Option is flagged as used and no more checks against
64 * it will be done in future.
66 * Passed value should be in form of:
67 * OptionName=OptionValue
69 * If no equal sign is after OptionName part,
70 * std::invalid_argument exception with appropriate message is thrown
72 * If conversion of OptionValue part to OptionType failed,
73 * std::invalid_argument exception with appropriate message is thrown
75 * @param argv command line value against which test will be made.
76 * User value should be in format: OptionName=OptionValue.
78 * @return If passed argv succesfully detected OptionName part as a
79 * current option and its OptionValue part has been succesfully
80 * converted to OptionType, returns true and Option is set as used one.
81 * Otherwise returns false and can be used to check against next value.
83 * @throw std::invalid_argument when OptionName part has no equal sign
85 * @throw std::invalid_argument when OptionValue part failed conversion
88 virtual bool understand(const std::string & argv) {
90 (argv.find(name) == 0)) {
91 std::size_t pos = argv.find("=");
92 if (pos != name.size()) {
93 throw std::invalid_argument("Option: " + name + " requires value but no one has been provided");
98 ss << argv.substr(pos + 1);
102 throw std::invalid_argument("Value for option: " + name + " failed conversion to the required type");
113 #endif /* __COMMAND_OPTION_H */