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() {
63 * Method used for checking if Option understands given user value.
64 * If so current Option is flagged as used and no more checks against
65 * it will be done in future.
67 * Passed value should be in form of:
68 * OptionName=OptionValue
70 * If no equal sign is after OptionName part,
71 * std::invalid_argument exception with appropriate message is thrown
73 * If conversion of OptionValue part to ParameterType failed,
74 * std::invalid_argument exception with appropriate message is thrown
76 * @param argv command line value against which test will be made.
77 * User value should be in format: OptionName=OptionValue.
79 * @return If passed argv succesfully detected OptionName part as a
80 * current option and its OptionValue part has been succesfully
81 * converted to ParameterType, returns true and Option is set as used one.
82 * Otherwise returns false and can be used to check against next value.
84 * @throw MissingOptionValue when OptionValue part is missing after
86 * @throw OptionFailedConversion when OptionValue part failed conversion
89 virtual bool understand(const std::string & argv) {
91 if ((!isUsed()) && (argv.find(name) == 0)) {
92 std::size_t pos = argv.find("=");
94 if (pos != name.size()) {
95 throw MissingOptionValue("Option: " + name + " requires value but no one has been provided");
99 ss << argv.substr(pos + 1);
103 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() {
155 * Method used for checking if Option understands given user value.
156 * If so, current Option is flagged as used and no more checks against
157 * it will be done in future.
159 * Passed value should be in form of:
162 * @param argv command line value against which test will be made.
163 * User value should be in format: OptionName.
165 * @return If passed argv succesfully detected OptionName returns true
166 * and Option is set as used one. Otherwise returns false and can be
167 * used to check against next value.
169 virtual bool understand(const std::string & argv) {
180 #endif /* __COMMAND_OPTION_H */