[complement-svn] SF.net SVN: complement: [1926] trunk/complement/explore
Status: Pre-Alpha
Brought to you by:
complement
From: <com...@us...> - 2008-06-29 21:47:49
|
Revision: 1926 http://complement.svn.sourceforge.net/complement/?rev=1926&view=rev Author: complement Date: 2008-06-29 14:47:47 -0700 (Sun, 29 Jun 2008) Log Message: ----------- Rename Opt -> option; set proper ctors; hide implementation. Modified Paths: -------------- trunk/complement/explore/include/misc/opts.h trunk/complement/explore/lib/misc/opts.cc 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-06-27 17:42:24 UTC (rev 1925) +++ trunk/complement/explore/include/misc/opts.h 2008-06-29 21:47:47 UTC (rev 1926) @@ -1,5 +1,16 @@ -// -*- C++ -*- Time-stamp: <08/05/21 12:17:39 yeti> +// -*- C++ -*- Time-stamp: <08/06/28 10:26:23 ptr> +/* + * Copyright (c) 2008 + * Dmitry Osmakov + * + * Copyright (c) 2008 + * Petr Ovtchenkov + * + * Licensed under the Academic Free License Version 3.0 + * + */ + #ifndef __OPTS_H__ #define __OPTS_H__ @@ -14,11 +25,36 @@ #define all(c) (c).begin() , (c).end() -class Opt +class option { public: - Opt() : shortname(' ') + + option( const char* _description, char _short_var, const char* _long_var ) : + shortname( _short_var ), + longname( _long_var ), + desc( _description ) { } + + option( const char* _description, char _short_var ) : + shortname( _short_var ), + longname(), + desc( _description ) + { } + + option( const char* _description, const char* _long_var ) : + shortname( 0 ), + longname( _long_var ), + desc( _description ) + { } + + 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; } + + private: char shortname; std::string longname; std::string desc; @@ -29,24 +65,31 @@ std::vector< int > pos; bool has_arg; - 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); + + friend std::ostream& operator <<( std::ostream& t, const option& opt ); + friend class Opts; }; class Opts { private: - typedef std::vector< Opt > options_container_type; + typedef std::vector< option > options_container_type; options_container_type storage; + public: - Opts( const std::string& _brief = "", const std::string& _author = "", const std::string& _copyright = "") : - brief(_brief), - author(_author), - copyright(_copyright) - { free_token = 0; } + Opts() : + free_token(0) + { } + void description( const char* text ) + { _brief = text; } + + void author( const char* text ) + { _author = text; } + + void copyright( const char* text ) + { _copyright = text; } + // 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 = "(no decription)" ); @@ -118,13 +161,14 @@ std::invalid_argument(std::string("argument [").append(_argname).append("] doesn't match by type for option ").append(_optname)) { } }; - + + private: int free_token; - private: + std::string pname; - std::string brief; - std::string author; - std::string copyright; + std::string _brief; + std::string _author; + std::string _copyright; bool isterm( const std::string& s ); bool is_opt_name( const std::string& s ); @@ -332,4 +376,4 @@ } } -#endif +#endif // __OPTS_H__ Modified: trunk/complement/explore/lib/misc/opts.cc =================================================================== --- trunk/complement/explore/lib/misc/opts.cc 2008-06-27 17:42:24 UTC (rev 1925) +++ trunk/complement/explore/lib/misc/opts.cc 2008-06-29 21:47:47 UTC (rev 1926) @@ -1,3 +1,16 @@ +// -*- C++ -*- Time-stamp: <08/06/28 10:25:43 ptr> + +/* + * Copyright (c) 2008 + * Dmitry Osmakov + * + * Copyright (c) 2008 + * Petr Ovtchenkov + * + * Licensed under the Academic Free License Version 3.0 + * + */ + #include <vector> #include <string> #include <sstream> @@ -76,14 +89,11 @@ return storage.end(); } -ostream& operator<<(ostream& out,const Opt& opt) +ostream& operator <<( ostream& out, const option& opt ) { - if (opt.shortname == ' ') - { + if ( opt.shortname == 0 ) { out << "--" << opt.longname << '\t' << (opt.has_arg ? (string("[") + opt.default_v + "]\t") : "" ) << opt.desc; - } - else - { + } else { out << '-' << opt.shortname << '\t' << (!opt.longname.empty() ? (string("[--") + opt.longname +"]\t") : "") << (opt.has_arg ? string("[") + opt.default_v + ("]\t") : "") << opt.desc; } @@ -92,12 +102,12 @@ void Opts::help(ostream& out) { - if (!brief.empty()) - out << brief << endl; - if (!author.empty()) - out << author << endl; - if (!copyright.empty()) - out << copyright << endl; + if (!_brief.empty()) + out << _brief << endl; + if (!_author.empty()) + out << _author << endl; + if (!_copyright.empty()) + out << _copyright << endl; out << "Valid options:" << endl; options_container_type::const_iterator i; @@ -107,23 +117,20 @@ } } -int Opts::addflag(char _shortname,const string& _longname,const string& _desc) +int Opts::addflag( char _shortname, const string& _longname, const string& _desc ) { - Opt opt; - opt.shortname = _shortname; - opt.longname = _longname; - opt.desc = _desc; + option opt( _desc.c_str(), _shortname, _longname.c_str() ); + opt.has_arg = false; opt.token = ++free_token; storage.push_back(opt); return opt.token; } -int Opts::addflag(const string& _longname,const string& _desc) +int Opts::addflag( const string& _longname, const string& _desc ) { - Opt opt; - opt.longname = _longname; - opt.desc = _desc; + option opt( _desc.c_str(), _longname.c_str() ); + opt.has_arg = false; opt.token = ++free_token; storage.push_back(opt); Modified: trunk/complement/explore/lib/misc/ut/opts_test.cc =================================================================== --- trunk/complement/explore/lib/misc/ut/opts_test.cc 2008-06-27 17:42:24 UTC (rev 1925) +++ trunk/complement/explore/lib/misc/ut/opts_test.cc 2008-06-29 21:47:47 UTC (rev 1926) @@ -1,9 +1,12 @@ -// -*- C++ -*- Time-stamp: <08/05/21 12:20:14 yeti> +// -*- C++ -*- Time-stamp: <08/06/28 10:25:10 ptr> /* * Copyright (c) 2008 * Petr Ovtchenkov * + * Copyright (c) 2008 + * Dmitry Osmakov + * * Licensed under the Academic Free License Version 3.0 * */ @@ -705,8 +708,11 @@ const char* argv[] = { "name" , "--help" }; int argc = sizeof( argv ) / sizeof(argv[0]); - Opts opts("what utility do","author","copyright"); + Opts opts; + opts.description( "what utility do" ); + opts.author( "author" ); + opts.copyright( "copyright" ); opts.addflag('h',"help","print this help message"); opts.addflag("flag","some program flag"); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |