[go: up one dir, main page]

Menu

[f7210c]: / lib / common / cmdline.cpp  Maximize  Restore  History

Download this file

354 lines (298 with data), 10.5 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/***************************************************************************
* lib/common/cmdline.cpp
*
* Part of the STXXL. See http://stxxl.sourceforge.net
*
* Copyright (C) 2013 Timo Bingmann <tb@panthema.net>
*
* Distributed under the Boost Software License, Version 1.0.
* (See accompanying file LICENSE_1_0.txt or copy at
* http://www.boost.org/LICENSE_1_0.txt)
**************************************************************************/
#include <stxxl/bits/common/cmdline.h>
#include <stxxl/bits/common/utils.h>
#include <stxxl/bits/namespace.h>
#include <iomanip>
#include <cstring>
STXXL_BEGIN_NAMESPACE
void cmdline_parser::output_wrap(std::ostream& os, const std::string& text, size_t wraplen,
size_t indent_first, size_t indent_rest,
size_t current, size_t indent_newline)
{
std::string::size_type t = 0;
size_t indent = indent_first;
while (t != text.size())
{
std::string::size_type to = t, lspace = t;
// scan forward in text until we hit a newline or wrap point
while (to != text.size() &&
to + current + indent < t + wraplen &&
text[to] != '\n')
{
if (text[to] == ' ') lspace = to;
++to;
}
// go back to last space
if (to != text.size() && text[to] != '\n' &&
lspace != t) to = lspace + 1;
// output line
os << std::string(indent, ' ')
<< text.substr(t, to - t) << std::endl;
current = 0;
indent = indent_rest;
// skip over last newline
if (to != text.size() && text[to] == '\n') {
indent = indent_newline;
++to;
}
t = to;
}
}
void cmdline_parser::print_usage(std::ostream& os)
{
std::ios state(NULL);
state.copyfmt(os);
os << "Usage: " << m_progname
<< (m_optlist.size() ? " [options]" : "");
for (arglist_type::const_iterator it = m_paramlist.begin();
it != m_paramlist.end(); ++it)
{
const argument* arg = *it;
os << (arg->m_required ? " <" : " [")
<< arg->m_longkey
<< (arg->m_repeated ? " ..." : "")
<< (arg->m_required ? '>' : ']');
}
os << std::endl;
if (m_description.size())
{
os << std::endl;
output_wrap(os, m_description, m_linewrap);
}
if (m_author.size())
{
os << "Author: " << m_author << std::endl;
}
if (m_description.size() || m_author.size())
os << std::endl;
if (m_paramlist.size())
{
os << "Parameters:" << std::endl;
for (arglist_type::const_iterator it = m_paramlist.begin();
it != m_paramlist.end(); ++it)
{
const argument* arg = *it;
os << " " << std::setw(m_param_maxlong) << std::left << arg->param_text();
output_wrap(os, arg->m_desc, m_linewrap,
0, m_param_maxlong + 2, m_param_maxlong + 2, 8);
}
}
if (m_optlist.size())
{
os << "Options:" << std::endl;
for (arglist_type::const_iterator it = m_optlist.begin();
it != m_optlist.end(); ++it)
{
const argument* arg = *it;
os << " " << std::setw(m_opt_maxlong) << std::left << arg->option_text();
output_wrap(os, arg->m_desc, m_linewrap,
0, m_opt_maxlong + 2, m_opt_maxlong + 2, 8);
}
}
os.copyfmt(state);
}
void cmdline_parser::print_option_error(int argc, const char* const* argv, const argument* arg,
std::ostream& os)
{
os << "Error: Argument ";
if (argc != 0)
os << '"' << argv[0] << '"';
os << " for " << arg->type_name() << " option " << arg->option_text()
<< (argc == 0 ? " is missing!" : " is invalid!")
<< std::endl << std::endl;
print_usage(os);
}
void cmdline_parser::print_param_error(int argc, const char* const* argv, const argument* arg,
std::ostream& os)
{
os << "Error: Argument ";
if (argc != 0)
os << '"' << argv[0] << '"';
os << " for " << arg->type_name() << " parameter " << arg->param_text()
<< (argc == 0 ? " is missing!" : " is invalid!")
<< std::endl << std::endl;
print_usage(os);
}
bool cmdline_parser::process(int argc, const char* const* argv, std::ostream& os)
{
m_progname = argv[0];
--argc, ++argv;
// search for help string and output help
for (int i = 0; i < argc; ++i)
{
if (strcmp(argv[i], "-h") == 0 ||
strcmp(argv[i], "--help") == 0)
{
print_usage(os);
return false;
}
}
arglist_type::iterator argi = m_paramlist.begin(); // current argument in m_paramlist
bool end_optlist = false;
while (argc != 0)
{
const char* arg = argv[0];
if (arg[0] == '-' && !end_optlist)
{
// option, advance to argument
--argc, ++argv;
if (arg[1] == '-')
{
if (arg[2] == '-') {
end_optlist = true;
}
else {
// long option
arglist_type::const_iterator oi = m_optlist.begin();
for ( ; oi != m_optlist.end(); ++oi)
{
if ((arg + 2) == (*oi)->m_longkey)
{
if (!(*oi)->process(argc, argv))
{
print_option_error(argc, argv, *oi, os);
return false;
}
else if (m_verbose_process)
{
os << "Option " << (*oi)->option_text() << " set to ";
(*oi)->print_value(os);
os << '.' << std::endl;
}
break;
}
}
if (oi == m_optlist.end())
{
os << "Error: Unknown option \"" << arg << "\"." << std::endl << std::endl;
print_usage(os);
return false;
}
}
}
else
{
// short option
if (arg[1] == 0) {
os << "Invalid option \"" << arg << '"' << std::endl;
}
else {
arglist_type::const_iterator oi = m_optlist.begin();
for ( ; oi != m_optlist.end(); ++oi)
{
if (arg[1] == (*oi)->m_key)
{
if (!(*oi)->process(argc, argv))
{
print_option_error(argc, argv, *oi, os);
return false;
}
else if (m_verbose_process)
{
os << "Option " << (*oi)->option_text() << " set to ";
(*oi)->print_value(os);
os << '.' << std::endl;
}
break;
}
}
if (oi == m_optlist.end())
{
os << "Error: Unknown option \"" << arg << "\"." << std::endl << std::endl;
print_usage(os);
return false;
}
}
}
}
else
{
if (argi != m_paramlist.end())
{
if (!(*argi)->process(argc, argv))
{
print_param_error(argc, argv, *argi, os);
return false;
}
else if (m_verbose_process)
{
os << "Parameter " << (*argi)->param_text() << " set to ";
(*argi)->print_value(os);
os << '.' << std::endl;
}
(*argi)->m_found = true;
if (!(*argi)->m_repeated)
++argi;
}
else
{
os << "Error: Unexpected extra argument \"" << argv[0] << "\"."
<< std::endl << std::endl;
--argc, ++argv;
print_usage(os);
return false;
}
}
}
bool good = true;
for (arglist_type::const_iterator it = m_paramlist.begin();
it != m_paramlist.end(); ++it)
{
if ((*it)->m_required && !(*it)->m_found) {
os << "Error: Argument for parameter "
<< (*it)->m_longkey << " is required!" << std::endl;
good = false;
}
}
if (!good) {
os << std::endl;
print_usage(os);
}
return good;
}
void cmdline_parser::print_result(std::ostream& os)
{
std::ios state(NULL);
state.copyfmt(os);
size_t maxlong = STXXL_MAX(m_param_maxlong, m_opt_maxlong);
if (m_paramlist.size())
{
os << "Parameters:" << std::endl;
for (arglist_type::const_iterator it = m_paramlist.begin();
it != m_paramlist.end(); ++it)
{
const argument* arg = *it;
os << " " << std::setw(maxlong) << std::left << arg->param_text();
std::string typestr = "(" + std::string(arg->type_name()) + ")";
os << std::setw(m_maxtypename + 4) << typestr;
arg->print_value(os);
os << std::endl;
}
}
if (m_optlist.size())
{
os << "Options:" << std::endl;
for (arglist_type::const_iterator it = m_optlist.begin();
it != m_optlist.end(); ++it)
{
const argument* arg = *it;
os << " " << std::setw(maxlong) << std::left << arg->option_text();
std::string typestr = "(" + std::string(arg->type_name()) + ")";
os << std::setw(m_maxtypename + 4) << std::left << typestr;
arg->print_value(os);
os << std::endl;
}
}
os.copyfmt(state);
}
STXXL_END_NAMESPACE