summaryrefslogtreecommitdiffstats
path: root/task1/getoptwrapper.h
diff options
context:
space:
mode:
Diffstat (limited to 'task1/getoptwrapper.h')
-rw-r--r--task1/getoptwrapper.h231
1 files changed, 231 insertions, 0 deletions
diff --git a/task1/getoptwrapper.h b/task1/getoptwrapper.h
new file mode 100644
index 0000000..21270f7
--- /dev/null
+++ b/task1/getoptwrapper.h
@@ -0,0 +1,231 @@
1/**
2 * $Id: getoptwrapper.h 2 2009-10-31 02:48:23Z l0728348 $
3 *
4 * Copyright 2009
5 *
6 * @author Manuel Mausz (0728348)
7 * @brief Wraps around getopt_long() using two classes
8 * class CommandOption represents a valid commandline option
9 * class CommandOptionParse calls getopt_long
10 * and generates usage message
11 */
12
13#ifndef GETOPTWRAPPER_H
14#define GETOPTWRAPPER_H
15
16#include <string>
17#include <list>
18#include <vector>
19#if !defined(_GNU_SOURCE)
20# define _GNU_SOURCE
21#endif
22#include <getopt.h>
23
24/* forward declarations */
25class CommandOption;
26class CommandOptionParse;
27
28/** default list containing commandline options */
29extern std::list<CommandOption *> cmdoptionlist;
30
31/**
32 * every instance of CommandOption represents a valid commandline option
33 * use CommandOptionParse to parse them
34 */
35class CommandOption
36{
37 /* be friend with CommandOptionParse so it can use our privates */
38 friend class CommandOptionParse;
39
40 public:
41 /** commandline option argument */
42 enum Type
43 {
44 noArg = 0, /* no argument */
45 needArg = 1, /* argument required */
46 optArg = 2, /* argument optional */
47 };
48
49 /**
50 * @brief Default ctor. Adds commandline option to commandline list
51 * @param longopt long commandline option. empty string to omit
52 * @param shortopt short commandline option. 0 to omit
53 * @param description description. will be printed in usage
54 * @param type type of argument
55 * @param cmdlist list to add this commandline option
56 */
57 CommandOption(const std::string& longopt, const char shortopt, const std::string& description,
58 const Type& type, std::list<CommandOption *>& cmdlist = cmdoptionlist)
59 : m_longopt(longopt), m_shortopt(shortopt), m_description(description),
60 m_type(type), m_count(0), m_value("")
61 {
62 if (!m_longopt.empty() || m_shortopt != 0)
63 cmdlist.push_back(this);
64 }
65
66 /**
67 * @brief Default dtor
68 */
69 virtual ~CommandOption()
70 {}
71
72 /**
73 * @brief generate a usage string/valid syntax
74 * for this commandline option
75 * @return usage string for this commandline option
76 */
77 const std::string printParameters();
78
79 /**
80 * @brief get number of commandline matches found
81 * @return number of commandline matches found
82 */
83 unsigned count()
84 {
85 return m_count;
86 }
87
88 /**
89 * @brief get number of commandline matches found
90 * @return number of commandline matches found
91 */
92 operator unsigned()
93 {
94 return count();
95 }
96
97 /**
98 * @brief returns first valid commandline option argument
99 * if the commandline option is found a second time,
100 * the argument is omitted
101 * @return first valid argument for this commandline option
102 */
103 const std::string value()
104 {
105 return m_value;
106 }
107
108 /**
109 * @brief returns first valid commandline option argument
110 * if the commandline option is found a second time,
111 * the argument is omitted
112 * @return first valid argument for this commandline option
113 */
114 operator std::string()
115 {
116 return value();
117 }
118
119 private:
120 /**
121 * @brief gets called by CommandOptionParse if the commandline option
122 * is found. Parses and stores the argument. Only the first call
123 * will be stored.
124 * @param value commandline option argument
125 */
126 void foundOption(const char *value);
127
128 std::string m_longopt;
129 char m_shortopt;
130 std::string m_description;
131 Type m_type;
132 unsigned m_count;
133 std::string m_value;
134};
135
136/*----------------------------------------------------------------------------*/
137
138/**
139 * parses argc/argv using getopt_long and a list of instances of CommandOption
140 * also generates usage message
141 */
142class CommandOptionParse
143{
144 public:
145 /** list of pointers of CommandOption */
146 typedef std::list<CommandOption *> CommandOptionList;
147
148 /**
149 * @brief Default ctor.
150 * @param argc argc of main
151 * @param argv argv of main
152 * @param description will be printed in usage before CommandOption list
153 * @param cmdlist list to add this commandline option
154 */
155 CommandOptionParse(int argc, char* argv[], const std::string& description,
156 std::list<CommandOption *>& cmdlist = cmdoptionlist)
157 : m_argc(argc), m_argv(argv), m_description(description), m_cmdlist(cmdlist),
158 m_longopts(NULL)
159 {}
160
161 /**
162 * @brief Default dtor.
163 */
164 virtual ~CommandOptionParse()
165 {
166 if (m_longopts != NULL)
167 delete[] m_longopts;
168 }
169
170 /**
171 * @brief adds valid synopsis to usage message
172 * @param synopsis synopsis
173 */
174 void addSynopsis(const std::string& synopsis)
175 {
176 m_synopsis.push_back(synopsis);
177 }
178
179 /**
180 * @brief starts parsing argc/argv
181 * @return true on success. false otherwise
182 */
183 bool parse();
184
185 /**
186 * @brief generates usage message
187 * @return usage message as string
188 */
189 const std::string usage();
190
191 /**
192 * @brief generates usage message and prints them on ostream
193 * @return reference to ostream
194 */
195 friend std::ostream& operator<<(std::ostream& os, CommandOptionParse& cmdoptions)
196 {
197 os << cmdoptions.usage();
198 return os;
199 }
200
201 /**
202 * @brief directly access a valid/parsed commandline option
203 * using its longopt parameter
204 * @param longopt parameter "longopt" of commandline option
205 * @return reference to commandline option
206 * @throw std::out_of_range if the commandline option cannot be found
207 */
208 CommandOption& operator[](const std::string& longopt);
209
210 /**
211 * @brief directly access a valid/parsed commandline option
212 * using its shortopt parameter
213 * @param shortopt parameter "shortopt" of commandline option
214 * @return reference to commandline option
215 * @throw std::out_of_range if the commandline option cannot be found
216 */
217 CommandOption& operator[](const char shortopt);
218
219 private:
220 int m_argc;
221 char **m_argv;
222 std::string m_description;
223 CommandOptionList m_cmdlist;
224 std::vector<std::string> m_synopsis;
225 std::string m_optstring;
226 struct option *m_longopts;
227};
228
229#endif
230
231/* vim: set et sw=2 ts=2: */