Minimac4
Loading...
Searching...
No Matches
getopt_wrapper.hpp
Go to the documentation of this file.
1#ifndef GETOPT_WRAPPER_HPP
2#define GETOPT_WRAPPER_HPP
3
4#include <cstdint>
5#include <string>
6#include <cstring>
7#include <vector>
8#include <iostream>
9#include <getopt.h>
10
30{
31public:
57 struct option_with_desc : public ::option
58 {
62 const char* description;
63
73 option_with_desc(const char* _name, int _has_arg, int* _flag, int _val, const char* _description)
74 {
75 name = _name;
76 has_arg = _has_arg;
77 flag = _flag;
78 val = _val;
79 description = _description;
80 }
81 };
82protected:
90 std::vector<option> long_options_;
91
99 std::vector<option_with_desc> opts_;
100
107 std::string usage_str_;
108
116 std::string short_opt_string_;
117
124 std::size_t max_long_opt_length_ = 0;
125public:
151 getopt_wrapper(std::string usage_str, std::vector<option_with_desc> long_opts) :
152 usage_str_(std::move(usage_str)),
153 opts_(std::move(long_opts))
154 {
155
156 long_options_.resize(opts_.size() + 1, {0, 0, 0, 0});
157 auto lit = long_options_.begin();
158 for (auto it = opts_.begin(); it != opts_.end(); ++it)
159 {
160 *(lit++) = *it;
161 max_long_opt_length_ = std::max(max_long_opt_length_, it->name ? std::strlen(it->name) : 0);
162 if (it->val)
163 {
164 short_opt_string_ += (char)it->val;
165 if (it->has_arg == required_argument)
166 short_opt_string_ += ':';
167 }
168 }
169 }
170
200 void print_usage(std::ostream& os)
201 {
202 os << usage_str_ << '\n';
203 os << '\n';
204 for (auto it = opts_.begin(); it != opts_.end(); ++it)
205 {
206 if (!it->description)
207 continue;
208
209 if (std::isprint(it->val))
210 {
211 if (it->name)
212 os << " -" << (char)it->val << ", ";
213 else
214 os << " -" << (char)it->val << " ";
215 }
216 else
217 os << " ";
218
219 std::size_t n_spaces = 2;
220 if (it->name)
221 os << "--" << it->name;
222 else
223 n_spaces += 2;
224
225 n_spaces += max_long_opt_length_ - std::strlen(it->name);
226 for (std::size_t i = 0; i < n_spaces; ++i)
227 os.put(' ');
228 os << it->description << '\n';
229 }
230
231 os << std::flush;
232 }
233};
234
235#endif // GETOPT_WRAPPER_HPP
std::string short_opt_string_
Concatenated short option string.
Definition getopt_wrapper.hpp:116
getopt_wrapper(std::string usage_str, std::vector< option_with_desc > long_opts)
Constructs a getopt_wrapper object for parsing command-line options.
Definition getopt_wrapper.hpp:151
std::size_t max_long_opt_length_
Maximum long option length.
Definition getopt_wrapper.hpp:124
std::vector< option > long_options_
Storage of long options for getopt parsing.
Definition getopt_wrapper.hpp:90
void print_usage(std::ostream &os)
Prints the program usage and all available options to a stream.
Definition getopt_wrapper.hpp:200
std::string usage_str_
Program usage/help string.
Definition getopt_wrapper.hpp:107
std::vector< option_with_desc > opts_
Original option definitions with descriptions.
Definition getopt_wrapper.hpp:99
option_with_desc(const char *_name, int _has_arg, int *_flag, int _val, const char *_description)
Construct an option with description.
Definition getopt_wrapper.hpp:73
const char * description
Description of the command-line option for help output.
Definition getopt_wrapper.hpp:62