From cb5dfe06a55a2b382dbd5ed0848bd42d0d06a699 Mon Sep 17 00:00:00 2001 From: Konrad Rosenbaum Date: Tue, 1 May 2012 22:01:34 +0200 Subject: [PATCH] start on option help --- client.cpp | 12 +++++++++--- optparse.cpp | 30 +++++++++++++++++++++++++++++- optparse.h | 13 +++++++++++-- 3 files changed, 49 insertions(+), 6 deletions(-) diff --git a/client.cpp b/client.cpp index 3bc2841..f5d1e41 100644 --- a/client.cpp +++ b/client.cpp @@ -292,6 +292,8 @@ struct option longopt[]= { using namespace OptParse; using namespace std; + + /*main loop, message sender, etc.pp.*/ int main(int argc,char**argv) { @@ -300,8 +302,12 @@ int main(int argc,char**argv) /*parse options*/ argv0=*argv; Parser pp({ - Argument({"local-id"_Opt,"l"_Opt,Option::ArgumentRequired}, [](string,string a){localid=a;}), - Argument({"log-level"_Opt,'L'_Opt,Option::ArgumentRequired}, [](string,string a){setloglevel(a);}), + Argument({"local-id"_Opt,"l"_Opt,Option::ArgumentRequired, + "set the local ID from which the DUID is calculated"_Descr}, + [](string,string a){localid=a;}), + Argument({"log-level"_Opt,'L'_Opt,Option::ArgumentRequired, + "set the log level (default is warn), must be one of: none, error, warn, info, debug"_Descr}, + [](string,string a){setloglevel(a);}), Argument({"prefix"_Opt,'p'_Opt},[](std::string,std::string){getprefix=1;}), Argument({"no-prefix"_Opt,'P'_Opt},[](std::string,std::string){getprefix=0;}), Argument({"address"_Opt,'a'_Opt},[](string,string){getaddress=1;}), @@ -311,7 +317,7 @@ int main(int argc,char**argv) Argument({"rapid-commit"_Opt,'c'_Opt},[](string,string){userapid=1;}), Argument({"no-rapid-commit"_Opt,'C'_Opt},[](string,string){userapid=1;}), Argument({"retries"_Opt,'r'_Opt,Option::ArgumentRequired},[](string,string a){retries=atoi(a.data());}), - Argument({"help"_Opt,'h'_Opt},[](string,string){printhelp();exit(0);}), + Argument({"help"_Opt,'h'_Opt},[&](string,string){printhelp();pp.printHelp(2);exit(0);}), Argument({"duid"_Opt,'u'_Opt,Option::ArgumentRequired}, [](string,string a){setduid(a);}), Argument({"dev"_Opt,"device"_Opt,Option::ArgumentRequired}, [](string,string a){device=a;}), Argument({"script"_Opt,"s"_Opt,Option::ArgumentRequired}, [](string, string a){script=a;}), diff --git a/optparse.cpp b/optparse.cpp index 38b6b1a..140ecc9 100644 --- a/optparse.cpp +++ b/optparse.cpp @@ -34,7 +34,6 @@ Argument::Argument(const Option& o, OptionCallback oc) Argument::Argument(const std::initializer_list< Option >& ol, OptionCallback oc) :m_call(oc) { - //TODO: filter enum opts for(const Option&o:ol)setOption(o); } @@ -59,6 +58,8 @@ void Argument::setOption(const Option& o) if(o.type()==Option::Type::NeedArg) if(o.needArg()!=Option::NeedArg::Null) m_arg=o.needArg(); + if(o.type()==Option::Type::Description) + m_descr=o.name(); } Argument Parser::get(const std::string& n) const @@ -165,4 +166,31 @@ void Parser::parseFile(std::istream& stream) const } } +void Parser::printHelp(int base, int indent) const +{ + //calc indents + std::string basestr,indstr; + for(int i=0;iOptionCallback; @@ -74,6 +75,7 @@ class Argument std::vector m_opt; Option::NeedArg m_arg; OptionCallback m_call; + std::string m_descr; public: Argument():m_arg(Option::NeedArg::Null){} Argument(const Option&o,OptionCallback oc=nullptr); @@ -93,6 +95,11 @@ class Argument void setOption(const Option&); void setCallback(OptionCallback oc){m_call=oc;} + void setDescription(const std::string&s){m_descr=s;} + std::string description()const{return m_descr;} + + std::vector options()const{return m_opt;} + void operator()(std::string name,std::string value)const {if(m_call)m_call(name,value);} }; @@ -128,6 +135,8 @@ class Parser void parseFile(const std::string &filename)const; void parseFile(std::istream &stream)const; + + void printHelp(int base=0,int indent=2)const; private: void parseArgument(const std::string&,bool)const; }; -- 1.7.2.5