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;
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(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) {
92 (argv.find(name) == 0)) {
93 std::size_t pos = argv.find("=");
94 if (pos != name.size()) {
95 throw std::invalid_argument("Option: " + name + " requires value but no one has been provided");
100 ss << argv.substr(pos + 1);
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
137 /** Variable indicating if current Option was already used or not */
142 * Default constructor.
144 * @param name Name of the current Option
145 * @param description Description of current Option
146 * @param function Function used to handle current Option.
148 Option(std::string name, const std::string & description, void (*function)(void))
149 : Parameter(description), Callable<void>(function), name(name) {
155 virtual ~Option() { }
160 virtual void handle() {
165 * Method used for checking if Option understands given user value.
166 * If so, current Option is flagged as used and no more checks against
167 * it will be done in future.
169 * Passed value should be in form of:
172 * @param argv command line value against which test will be made.
173 * User value should be in format: OptionName.
175 * @return If passed argv succesfully detected OptionName returns true
176 * and Option is set as used one. Otherwise returns false and can be
177 * used to check against next value.
179 virtual bool understand(const std::string & argv) {
190 #endif /* __COMMAND_OPTION_H */