+string GetOpt::generate_usage(const string &argv0) const
+{
+ string result = argv0;
+ for(list<OptBase *>::const_iterator i=opts.begin(); i!=opts.end(); ++i)
+ {
+ result += " [";
+ if((*i)->get_short())
+ {
+ result += format("-%c", (*i)->get_short());
+ if(!(*i)->get_long().empty())
+ result += '|';
+ else if((*i)->get_arg_type()==OPTIONAL_ARG)
+ result += format("[%s]", (*i)->get_metavar());
+ else if((*i)->get_arg_type()==REQUIRED_ARG)
+ result += format(" %s", (*i)->get_metavar());
+ }
+ if(!(*i)->get_long().empty())
+ {
+ result += format("--%s", (*i)->get_long());
+
+ if((*i)->get_arg_type()==OPTIONAL_ARG)
+ result += format("[=%s]", (*i)->get_metavar());
+ else if((*i)->get_arg_type()==REQUIRED_ARG)
+ result += format("=%s", (*i)->get_metavar());
+ }
+ result += ']';
+ }
+
+ return result;
+}
+
+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)
+ {
+ string swtch;
+ if((*i)->get_short())
+ {
+ swtch += format("-%c", (*i)->get_short());
+ if(!(*i)->get_long().empty())
+ swtch += ", ";
+ else if((*i)->get_arg_type()==OPTIONAL_ARG)
+ swtch += format("[%s]", (*i)->get_metavar());
+ else if((*i)->get_arg_type()==REQUIRED_ARG)
+ swtch += format(" %s", (*i)->get_metavar());
+ }
+ else if(any_short)
+ swtch += " ";
+ if(!(*i)->get_long().empty())
+ {
+ swtch += format("--%s", (*i)->get_long());
+
+ if((*i)->get_arg_type()==OPTIONAL_ARG)
+ swtch += format("[=%s]", (*i)->get_metavar());
+ else if((*i)->get_arg_type()==REQUIRED_ARG)
+ swtch += format("=%s", (*i)->get_metavar());
+ }
+ switches.push_back(swtch);
+ maxw = max(maxw, swtch.size());
+ }
+
+ string result;
+ list<string>::const_iterator j = switches.begin();
+ for(list<OptBase *>::const_iterator i=opts.begin(); i!=opts.end(); ++i, ++j)
+ result += format(" %s%s%s\n", *j, string(maxw+2-j->size(), ' '), (*i)->get_help());
+
+ return result;
+}
+