+string GetOpt::generate_usage(const string &argv0) const
+{
+ ostringstream line;
+
+ line<<argv0;
+ for(list<OptBase *>::const_iterator i=opts.begin(); i!=opts.end(); ++i)
+ {
+ line<<" [";
+ if((*i)->get_short())
+ {
+ line<<'-'<<(*i)->get_short();
+ if(!(*i)->get_long().empty())
+ line<<'|';
+ else if((*i)->get_arg_type()==OPTIONAL_ARG)
+ line<<'['<<(*i)->get_metavar()<<']';
+ else if((*i)->get_arg_type()==REQUIRED_ARG)
+ line<<' '<<(*i)->get_metavar();
+ }
+ if(!(*i)->get_long().empty())
+ {
+ line<<"--"<<(*i)->get_long();
+
+ if((*i)->get_arg_type()==OPTIONAL_ARG)
+ line<<"[="<<(*i)->get_metavar()<<']';
+ else if((*i)->get_arg_type()==REQUIRED_ARG)
+ line<<'='<<(*i)->get_metavar();
+ }
+ line<<']';
+ }
+
+ return line.str();
+}
+
+string GetOpt::generate_help() const
+{
+ bool any_short = false;
+ for(list<OptBase *>::const_iterator i=opts.begin(); (!any_short && i!=opts.end()); ++i)
+ any_short = (*i)->get_short();
+
+ string::size_type maxw = 0;
+ list<string> switches;
+ for(list<OptBase *>::const_iterator i=opts.begin(); i!=opts.end(); ++i)
+ {
+ ostringstream swtch;
+ if((*i)->get_short())
+ {
+ swtch<<'-'<<(*i)->get_short();
+ if(!(*i)->get_long().empty())
+ swtch<<", ";
+ else if((*i)->get_arg_type()==OPTIONAL_ARG)
+ swtch<<'['<<(*i)->get_metavar()<<']';
+ else if((*i)->get_arg_type()==REQUIRED_ARG)
+ swtch<<' '<<(*i)->get_metavar();
+ }
+ else if(any_short)
+ swtch<<" ";
+ if(!(*i)->get_long().empty())
+ {
+ swtch<<"--"<<(*i)->get_long();
+
+ if((*i)->get_arg_type()==OPTIONAL_ARG)
+ swtch<<"[="<<(*i)->get_metavar()<<']';
+ else if((*i)->get_arg_type()==REQUIRED_ARG)
+ swtch<<'='<<(*i)->get_metavar();
+ }
+ switches.push_back(swtch.str());
+ maxw = max(maxw, switches.back().size());
+ }
+
+ string result;
+ list<string>::const_iterator j = switches.begin();
+ for(list<OptBase *>::const_iterator i=opts.begin(); i!=opts.end(); ++i, ++j)
+ {
+ result += " "+*j;
+ result += string(maxw+2-j->size(), ' ');
+ result += (*i)->get_help();
+ result += '\n';
+ }
+
+ return result;
+}
+