22 void cmdline_parser::output_wrap(std::ostream& os,
const std::string& text,
size_t wraplen,
23 size_t indent_first,
size_t indent_rest,
24 size_t current,
size_t indent_newline)
26 std::string::size_type t = 0;
27 size_t indent = indent_first;
28 while (t != text.size())
30 std::string::size_type to = t, lspace = t;
33 while (to != text.size() &&
34 to + current + indent < t + wraplen &&
37 if (text[to] ==
' ') lspace = to;
42 if (to != text.size() && text[to] !=
'\n' &&
43 lspace != t) to = lspace + 1;
46 os << std::string(indent,
' ')
47 << text.substr(t, to - t) << std::endl;
53 if (to != text.size() && text[to] ==
'\n') {
54 indent = indent_newline;
62 void cmdline_parser::print_usage(std::ostream& os)
67 os <<
"Usage: " << m_progname
68 << (m_optlist.size() ?
" [options]" :
"");
70 for (arglist_type::const_iterator it = m_paramlist.begin();
71 it != m_paramlist.end(); ++it)
83 if (m_description.size())
86 output_wrap(os, m_description, m_linewrap);
90 os <<
"Author: " << m_author << std::endl;
93 if (m_description.size() || m_author.size())
96 if (m_paramlist.size())
98 os <<
"Parameters:" << std::endl;
100 for (arglist_type::const_iterator it = m_paramlist.begin();
101 it != m_paramlist.end(); ++it)
105 os <<
" " << std::setw(m_param_maxlong) << std::left
107 output_wrap(os, arg->
m_desc, m_linewrap,
108 0, m_param_maxlong + 2, m_param_maxlong + 2, 8);
112 if (m_optlist.size())
114 os <<
"Options:" << std::endl;
116 for (arglist_type::const_iterator it = m_optlist.begin();
117 it != m_optlist.end(); ++it)
121 os <<
" " << std::setw(m_opt_maxlong) << std::left
123 output_wrap(os, arg->
m_desc, m_linewrap,
124 0, m_opt_maxlong + 2, m_opt_maxlong + 2, 8);
131 void cmdline_parser::print_option_error(
132 int argc,
const char*
const* argv,
const argument* arg,
135 os <<
"Error: Argument ";
137 os <<
'"' << argv[0] <<
'"';
140 << (argc == 0 ?
" is missing!" :
" is invalid!")
141 << std::endl << std::endl;
146 void cmdline_parser::print_param_error(
int argc,
const char*
const* argv,
const argument* arg,
149 os <<
"Error: Argument ";
151 os <<
'"' << argv[0] <<
'"';
154 << (argc == 0 ?
" is missing!" :
" is invalid!")
155 << std::endl << std::endl;
160 bool cmdline_parser::process(
int argc,
const char*
const* argv, std::ostream& os)
162 m_progname = argv[0];
166 for (
int i = 0; i < argc; ++i)
168 if (strcmp(argv[i],
"-h") == 0 ||
169 strcmp(argv[i],
"--help") == 0)
176 arglist_type::iterator argi = m_paramlist.begin();
177 bool end_optlist =
false;
181 const char* arg = argv[0];
183 if (arg[0] ==
'-' && !end_optlist)
194 arglist_type::const_iterator oi = m_optlist.begin();
195 for ( ; oi != m_optlist.end(); ++oi)
197 if ((arg + 2) == (*oi)->m_longkey)
199 if (!(*oi)->process(argc, argv))
201 print_option_error(argc, argv, *oi, os);
204 else if (m_verbose_process)
206 os <<
"Option " << (*oi)->option_text() <<
" set to ";
207 (*oi)->print_value(os);
208 os <<
'.' << std::endl;
213 if (oi == m_optlist.end())
215 os <<
"Error: Unknown option \"" << arg <<
"\"." << std::endl << std::endl;
225 os <<
"Invalid option \"" << arg <<
'"' << std::endl;
228 arglist_type::const_iterator oi = m_optlist.begin();
229 for ( ; oi != m_optlist.end(); ++oi)
231 if (arg[1] == (*oi)->m_key)
233 if (!(*oi)->process(argc, argv))
235 print_option_error(argc, argv, *oi, os);
238 else if (m_verbose_process)
240 os <<
"Option " << (*oi)->option_text() <<
" set to ";
241 (*oi)->print_value(os);
242 os <<
'.' << std::endl;
247 if (oi == m_optlist.end())
249 os <<
"Error: Unknown option \"" << arg <<
"\"." << std::endl << std::endl;
258 if (argi != m_paramlist.end())
260 if (!(*argi)->process(argc, argv))
262 print_param_error(argc, argv, *argi, os);
265 else if (m_verbose_process)
267 os <<
"Parameter " << (*argi)->param_text() <<
" set to ";
268 (*argi)->print_value(os);
269 os <<
'.' << std::endl;
271 (*argi)->m_found =
true;
272 if (!(*argi)->m_repeated)
277 os <<
"Error: Unexpected extra argument \"" << argv[0] <<
"\"."
278 << std::endl << std::endl;
288 for (arglist_type::const_iterator it = m_paramlist.begin();
289 it != m_paramlist.end(); ++it)
291 if ((*it)->m_required && !(*it)->m_found) {
292 os <<
"Error: Argument for parameter "
293 << (*it)->m_longkey <<
" is required!" << std::endl;
306 void cmdline_parser::print_result(std::ostream& os)
308 std::ios
state(NULL);
311 int maxlong =
STXXL_MAX(m_param_maxlong, m_opt_maxlong);
313 if (m_paramlist.size())
315 os <<
"Parameters:" << std::endl;
317 for (arglist_type::const_iterator it = m_paramlist.begin();
318 it != m_paramlist.end(); ++it)
322 os <<
" " << std::setw(maxlong) << std::left << arg->
param_text();
324 std::string typestr =
"(" + std::string(arg->
type_name()) +
")";
325 os << std::setw(m_maxtypename + 4) << typestr;
333 if (m_optlist.size())
335 os <<
"Options:" << std::endl;
337 for (arglist_type::const_iterator it = m_optlist.begin();
338 it != m_optlist.end(); ++it)
342 os <<
" " << std::setw(maxlong) << std::left << arg->
option_text();
344 std::string typestr =
"(" + std::string(arg->
type_name()) +
")";
345 os << std::setw(m_maxtypename + 4) << std::left << typestr;
bool m_required
required, process() fails if the option/parameter is not found.
std::string m_longkey
long option key or name for parameters
virtual const char * type_name() const =0
return formatted type name to user
base class of all options and parameters
std::string option_text() const
return '-s, –longkey [keytype]'
const Type & STXXL_MAX(const Type &a, const Type &b)
bool m_repeated
repeated argument, i.e. std::vector<std::string>
#define STXXL_BEGIN_NAMESPACE
std::string param_text() const
return 'longkey [keytype]'
std::string m_desc
longer description, which will be wrapped
virtual void print_value(std::ostream &os) const =0
format value to ostream
#define STXXL_END_NAMESPACE