comparison common/options.h @ 0:1158cffe5a5e

Initial import
author David Demelier <markand@malikania.fr>
date Mon, 08 Feb 2016 16:43:14 +0100
parents
children 03068f5ed79d
comparison
equal deleted inserted replaced
-1:000000000000 0:1158cffe5a5e
1 /*
2 * options.h -- parse Unix command line options
3 *
4 * Copyright (c) 2015 David Demelier <markand@malikania.fr>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #ifndef _OPTION_PARSER_H_
20 #define _OPTION_PARSER_H_
21
22 #include <exception>
23 #include <map>
24 #include <string>
25 #include <utility>
26 #include <vector>
27
28 namespace irccd {
29
30 /**
31 * Namespace for options parsing.
32 */
33 namespace parser {
34
35 /**
36 * @class InvalidOption
37 * @brief This exception is thrown when an invalid option has been found.
38 */
39 class InvalidOption : public std::exception {
40 private:
41 std::string message;
42
43 public:
44 /**
45 * The invalid option given.
46 */
47 std::string argument;
48
49 /**
50 * Construct the exception.
51 *
52 * @param arg the argument missing
53 */
54 inline InvalidOption(std::string arg)
55 : argument{std::move(arg)}
56 {
57 message = std::string{"invalid option: "} + argument;
58 }
59
60 /**
61 * Get the error message.
62 *
63 * @return the error message
64 */
65 const char *what() const noexcept override
66 {
67 return message.c_str();
68 }
69 };
70
71 /**
72 * @class MissingValue
73 * @brief This exception is thrown when an option requires a value and no value has been given
74 */
75 class MissingValue : public std::exception {
76 private:
77 std::string message;
78
79 public:
80 /**
81 * The argument that requires a value.
82 */
83 std::string argument;
84
85 /**
86 * Construct the exception.
87 *
88 * @param arg the argument that requires a value
89 */
90 inline MissingValue(std::string arg)
91 : argument{std::move(arg)}
92 {
93 message = std::string{"missing argument for: "} + argument;
94 }
95
96 /**
97 * Get the error message.
98 *
99 * @return the error message
100 */
101 const char *what() const noexcept override
102 {
103 return message.c_str();
104 }
105 };
106
107 /**
108 * Packed multimap of options.
109 */
110 using Result = std::multimap<std::string, std::string>;
111
112 /**
113 * Define the allowed options.
114 */
115 using Options = std::map<std::string, bool>;
116
117 /**
118 * Extract the command line options and return a result.
119 *
120 * @param args the arguments
121 * @param definition
122 * @warning the arguments vector is modified in place to remove parsed options
123 * @throw MissingValue
124 * @throw InvalidOption
125 */
126 Result read(std::vector<std::string> &args, const Options &definition);
127
128 /**
129 * Overloaded function for usage with main() arguments.
130 *
131 * @param argc the number of arguments
132 * @param argv the argument vector
133 * @param definition
134 * @note don't forget to remove the first argv[0] argument
135 * @warning the argc and argv are modified in place to remove parsed options
136 * @throw MissingValue
137 * @throw InvalidOption
138 */
139 Result read(int &argc, char **&argv, const Options &definition);
140
141 } // !parser
142
143 } // !irccd
144
145 #endif // !_OPTION_PARSER_H_