comparison modules/options/options.hpp @ 553:2d1a04e8240a

Options: rename to options.hpp, style
author David Demelier <markand@malikania.fr>
date Wed, 15 Jun 2016 13:45:44 +0200
parents
children 14ac7c28c0ea
comparison
equal deleted inserted replaced
552:c8b6ce961af0 553:2d1a04e8240a
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 OPTIONS_HPP
20 #define OPTIONS_HPP
21
22 /**
23 * \file options.h
24 * \brief Basic Unix options parser.
25 */
26
27 #include <exception>
28 #include <map>
29 #include <string>
30 #include <utility>
31 #include <vector>
32
33 /**
34 * Namespace for options parsing.
35 */
36 namespace option {
37
38 /**
39 * \brief This exception is thrown when an invalid option has been found.
40 */
41 class InvalidOption : public std::exception {
42 private:
43 std::string message;
44
45 public:
46 /**
47 * The invalid option given.
48 */
49 std::string argument;
50
51 /**
52 * Construct the exception.
53 *
54 * \param arg the argument missing
55 */
56 inline InvalidOption(std::string arg)
57 : argument(std::move(arg))
58 {
59 message = std::string("invalid option: ") + argument;
60 }
61
62 /**
63 * Get the error message.
64 *
65 * \return the error message
66 */
67 const char *what() const noexcept override
68 {
69 return message.c_str();
70 }
71 };
72
73 /**
74 * \brief This exception is thrown when an option requires a value and no value has been given.
75 */
76 class MissingValue : public std::exception {
77 private:
78 std::string m_message;
79 std::string m_option;
80
81 public:
82 /**
83 * Construct the exception.
84 *
85 * \param option the option that requires a value
86 */
87 inline MissingValue(std::string option)
88 : m_option(std::move(option))
89 {
90 m_message = std::string("missing argument for: ") + m_option;
91 }
92
93 /**
94 * Get the options that requires a value.
95 *
96 * \return the option name
97 */
98 inline const std::string &option() const noexcept
99 {
100 return m_option;
101 }
102
103 /**
104 * Get the error message.
105 *
106 * \return the error message
107 */
108 const char *what() const noexcept override
109 {
110 return m_message.c_str();
111 }
112 };
113
114 /**
115 * Packed multimap of options.
116 */
117 using Result = std::multimap<std::string, std::string>;
118
119 /**
120 * Define the allowed options.
121 */
122 using Options = std::map<std::string, bool>;
123
124 /**
125 * Extract the command line options and return a result.
126 *
127 * \param args the arguments
128 * \param definition
129 * \warning the arguments vector is modified in place to remove parsed options
130 * \throw MissingValue
131 * \throw InvalidOption
132 */
133 Result read(std::vector<std::string> &args, const Options &definition);
134
135 /**
136 * Overloaded function for usage with main() arguments.
137 *
138 * \param argc the number of arguments
139 * \param argv the argument vector
140 * \param definition
141 * \note don't forget to remove the first argv[0] argument
142 * \warning the argc and argv are modified in place to remove parsed options
143 * \throw MissingValue
144 * \throw InvalidOption
145 */
146 Result read(int &argc, char **&argv, const Options &definition);
147
148 } // !option
149
150 #endif // !OPTIONS_HPP