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 << arg->
param_text();
 
  106             output_wrap(os, arg->
m_desc, m_linewrap,
 
  107                         0, m_param_maxlong + 2, m_param_maxlong + 2, 8);
 
  111     if (m_optlist.size())
 
  113         os << 
"Options:" << std::endl;
 
  115         for (arglist_type::const_iterator it = m_optlist.begin();
 
  116              it != m_optlist.end(); ++it)
 
  120             os << 
"  " << std::setw(m_opt_maxlong) << std::left << arg->
option_text();
 
  121             output_wrap(os, arg->
m_desc, m_linewrap,
 
  122                         0, m_opt_maxlong + 2, m_opt_maxlong + 2, 8);
 
  129 void cmdline_parser::print_option_error(
int argc, 
const char* 
const* argv, 
const argument* arg,
 
  132     os << 
"Error: Argument ";
 
  134         os << 
'"' << argv[0] << 
'"';
 
  137        << (argc == 0 ? 
" is missing!" : 
" is invalid!")
 
  138        << std::endl << std::endl;
 
  143 void cmdline_parser::print_param_error(
int argc, 
const char* 
const* argv, 
const argument* arg,
 
  146     os << 
"Error: Argument ";
 
  148         os << 
'"' << argv[0] << 
'"';
 
  151        << (argc == 0 ? 
" is missing!" : 
" is invalid!")
 
  152        << std::endl << std::endl;
 
  157 bool cmdline_parser::process(
int argc, 
const char* 
const* argv, std::ostream& os)
 
  159     m_progname = argv[0];
 
  163     for (
int i = 0; i < argc; ++i)
 
  165         if (strcmp(argv[i], 
"-h") == 0 ||
 
  166             strcmp(argv[i], 
"--help") == 0)
 
  173     arglist_type::iterator argi = m_paramlist.begin(); 
 
  174     bool end_optlist = 
false;
 
  178         const char* arg = argv[0];
 
  180         if (arg[0] == 
'-' && !end_optlist)
 
  191                     arglist_type::const_iterator oi = m_optlist.begin();
 
  192                     for ( ; oi != m_optlist.end(); ++oi)
 
  194                         if ((arg + 2) == (*oi)->m_longkey)
 
  196                             if (!(*oi)->process(argc, argv))
 
  198                                 print_option_error(argc, argv, *oi, os);
 
  201                             else if (m_verbose_process)
 
  203                                 os << 
"Option " << (*oi)->option_text() << 
" set to ";
 
  204                                 (*oi)->print_value(os);
 
  205                                 os << 
'.' << std::endl;
 
  210                     if (oi == m_optlist.end())
 
  212                         os << 
"Error: Unknown option \"" << arg << 
"\"." << std::endl << std::endl;
 
  222                     os << 
"Invalid option \"" << arg << 
'"' << std::endl;
 
  225                     arglist_type::const_iterator oi = m_optlist.begin();
 
  226                     for ( ; oi != m_optlist.end(); ++oi)
 
  228                         if (arg[1] == (*oi)->m_key)
 
  230                             if (!(*oi)->process(argc, argv))
 
  232                                 print_option_error(argc, argv, *oi, os);
 
  235                             else if (m_verbose_process)
 
  237                                 os << 
"Option " << (*oi)->option_text() << 
" set to ";
 
  238                                 (*oi)->print_value(os);
 
  239                                 os << 
'.' << std::endl;
 
  244                     if (oi == m_optlist.end())
 
  246                         os << 
"Error: Unknown option \"" << arg << 
"\"." << std::endl << std::endl;
 
  255             if (argi != m_paramlist.end())
 
  257                 if (!(*argi)->process(argc, argv))
 
  259                     print_param_error(argc, argv, *argi, os);
 
  262                 else if (m_verbose_process)
 
  264                     os << 
"Parameter " << (*argi)->param_text() << 
" set to ";
 
  265                     (*argi)->print_value(os);
 
  266                     os << 
'.' << std::endl;
 
  268                 (*argi)->m_found = 
true;
 
  269                 if (!(*argi)->m_repeated)
 
  274                 os << 
"Error: Unexpected extra argument \"" << argv[0] << 
"\"." 
  275                    << std::endl << std::endl;
 
  285     for (arglist_type::const_iterator it = m_paramlist.begin();
 
  286          it != m_paramlist.end(); ++it)
 
  288         if ((*it)->m_required && !(*it)->m_found) {
 
  289             os << 
"Error: Argument for parameter " 
  290                << (*it)->m_longkey << 
" is required!" << std::endl;
 
  303 void cmdline_parser::print_result(std::ostream& os)
 
  305     std::ios 
state(NULL);
 
  308     size_t maxlong = 
STXXL_MAX(m_param_maxlong, m_opt_maxlong);
 
  310     if (m_paramlist.size())
 
  312         os << 
"Parameters:" << std::endl;
 
  314         for (arglist_type::const_iterator it = m_paramlist.begin();
 
  315              it != m_paramlist.end(); ++it)
 
  319             os << 
"  " << std::setw(maxlong) << std::left << arg->
param_text();
 
  321             std::string typestr = 
"(" + std::string(arg->
type_name()) + 
")";
 
  322             os << std::setw(m_maxtypename + 4) << typestr;
 
  330     if (m_optlist.size())
 
  332         os << 
"Options:" << std::endl;
 
  334         for (arglist_type::const_iterator it = m_optlist.begin();
 
  335              it != m_optlist.end(); ++it)
 
  339             os << 
"  " << std::setw(maxlong) << std::left << arg->
option_text();
 
  341             std::string typestr = 
"(" + std::string(arg->
type_name()) + 
")";
 
  342             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 Tp & STXXL_MAX(const Tp &a, const Tp &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