Added mechanism to understand passed Option without value.
[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
10 namespace command {
11     /**
12      * Class responsible for handling commandline options.
13      * Options are non-required, named parameters of program.
14      *
15      * Example:
16      *  ./myprog OptionName=OptionValue
17      *  ./myprog -f=/some/file
18      *  ./myprog --level=15
19      */
20     template<typename OptionType>
21     class Option
22         : public Parameter, public Callable<OptionType>  {
23     public:
24         typedef std::string OptionName;
25     protected:
26         /**
27          * Current Option name
28          */
29         OptionName name;
30
31         /**
32          * Current Option value
33          */
34         OptionType value;
35
36         /** Variable indicating if current Option was already used or not */
37         bool used = false;
38
39     public:
40         /**
41          * Default constructor.
42          *
43          * @param name Name of the current Option
44          * @param description Description of current Option
45          * @param function Function used to handle current Option.
46          */
47         Option(std::string name, const std::string & description, void (*function)(OptionType))
48             : Parameter(description), Callable<OptionType>(function), name(name) {
49         }
50
51         /**
52          *
53          */
54         virtual ~Option() { }
55
56         /**
57          *
58          */
59         virtual void handle() {
60             this->call(value);
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 OptionType 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 OptionType, 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 std::invalid_argument when OptionName part has no equal sign
86          *  after itself
87          * @throw std::invalid_argument when OptionValue part failed conversion
88          *  to OptionType
89          */
90         virtual bool understand(const std::string & argv) {
91             if ((!used) &&
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");
96                 }
97
98                 std::stringstream ss;
99
100                 ss << argv.substr(pos + 1);
101                 ss >> value;
102
103                 if (ss.fail()) {
104                     throw std::invalid_argument("Value for option: " + name + " failed conversion to the required type");
105                 }
106
107                 used = true;
108                 return true;
109             }
110             return false;
111         }
112     };
113
114     /**
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
119      * is used.
120      *
121      * Example:
122      *  ./myprog OptionName
123      *  ./myprog -h
124      *  ./myprog --help
125      */
126     template<>
127     class Option<void>
128         : public Parameter, public Callable<void>  {
129     public:
130         typedef std::string OptionName;
131     protected:
132         /**
133          * Current Option name
134          */
135         OptionName name;
136
137         /** Variable indicating if current Option was already used or not */
138         bool used = false;
139
140     public:
141         /**
142          * Default constructor.
143          *
144          * @param name Name of the current Option
145          * @param description Description of current Option
146          * @param function Function used to handle current Option.
147          */
148         Option(std::string name, const std::string & description, void (*function)(void))
149             : Parameter(description), Callable<void>(function), name(name) {
150         }
151
152         /**
153          *
154          */
155         virtual ~Option() { }
156
157         /**
158          *
159          */
160         virtual void handle() {
161             this->call();
162         }
163
164         /**
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.
168          *
169          * Passed value should be in form of:
170          *      OptionName
171          *
172          * @param argv command line value against which test will be made.
173          *  User value should be in format: OptionName.
174          *
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.
178          */
179         virtual bool understand(const std::string & argv) {
180             if ((!used) &&
181                 (argv == name)) {
182                 used = true;
183                 return true;
184             }
185             return false;
186         }
187     };
188 }
189
190 #endif /* __COMMAND_OPTION_H */