[complement-svn] SF.net SVN: complement: [1882] trunk/complement/explore
Status: Pre-Alpha
Brought to you by:
complement
From: <dmi...@us...> - 2008-05-25 12:14:47
|
Revision: 1882 http://complement.svn.sourceforge.net/complement/?rev=1882&view=rev Author: dmitryosmakov Date: 2008-05-25 05:14:17 -0700 (Sun, 25 May 2008) Log Message: ----------- fixed lib Opts : loops in get functions are replaced by find and copy algorithms , help function is rewriten (using overloaded << operator for class Opt) Modified Paths: -------------- trunk/complement/explore/include/misc/opts.h trunk/complement/explore/lib/misc/opts.cpp trunk/complement/explore/lib/misc/ut/opts_test.cc Modified: trunk/complement/explore/include/misc/opts.h =================================================================== --- trunk/complement/explore/include/misc/opts.h 2008-05-24 10:09:48 UTC (rev 1881) +++ trunk/complement/explore/include/misc/opts.h 2008-05-25 12:14:17 UTC (rev 1882) @@ -12,28 +12,27 @@ #include <exception> #include <stdexcept> -const char* OPT_DEFAULT_DESCRIPTION = "(no description)"; -const char* OPT_DEFAULT_LONGNAME = "(no longname)"; +#define all(c) (c).begin() , (c).end() class Opt { public: - Opt() { cnt = 0 , shortname = ' '; } + Opt() : shortname(' ') + { } char shortname; std::string longname; std::string desc; std::string default_v; int token; - + std::vector< std::string > args; std::vector< int > pos; bool has_arg; - bool is_set; - int cnt; // number of times this option was encounterd in command line bool operator==( const std::string& _longname ) const { return longname == _longname; } bool operator==( char _shortname ) const { return shortname == _shortname; } bool operator==( int _token ) const { return token == _token; } + friend std::ostream& operator<<(std::ostream& t,const Opt& opt); }; class Opts @@ -50,14 +49,14 @@ // adding option / flag (option that doesn't need arguments) template <class T> - int add( const std::string& _longname,T _default_value,const std::string& _desc = OPT_DEFAULT_DESCRIPTION ); + int add( const std::string& _longname,T _default_value,const std::string& _desc = "(no decription)" ); template <class T> - int add( char _shortname,T _default_value,const std::string& _longname = OPT_DEFAULT_LONGNAME, const std::string& _desc = OPT_DEFAULT_DESCRIPTION ); + int add( char _shortname,T _default_value,const std::string& _longname = "(no longname)", const std::string& _desc = "(no decription)" ); - int addflag( const std::string& _longname,const std::string& desc = OPT_DEFAULT_DESCRIPTION); + int addflag( const std::string& _longname = "(no longname)",const std::string& desc = "(no decription)"); - int addflag( char _shortname,const std::string& _longname = OPT_DEFAULT_LONGNAME,const std::string& _desc = OPT_DEFAULT_DESCRIPTION ); + int addflag( char _shortname,const std::string& _longname = "(no longname)",const std::string& _desc = "(no decription)" ); // getting option @@ -151,54 +150,42 @@ template <class T> bool Opts::is_set(T field) const { - options_container_type::const_iterator i; - for (i = storage.begin();i != storage.end();++i) - if (*i == field) - return i->is_set; - return false; + options_container_type::const_iterator i = find(all(storage),field); + return ( (i == storage.end()) ? false : !i->pos.empty()); } template <class T> int Opts::get_cnt(T field) const { - options_container_type::const_iterator i; - for (i = storage.begin();i != storage.end();++i) - if (*i == field) - return i->cnt; - return 0; + options_container_type::const_iterator i = find(all(storage),field); + return ( (i == storage.end()) ? 0 : i->pos.size()); } template < class T , class V > T Opts::get( V field ) { - options_container_type::const_iterator i; + options_container_type::const_iterator i = find(all(storage),field); T res; - for ( i = storage.begin(); i != storage.end(); ++i ) + + if (i != storage.end()) { - if ( *i == field ) + if ( !i->has_arg ) { - if ( !i->has_arg ) - { - throw std::logic_error("using Opts::get for option without arguments"); - } - - std::stringstream ss; - ss << (i->args.empty() ? i->default_v : i->args[0]); - ss >> res; + throw std::logic_error("using Opts::get for option without arguments"); + } - if (ss.fail()) - { - std::stringstream ss1; - ss1 << field; - throw arg_typemismatch(ss1.str(),i->args[0]); - } - - break; + std::stringstream ss(i->args.empty() ? i->default_v : i->args[0]); + ss >> res; + + if (ss.fail()) + { + std::stringstream ss1; + ss1 << field; + throw arg_typemismatch(ss1.str(),i->args.empty() ? i->default_v : i->args[0]); } } - - if ( i == storage.end() ) - { + else + { std::stringstream ss1; ss1 << field; throw unknown_option( ss1.str() ); @@ -211,67 +198,60 @@ template <class T , class V> T Opts::get_default( V field ) { - options_container_type::const_iterator i; + options_container_type::const_iterator i = find(all(storage),field); T res; - for (i = storage.begin();i != storage.end();++i) - if (*i == field) - { - if (!i->has_arg) - throw std::logic_error("using Opts::get for option without arguments"); + + if (i != storage.end()) + { + if (!i->has_arg) + throw std::logic_error("using Opts::get for option without arguments"); - std::stringstream ss; - ss << i->default_v; + std::stringstream ss(i->default_v); + ss >> res; - ss >> res; - - if (ss.fail()) - { - std::stringstream ss1; - ss1 << field; - throw arg_typemismatch(ss1.str(),i->default_v); - } - - break; - } - - if (i == storage.end()) + if (ss.fail()) + { + std::stringstream ss1; + ss1 << field; + throw arg_typemismatch(ss1.str(),i->default_v); + } + } + else { std::stringstream ss1; ss1 << field; throw unknown_option( ss1.str() ); } + return res; } template <class V , class BackInsertIterator> void Opts::getemall( V field , BackInsertIterator bi) { - options_container_type::const_iterator i; - for (i = storage.begin();i != storage.end();++i) - if (*i == field) - { - if (!i->has_arg) - throw std::logic_error("using Opts::getemall for option without arguments"); + options_container_type::const_iterator i = find(all(storage),field); + if (i != storage.end()) + { + if (!i->has_arg) + throw std::logic_error("using Opts::getemall for option without arguments"); - if (!i->args.empty()) - for (int j = 0;j < i->args.size();j++) - { + if (!i->args.empty()) + { + for (int j = 0;j < i->args.size();++j) + { + std::stringstream ss(i->args[j]); + ss >> *bi++; - std::stringstream ss(i->args[j]); - ss >> *bi++; - - if (ss.fail()) - { - std::stringstream ss1; - ss1 << field; - throw arg_typemismatch(ss1.str(),i->args[j]); - } + if (ss.fail()) + { + std::stringstream ss1; + ss1 << field; + throw arg_typemismatch(ss1.str(),i->args[j]); } - - break; + } } - - if (i == storage.end()) + } + else { std::stringstream ss1; ss1 << field; @@ -282,27 +262,18 @@ template <class V , class BackInsertIterator> void Opts::get_pos( V field , BackInsertIterator bi) { - options_container_type::const_iterator i; - for (i = storage.begin();i != storage.end();++i) - if (*i == field) - { - if (i->is_set) - for (int j = 0;j < i->pos.size();j++) - { - *bi++ = i->pos[j]; - } - - break; - } + options_container_type::const_iterator i = find(all(storage),field); - if (i == storage.end()) + if (i != storage.end()) { + copy(all(i->pos),bi); + } + else + { std::stringstream ss1; ss1 << field; throw unknown_option(ss1.str()); } } - - #endif Modified: trunk/complement/explore/lib/misc/opts.cpp =================================================================== --- trunk/complement/explore/lib/misc/opts.cpp 2008-05-24 10:09:48 UTC (rev 1881) +++ trunk/complement/explore/lib/misc/opts.cpp 2008-05-25 12:14:17 UTC (rev 1882) @@ -10,10 +10,6 @@ string Opts::get_pname() const { return pname; } -bool Opts::isterm(const string& s) -{ - return (s == "--"); -} bool Opts::is_opt_name(const string& s) { @@ -81,6 +77,20 @@ return storage.end(); } +ostream& operator<<(ostream& out,const Opt& opt) +{ + if (opt.shortname == ' ') + { + out << "--" << opt.longname << '\t' << (opt.has_arg ? (string("[") + opt.default_v + "]\t") : "" ) << opt.desc; + } + else + { + out << '-' << opt.shortname << '\t' << (!opt.longname.empty() ? (string("[--") + opt.longname +"]\t") : "") << (opt.has_arg ? string("[") + opt.default_v + ("]\t") : "") << opt.desc; + } + + return out; +} + void Opts::help(ostream& out) { if (!brief.empty()) @@ -90,13 +100,11 @@ if (!copyright.empty()) out << copyright << endl; - out << "usage: " << endl; - out << pname << " [option ...] [optiongoup ...] [end operands ...]" << endl; - out << "available options:" << endl << "shortname:" << '\t' << "longname:" << '\t' << "default value" << '\t' << "description" << endl; + out << "Valid options:" << endl; options_container_type::const_iterator i; for (i = storage.begin();i != storage.end();++i) { - out << i->shortname << "\t[--" << i->longname << "] [" << i->default_v << "]\t-\t" << i->desc << endl; + out << *i << endl; } } @@ -107,7 +115,6 @@ opt.longname = _longname; opt.desc = _desc; opt.has_arg = false; - opt.is_set = false; opt.token = ++free_token; storage.push_back(opt); return opt.token; @@ -119,7 +126,6 @@ opt.longname = _longname; opt.desc = _desc; opt.has_arg = false; - opt.is_set = false; opt.token = ++free_token; storage.push_back(opt); return opt.token; @@ -132,7 +138,7 @@ int i = 1; int j = 1; int q = 0; - while (i < ac && !isterm(av[i])) + while ( (i < ac) && (string(av[i]) != "--") ) { if (is_opt_name(av[i])) { @@ -154,8 +160,6 @@ else { p->pos.push_back(++q); - p->is_set = true; - p->cnt++; if (p->has_arg) { if (!arg.empty()) @@ -184,8 +188,6 @@ } else { - p->is_set = true; - p->cnt++; p->pos.push_back(++q); if (p->has_arg) throw missing_arg( "-" + string(1,optgroup[j]) ); @@ -197,7 +199,7 @@ i++; } - i += (i < ac && isterm(av[i])); + i += ( i < ac ); while (i < ac) av[j++] = av[i++]; Modified: trunk/complement/explore/lib/misc/ut/opts_test.cc =================================================================== --- trunk/complement/explore/lib/misc/ut/opts_test.cc 2008-05-24 10:09:48 UTC (rev 1881) +++ trunk/complement/explore/lib/misc/ut/opts_test.cc 2008-05-25 12:14:17 UTC (rev 1882) @@ -15,12 +15,10 @@ #include <vector> #include <list> #include <fstream> + #include <iostream> -// #include <iostream> - using namespace std; - int EXAM_IMPL(opts_test::bool_option) { const char* argv[] = { "name", "-h" }; @@ -711,11 +709,11 @@ opts.addflag('h',"help","print this help message"); - opts.addflag('v',"verbose"); opts.addflag("flag","some program flag"); + opts.addflag('v',"version","view program version"); opts.add('I',"/usr/include","include","include paths" ); opts.add('p',80,"port","listen to tcp port"); - opts.add('t',"standart");; + opts.add("mode","standart","program mode"); opts.parse(argc,argv); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |