if(!(*i)->get_long().empty())
line<<'|';
else if((*i)->get_arg_type()==OPTIONAL_ARG)
- line<<"[ARG]";
+ line<<'['<<(*i)->get_metavar()<<']';
else if((*i)->get_arg_type()==REQUIRED_ARG)
- line<<" ARG";
+ line<<' '<<(*i)->get_metavar();
}
if(!(*i)->get_long().empty())
{
line<<"--"<<(*i)->get_long();
if((*i)->get_arg_type()==OPTIONAL_ARG)
- line<<"[=ARG]";
+ line<<"[="<<(*i)->get_metavar()<<']';
else if((*i)->get_arg_type()==REQUIRED_ARG)
- line<<"=ARG";
+ line<<'='<<(*i)->get_metavar();
}
line<<']';
}
*/
string GetOpt::generate_help() const
{
- string result;
+ bool any_short=false;
+ for(list<OptBase *>::const_iterator i=opts.begin(); (!any_short && i!=opts.end()); ++i)
+ any_short=(*i)->get_short();
+
+ unsigned maxw=0;
+ list<string> switches;
for(list<OptBase *>::const_iterator i=opts.begin(); i!=opts.end(); ++i)
{
- ostringstream line;
- line<<" ";
+ ostringstream swtch;
if((*i)->get_short())
{
- line<<'-'<<(*i)->get_short();
+ swtch<<'-'<<(*i)->get_short();
if(!(*i)->get_long().empty())
- line<<", ";
+ swtch<<", ";
else if((*i)->get_arg_type()==OPTIONAL_ARG)
- line<<"[ARG]";
+ swtch<<'['<<(*i)->get_metavar()<<']';
else if((*i)->get_arg_type()==REQUIRED_ARG)
- line<<" ARG";
+ swtch<<' '<<(*i)->get_metavar();
}
+ else if(any_short)
+ swtch<<" ";
if(!(*i)->get_long().empty())
{
- line<<"--"<<(*i)->get_long();
+ swtch<<"--"<<(*i)->get_long();
if((*i)->get_arg_type()==OPTIONAL_ARG)
- line<<"[=ARG]";
+ swtch<<"[="<<(*i)->get_metavar()<<']';
else if((*i)->get_arg_type()==REQUIRED_ARG)
- line<<"=ARG";
+ swtch<<'='<<(*i)->get_metavar();
}
+ switches.push_back(swtch.str());
+ maxw=max(maxw, switches.back().size());
+ }
- line<<" "<<(*i)->get_help()<<'\n';
-
- result+=line.str();
+ 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;
return 1;
}
+
+GetOpt::OptBase::OptBase(char s, const std::string &l, ArgType a):
+ shrt(s),
+ lng(l),
+ arg_type(a),
+ seen_count(0),
+ metavar("ARG")
+{ }
+
+GetOpt::OptBase &GetOpt::OptBase::set_help(const string &h)
+{
+ help=h;
+ return *this;
+}
+
+GetOpt::OptBase &GetOpt::OptBase::set_help(const string &h, const string &m)
+{
+ help=h;
+ metavar=m;
+ return *this;
+}
+
} // namespace Msp
class OptBase
{
public:
- OptBase &set_help(const std::string &h) { help=h; return *this; }
+ OptBase &set_help(const std::string &);
+ OptBase &set_help(const std::string &, const std::string &);
char get_short() const { return shrt; }
const std::string &get_long() const { return lng; }
ArgType get_arg_type() const { return arg_type; }
const std::string &get_help() const { return help; }
+ const std::string &get_metavar() const { return metavar; }
unsigned get_seen_count() const { return seen_count; }
virtual void process()=0;
virtual void process(const std::string &)=0;
ArgType arg_type;
unsigned seen_count;
std::string help;
+ std::string metavar;
- OptBase(char s, const std::string &l, ArgType a): shrt(s), lng(l), arg_type(a), seen_count(0) { }
+ OptBase(char, const std::string &, ArgType);
};
private: