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() {
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 ParameterType 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 ParameterType, returns true and Option is set as used one.
83 * Otherwise returns false and can be used to check against next value.
85 * @throw MissingOptionValue when OptionValue part is missing after
87 * @throw OptionFailedConversion when OptionValue part failed conversion
90 virtual bool understand(const std::string & argv) {
92 if (argv.find(name) == 0) {
93 std::size_t pos = argv.find("=");
95 if (pos != name.size()) {
96 throw MissingOptionValue("Option: " + name + " requires value but no one has been provided");
100 ss << argv.substr(pos + 1);
104 throw OptionFailedConversion("Value for option: " + name + " failed conversion to the required type");
115 virtual unsigned int valuePosition(const std::string & argv) {
116 return argv.find("=");
121 * Template class responsible for handling commandline options.
122 * Options are non-required, named parameters of program.
123 * This template specialization allows Options to work like switches.
124 * It means that just named parameter is needed to invoke command. No value
128 * ./myprog OptionName
134 : public Parameter, public Callable<void> {
136 typedef std::string OptionName;
139 * Current Option name
141 const OptionName name;
144 * Default constructor.
146 * @param name Name of the current Option
147 * @param description Description of current Option
148 * @param function Function used to handle current Option.
150 Option(const std::string & name, const std::string & description, void (*function)(void))
151 : Parameter(description), Callable<void>(function), name(name) {
157 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) {
187 virtual unsigned int valuePosition(const std::string & ) {
188 throw new std::invalid_argument(this->describe() + "is void Option, so it does not have value part");
193 #endif /* __COMMAND_OPTION_H */