From: Mikko Rasa Date: Thu, 7 May 2009 06:01:55 +0000 (+0000) Subject: Improve GetOpt help generation X-Git-Tag: 1.1~4 X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=commitdiff_plain;h=c76bd823b81d723d8cd4531631a4b18544f1981a Improve GetOpt help generation --- diff --git a/source/core/getopt.cpp b/source/core/getopt.cpp index b8e2413..8fd13a0 100644 --- a/source/core/getopt.cpp +++ b/source/core/getopt.cpp @@ -37,18 +37,18 @@ string GetOpt::generate_usage(const string &argv0) const 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<<']'; } @@ -62,34 +62,48 @@ The returned string will have a linefeed at the end. */ string GetOpt::generate_help() const { - string result; + bool any_short=false; + for(list::const_iterator i=opts.begin(); (!any_short && i!=opts.end()); ++i) + any_short=(*i)->get_short(); + + unsigned maxw=0; + list switches; for(list::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::const_iterator j=switches.begin(); + for(list::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; @@ -211,4 +225,26 @@ unsigned GetOpt::process_short(const char *const *argp) 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 diff --git a/source/core/getopt.h b/source/core/getopt.h index 7e5e25f..f6c4859 100644 --- a/source/core/getopt.h +++ b/source/core/getopt.h @@ -27,11 +27,13 @@ public: 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; @@ -42,8 +44,9 @@ public: 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: