MVE - Multi-View Environment mve-devel
Loading...
Searching...
No Matches
arguments.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2015, Simon Fuhrmann
3 * TU Darmstadt - Graphics, Capture and Massively Parallel Computing
4 * All rights reserved.
5 *
6 * This software may be modified and distributed under the terms
7 * of the BSD 3-Clause license. See the LICENSE.txt file for details.
8 */
9
10#ifndef MVE_ARGUMENTS_HEADER
11#define MVE_ARGUMENTS_HEADER
12
13#include <ostream>
14#include <vector>
15#include <string>
16#include <stdexcept>
17
18#include "util/strings.h"
19#include "util/defines.h"
20#include "util/exception.h"
21
23
28{
29 char sopt;
30 std::string lopt;
31 std::string desc;
32 bool argument;
33};
34
39{
40 ArgOption const* opt;
41 std::string arg;
42
44 template <typename T>
45 T get_arg (void) const;
46};
47
76{
77public:
78 Arguments (void);
79
81 void set_usage (std::string const& str);
82
84 void set_usage (char const* argv0, std::string const& usage);
85
87 void set_description (std::string const& str);
88
93 void set_description_word_wrap (int width);
94
99 void set_helptext_indent (int indent);
100
105 void set_nonopt_maxnum (std::size_t limit);
106
111 void set_nonopt_minnum (std::size_t limit);
112
118 void set_exit_on_error (bool exit);
119
121 void add_option (char shortname, std::string const& longname,
122 bool has_argument, std::string const& description = "");
123
125 void parse (std::vector<std::string> const& args);
127 void parse (int argc, char const* const* argv);
128
134 ArgResult const* next_result (void);
135
141 ArgResult const* next_option (void);
142
144 std::string get_nth_nonopt (std::size_t index);
145
147 template <typename T>
148 T get_nth_nonopt_as (std::size_t index);
149
151 void get_ids_from_string (std::string const& str, std::vector<int>* ids);
152
154 void generate_helptext (std::ostream& stream) const;
155
156private:
157 void parse_intern (std::vector<std::string> const& args);
158 void parse_long_opt (std::string const& tok);
159 bool parse_short_opt (std::string const& tok1, std::string const& tok2);
160 ArgOption const* find_opt (char sopt);
161 ArgOption const* find_opt (std::string const& lopt);
162
163private:
164 /* Settings. */
165 std::size_t nonopt_min;
166 std::size_t nonopt_max;
167 bool auto_exit;
168 std::vector<ArgOption> options;
169 std::string usage_str;
170 std::string descr_str;
171 int helptext_indent;
172 int descrtext_width;
173
174 /* Parse result. */
175 std::vector<ArgResult> results;
176 std::string command_name;
177
178 /* Iterator. */
179 std::size_t cur_result;
180};
181
182/* ---------------------------------------------------------------- */
183
184template <typename T>
185inline T
186ArgResult::get_arg (void) const
187{
188 return string::convert<T>(this->arg);
189}
190
191template <>
192inline std::string
193ArgResult::get_arg (void) const
194{
195 return this->arg;
196}
197
198inline void
199Arguments::set_usage (std::string const& str)
200{
201 this->usage_str = str;
202}
203
204inline void
205Arguments::set_description (std::string const& str)
206{
207 this->descr_str = str;
208}
209
210inline void
211Arguments::set_description_word_wrap (int width)
212{
213 this->descrtext_width = width;
214}
215
216inline void
217Arguments::set_helptext_indent (int indent)
218{
219 this->helptext_indent = indent;
220}
221
222inline void
223Arguments::set_nonopt_maxnum (std::size_t limit)
224{
225 this->nonopt_max = limit;
226}
227
228inline void
229Arguments::set_nonopt_minnum (std::size_t limit)
230{
231 this->nonopt_min = limit;
232}
233
234inline void
235Arguments::set_exit_on_error (bool exit)
236{
237 this->auto_exit = exit;
238}
239
240template <typename T>
241T
242Arguments::get_nth_nonopt_as (std::size_t index)
243{
244 std::string str(this->get_nth_nonopt(index));
245 if (str.empty())
246 throw std::runtime_error("No such arguemnt");
247 return util::string::convert<T>(str);
248}
249
251
252#endif /* MVE_ARGUMENTS_HEADER */
Argument class that provides a parser and convenient access for command line arguments as used by GNU...
Definition arguments.h:76
A single argument option.
Definition arguments.h:28
std::string lopt
Long option name.
Definition arguments.h:30
std::string desc
Description.
Definition arguments.h:31
bool argument
Requires argument?
Definition arguments.h:32
char sopt
Short option name.
Definition arguments.h:29
An argument which can either be an option or a non-option.
Definition arguments.h:39
ArgOption const * opt
Null for non-options.
Definition arguments.h:40
std::string arg
Empty for options without arguments.
Definition arguments.h:41
#define UTIL_NAMESPACE_BEGIN
Definition defines.h:13
#define UTIL_NAMESPACE_END
Definition defines.h:14