Add Possibility to set MultiValue Parameters.
[command.git] / include / option.h
1 #ifndef __COMMAND_OPTION_H
2 #define __COMMAND_OPTION_H
3
4 #include <string>
5 #include <sstream>
6 #include <stdexcept>
7
8 #include "parameter.h"
9 #include "exception/missingOptionValue.h"
10 #include "exception/optionFailedConversion.h"
11
12 namespace command {
13     /**
14      * Class responsible for handling commandline options.
15      * Options are named parameters of program.
16      *
17      * Example:
18      *  - ./myprog OptionName=OptionValue
19      *  - ./myprog -f=/some/file
20      *  - ./myprog --level=15
21      */
22     template<typename ParameterType>
23     class Option
24         : public Parameter, public Callable<ParameterType>  {
25     public:
26         typedef std::string OptionName;
27     protected:
28         /**
29          * Current Option name
30          */
31         const OptionName name;
32
33         /**
34          * Current Option value
35          */
36         ParameterType value;
37
38     public:
39         /**
40          * Default constructor.
41          *
42          * @param name Name of the current Option
43          * @param description Description of current Option
44          * @param function Function used to handle current Option.
45          */
46         Option(const std::string & name, const std::string & description, void (*function)(ParameterType))
47             : Parameter(description), Callable<ParameterType>(function), name(name) {
48         }
49
50         /**
51          *
52          */
53         virtual ~Option() { }
54
55         /**
56          * \inheritdoc
57          */
58         virtual void handle() {
59             this->call(value);
60             used = true;
61         }
62
63         /**
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.
67          *
68          * Passed value should be in form of:
69          *      OptionName=OptionValue
70          *
71          * If no equal sign is after OptionName part,
72          * std::invalid_argument exception with appropriate message is thrown
73          *
74          * If conversion of OptionValue part to ParameterType failed,
75          * std::invalid_argument exception with appropriate message is thrown
76          *
77          * @param argv command line value against which test will be made.
78          *  User value should be in format: OptionName=OptionValue.
79          *
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.
84          *
85          * @throw MissingOptionValue when OptionValue part is missing after
86          *  equal sign
87          * @throw OptionFailedConversion when OptionValue part failed conversion
88          *  to ParameterType
89          */
90         virtual bool understand(const std::string & argv) {
91
92             if (argv.find(name) == 0) {
93                 std::size_t pos = argv.find("=");
94
95                 if (pos != name.size()) {
96                     throw MissingOptionValue("Option: " + name + " requires value but no one has been provided");
97                 }
98
99                 std::stringstream ss;
100                 ss << argv.substr(pos + 1);
101                 ss >> value;
102
103                 if (ss.fail()) {
104                     throw OptionFailedConversion("Value for option: " + name + " failed conversion to the required type");
105                 }
106
107                 return true;
108             }
109             return false;
110         }
111     };
112
113     /**
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
118      * is used.
119      *
120      * Example:
121      *  ./myprog OptionName
122      *  ./myprog -h
123      *  ./myprog --help
124      */
125     template<>
126     class Option<void>
127         : public Parameter, public Callable<void>  {
128     public:
129         typedef std::string OptionName;
130     protected:
131         /**
132          * Current Option name
133          */
134         const OptionName name;
135     public:
136         /**
137          * Default constructor.
138          *
139          * @param name Name of the current Option
140          * @param description Description of current Option
141          * @param function Function used to handle current Option.
142          */
143         Option(const std::string & name, const std::string & description, void (*function)(void))
144             : Parameter(description), Callable<void>(function), name(name) {
145         }
146
147         /**
148          *
149          */
150         virtual void handle() {
151             this->call();
152             used = true;
153         }
154
155         /**
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.
159          *
160          * Passed value should be in form of:
161          *      OptionName
162          *
163          * @param argv command line value against which test will be made.
164          *  User value should be in format: OptionName.
165          *
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.
169          */
170         virtual bool understand(const std::string & argv) {
171             if (argv == name) {
172                 return true;
173             }
174             return false;
175         }
176     };
177 }
178
179 #endif /* __COMMAND_OPTION_H */