[complement-svn] SF.net SVN: complement: [1869] trunk/complement/explore
Status: Pre-Alpha
Brought to you by:
complement
From: <com...@us...> - 2008-05-01 11:24:33
|
Revision: 1869 http://complement.svn.sourceforge.net/complement/?rev=1869&view=rev Author: complement Date: 2008-05-01 04:24:15 -0700 (Thu, 01 May 2008) Log Message: ----------- clean code; remove 'use namespace std' from header; push functionality demonstration/development/debugging into unit tests Modified Paths: -------------- trunk/complement/explore/include/misc/opts.h trunk/complement/explore/lib/misc/opts.cpp trunk/complement/explore/lib/misc/ut/Makefile.inc trunk/complement/explore/lib/misc/ut/misc_test_suite.cc Added Paths: ----------- trunk/complement/explore/lib/misc/ut/opts_test.cc trunk/complement/explore/lib/misc/ut/opts_test.h Removed Paths: ------------- trunk/complement/explore/lib/misc/opts_usage.cpp Modified: trunk/complement/explore/include/misc/opts.h =================================================================== --- trunk/complement/explore/include/misc/opts.h 2008-04-30 13:02:36 UTC (rev 1868) +++ trunk/complement/explore/include/misc/opts.h 2008-05-01 11:24:15 UTC (rev 1869) @@ -1,3 +1,5 @@ +// -*- C++ -*- Time-stamp: <08/05/01 12:02:26 ptr> + #ifndef __OPTS_H__ #define __OPTS_H__ @@ -8,77 +10,103 @@ #include <typeinfo> #include <cctype> -using namespace std; - class Opt { -public: - char shortname; - string longname; - string desc; - vector< string > args; + public: + char shortname; + std::string longname; + std::string desc; + std::vector< std::string > args; - bool has_arg; - bool is_set; + bool has_arg; + bool is_set; }; class Opts { -public: -// construct - Opts(const string& _brief = "",const string& _author = "",const string& _copyright = "") : brief(_brief) , author(_author) , copyright(_copyright) {}; + public: + Opts( const std::string& _brief = "", const std::string& _author = "", const std::string& _copyright = "") : + brief(_brief), + author(_author), + copyright(_copyright) + { } -// adding option - void add(char _shortname,const string& _longname = "",const string& _desc = "",bool has_arg = false); + // adding option + void add( char _shortname, const std::string& _longname = "", const std::string& _desc = "", bool has_arg = false ); -// getting option - template <class T> - T get(char _shortname,T& dest); - template <class T> - T get(const string& _longname,T& dest); + // getting option + template <class T> + T get( char _shortname, T& dest ); + + template <class T> + T get( const std::string& _longname, T& dest ); - bool is_set(char _shortname); - bool is_set(const string& _longname); + bool is_set( char _shortname ); + bool is_set( const std::string& _longname ); + // parse + void parse(int ac, const char** av); -// parse - void parse(int ac,char** av); + // stuff + void help(std::ostream& out); + std::string get_pname() const; + std::string get_brief() const; + std::string get_author() const; + std::string get_copyright() const; -// stuff - void help(ostream& out); - string get_pname() const; - string get_brief() const; - string get_author() const; - string get_copyright() const; + // error handling + struct invalid_opt + { + std::string optname; -// error handling - struct invalid_opt { string optname; invalid_opt(const string& _optname) : optname(_optname) {}; }; - struct missing_arg { string optname; missing_arg(const string& _optname) : optname(_optname) {}; }; - struct invalid_arg { string optname,argname; invalid_arg(const string& _optname,const string& _argname) : optname(_optname) , argname(_argname) {}; }; + invalid_opt(const std::string& _optname) : + optname(_optname) + { } + }; + struct missing_arg + { + std::string optname; + + missing_arg( const std::string& _optname) : + optname(_optname) + { } + }; + + struct invalid_arg + { + std::string optname; + std::string argname; + + invalid_arg( const std::string& _optname, const std::string& _argname) : + optname(_optname), + argname(_argname) + { } + }; + - vector< string > args; -private: - // data - vector< Opt > storage; + std::vector< std::string > args; + private: + // data + std::vector< Opt > storage; - string pname; - string brief; - string author; - string copyright; + std::string pname; + std::string brief; + std::string author; + std::string copyright; - bool isterm(const string& s); - bool is_opt_name(const string& s); - bool is_flag_group(const string& s); - bool is_substr(const string& small,const string& big); - int get_opt_index(string s); + bool isterm( const std::string& s ); + bool is_opt_name( const std::string& s ); + bool is_flag_group( const std::string& s ); + bool is_substr(const std::string& small, const std::string& big ); + int get_opt_index( std::string s ); }; template <class T> -T Opts::get(char _shortname,T& res) +T Opts::get( char _shortname, T& res ) { int i; - for (i = 0;i < storage.size();i++) + for (i = 0;i < storage.size();i++) { if (storage[i].shortname == _shortname) { if (storage[i].is_set && storage[i].has_arg) @@ -86,23 +114,24 @@ { try { - stringstream ss(storage[i].args[0]); + std::stringstream ss(storage[i].args[0]); ss >> res; } catch(...) { - throw invalid_arg(string("-") + string(1,_shortname),storage[i].args[0]); + throw invalid_arg(std::string("-") + std::string(1,_shortname),storage[i].args[0]); } } break; - } + } + } if (i == storage.size()) - throw invalid_opt(string("-") + string(1,_shortname)); + throw invalid_opt(std::string("-") + std::string(1,_shortname)); return res; } template <class T> -T Opts::get(const string& _longname,T& res) +T Opts::get(const std::string& _longname,T& res) { int i; for (i = 0;i < storage.size();i++) @@ -113,7 +142,7 @@ { try { - stringstream ss(storage[i].args[0]); + std::stringstream ss(storage[i].args[0]); ss >> res; } catch(...) Modified: trunk/complement/explore/lib/misc/opts.cpp =================================================================== --- trunk/complement/explore/lib/misc/opts.cpp 2008-04-30 13:02:36 UTC (rev 1868) +++ trunk/complement/explore/lib/misc/opts.cpp 2008-05-01 11:24:15 UTC (rev 1869) @@ -2,8 +2,9 @@ #include <string> #include <sstream> #include <typeinfo> -#include "opts.h" +#include <misc/opts.h> + using namespace std; string Opts::get_pname() const { return pname; } @@ -125,7 +126,7 @@ return false; } -void Opts::parse(int ac,char** av) +void Opts::parse(int ac,const char** av) { pname = av[0]; Deleted: trunk/complement/explore/lib/misc/opts_usage.cpp =================================================================== --- trunk/complement/explore/lib/misc/opts_usage.cpp 2008-04-30 13:02:36 UTC (rev 1868) +++ trunk/complement/explore/lib/misc/opts_usage.cpp 2008-05-01 11:24:15 UTC (rev 1869) @@ -1,92 +0,0 @@ -#include <iostream> -#include <string> -#include "opts.h" - -using namespace std; - -struct point -{ - int x; - int y; - point(int _x = 0,int _y = 0) : x(_x) , y(_y) {}; -}; - -istream& operator>>(istream& t,point& p) -{ - t >> p.x >> p.y; - return t; -} - -ostream& operator<<(ostream& t,const point& p) -{ - t << p.x << ' ' << p.y; - return t; -} - -int main(int ac,char** av) -{ - Opts opts; - - // control variables with default values - point p(1,1); - string name = "maos"; - int port = 80; - - opts.add('h',"help","display help message"); - opts.add('v',"verbose","verbose"); - opts.add('j',"just","just do it"); - - opts.add('p',"port","port number",true); - opts.add('s',"point","start point",true); - opts.add('n',"name","your name",true); - opts.add('g'); - - try - { - opts.parse(ac,av); - } - catch(Opts::invalid_opt& t) - { - cout << "Invalid option: " << t.optname << endl; - return 1; - } - catch(Opts::invalid_arg& t) - { - cout << "Invalid argument: " << t.optname << ' ' << t.argname << endl; - return 1; - } - catch(Opts::missing_arg& t) - { - cout << "Missing argument: " << t.optname << endl; - return 1; - } - - if (opts.is_set('h')) - opts.help(cout); - - if (opts.is_set('v')) - cout << "Verbose mode is set" << endl; - else - cout << "Verbose mode is not set" << endl; - - if (opts.is_set('g')) - cout << "-g is set" << endl; - else - cout << "-g is not set" << endl; - - if (opts.is_set("just")) - cout << "Just do it!" << endl; - else - cout << "Just don't do it!" << endl; - - cout << "port = " << opts.get('p',port) << endl; - cout << "point = " << opts.get('s',p) << endl; - cout << "name = " << opts.get('n',name) << endl; - - cout << "operands: " << endl; - - for (int i = 0;i < opts.args.size();i++) - cout << opts.args[i] << endl; - - return 0; -} Modified: trunk/complement/explore/lib/misc/ut/Makefile.inc =================================================================== --- trunk/complement/explore/lib/misc/ut/Makefile.inc 2008-04-30 13:02:36 UTC (rev 1868) +++ trunk/complement/explore/lib/misc/ut/Makefile.inc 2008-05-01 11:24:15 UTC (rev 1869) @@ -1,5 +1,7 @@ -# -*- makefile -*- Time-stamp: <07/07/16 22:12:31 ptr> +# -*- makefile -*- Time-stamp: <08/05/01 12:00:01 ptr> PRGNAME = misc_ut SRC_CC = unit_test.cc \ - misc_test.cc misc_test_suite.cc + misc_test.cc misc_test_suite.cc opts_test.cc + +SRC_CPP = ../opts.cpp Modified: trunk/complement/explore/lib/misc/ut/misc_test_suite.cc =================================================================== --- trunk/complement/explore/lib/misc/ut/misc_test_suite.cc 2008-04-30 13:02:36 UTC (rev 1868) +++ trunk/complement/explore/lib/misc/ut/misc_test_suite.cc 2008-05-01 11:24:15 UTC (rev 1869) @@ -1,7 +1,7 @@ -// -*- C++ -*- Time-stamp: <07/12/02 18:57:27 ptr> +// -*- C++ -*- Time-stamp: <08/05/01 15:18:28 ptr> /* - * Copyright (c) 2007 + * Copyright (c) 2007, 2008 * Petr Ovtchenkov * * Licensed under the Academic Free License Version 3.0 @@ -10,6 +10,7 @@ #include "misc_test_suite.h" #include "misc_test.h" +#include "opts_test.h" #include <config/feature.h> @@ -54,5 +55,21 @@ t.add( &misc_test::type_traits_is_pod, test, "is_pod", tc[0] ); t.add( &misc_test::type_traits_is_empty, test, "is_empty", tc[0] ); + + // test for options parsing + + opts_test opts; + + t.add( &opts_test::bool_option_long, opts, "simple boolean option, long", + t.add( &opts_test::bool_option, opts, "simple boolean option" ) ); + + t.add( &opts_test::int_option_long, opts, "option with int parameter, long", + t.add( &opts_test::int_option, opts, "option with int parameter" ) ); + + t.add( &opts_test::bad_option, opts, "bad option" ); + t.add( &opts_test::bad_argument, opts, "bad argument" ); + + t.add( &opts_test::user_defined, opts, "user-defined type" ); + return t.girdle(); }; Added: trunk/complement/explore/lib/misc/ut/opts_test.cc =================================================================== --- trunk/complement/explore/lib/misc/ut/opts_test.cc (rev 0) +++ trunk/complement/explore/lib/misc/ut/opts_test.cc 2008-05-01 11:24:15 UTC (rev 1869) @@ -0,0 +1,344 @@ +// -*- C++ -*- Time-stamp: <08/05/01 15:16:10 ptr> + +/* + * Copyright (c) 2008 + * Petr Ovtchenkov + * + * Licensed under the Academic Free License Version 3.0 + * + */ + +#include "opts_test.h" + +#include <misc/opts.h> + +// #include <iostream> + +using namespace std; + +int EXAM_IMPL(opts_test::bool_option) +{ + const char* argv[] = { "name", "-h" }; + int argc = sizeof( argv ) / sizeof(argv[0]); + + Opts opts; + + opts.add( 'h', "help", "print this help message" ); + + try { + opts.parse( argc, argv ); + + EXAM_CHECK( opts.is_set( 'h' ) ); + } + catch ( const Opts::invalid_opt& e ) { + } + catch ( const Opts::invalid_arg& e ) { + } + + return EXAM_RESULT; +} + +int EXAM_IMPL(opts_test::bool_option_long) +{ + const char* argv[] = { "name", "--help" }; + int argc = sizeof( argv ) / sizeof(argv[0]); + + Opts opts; + + opts.add( 'h', "help", "print this help message" ); + + try { + opts.parse( argc, argv ); + + EXAM_CHECK( opts.is_set( 'h' ) ); + } + catch ( const Opts::invalid_opt& e ) { + } + catch ( const Opts::invalid_arg& e ) { + } + + return EXAM_RESULT; +} + +int EXAM_IMPL(opts_test::int_option) +{ + + const char* argv[] = { "name", "-p", "80" }; + int argc = sizeof( argv ) / sizeof(argv[0]); + + Opts opts; + + opts.add( 'p', "port", "listen tcp port" ); + + try { + opts.parse( argc, argv ); + + int port = 0; + + EXAM_CHECK( opts.is_set( 'p' ) ); + EXAM_CHECK( opts.get( 'p', port ) == 80 ); + } + catch ( const Opts::invalid_opt& e ) { + } + catch ( const Opts::invalid_arg& e ) { + } + + return EXAM_RESULT; +} + +int EXAM_IMPL(opts_test::int_option_long) +{ + const char* argv[] = { "name", "--port=80" }; + int argc = sizeof( argv ) / sizeof(argv[0]); + + Opts opts; + + opts.add( 'p', "port", "listen tcp port" ); + + try { + opts.parse( argc, argv ); + + int port = 0; + + EXAM_CHECK( opts.is_set( 'p' ) ); + EXAM_CHECK( opts.get( 'p', port ) == 80 ); + } + catch ( const Opts::invalid_opt& e ) { + } + catch ( const Opts::invalid_arg& e ) { + } + + return EXAM_RESULT; +} + +int EXAM_IMPL(opts_test::bad_option) +{ + const char* argv[] = { "name", "-v" }; + int argc = sizeof( argv ) / sizeof(argv[0]); + + Opts opts; + + opts.add( 'h', "help", "print this help message" ); + + bool exception_happens = false; + + try { + opts.parse( argc, argv ); + + EXAM_ERROR( "exception expected" ); + } + catch ( const Opts::invalid_opt& e ) { + exception_happens = true; + EXAM_CHECK( e.optname == "-v" ); + } + catch ( const Opts::invalid_arg& e ) { + } + + EXAM_CHECK( exception_happens ); + + return EXAM_RESULT; +} + +int EXAM_IMPL(opts_test::bad_argument) +{ + const char* argv[] = { "name", "--port=www" }; + int argc = sizeof( argv ) / sizeof(argv[0]); + + Opts opts; + + opts.add( 'p', "port", "listen tcp port" ); + + bool exception_happens = false; + + try { + opts.parse( argc, argv ); + + EXAM_ERROR( "exception expected" ); + } + catch ( const Opts::invalid_opt& e ) { + } + catch ( const Opts::invalid_arg& e ) { + exception_happens = true; + EXAM_CHECK( e.optname == "--port" ); + EXAM_CHECK( e.argname == "www" ); + } + + EXAM_CHECK( exception_happens ); + + return EXAM_RESULT; +} + +int EXAM_IMPL(opts_test::multiple) +{ + { + const char* argv[] = { "name", "-vvv" }; + int argc = sizeof( argv ) / sizeof(argv[0]); + + Opts opts; + + opts.add( 'v', "verbose", "more trace messages" ); + + opts.parse( argc, argv ); + } + + { + const char* argv[] = { "name", "-v", "-v", "-v" }; + int argc = sizeof( argv ) / sizeof(argv[0]); + + Opts opts; + + opts.add( 'v', "verbose", "more trace messages" ); + + opts.parse( argc, argv ); + } + + { + const char* argv[] = { "name", "--verbose", "--verbose", "--verbose" }; + int argc = sizeof( argv ) / sizeof(argv[0]); + + Opts opts; + + opts.add( 'v', "verbose", "more trace messages" ); + + opts.parse( argc, argv ); + } + + return EXAM_RESULT; +} + +int EXAM_IMPL(opts_test::compound) +{ + { + const char* argv[] = { "name", "-abc" }; + int argc = sizeof( argv ) / sizeof(argv[0]); + + Opts opts; + + opts.add( 'a', "a-option", "option a" ); + opts.add( 'b', "b-option", "option b" ); + opts.add( 'c', "c-option", "option c" ); + + opts.parse( argc, argv ); + } + + return EXAM_RESULT; +} + +int EXAM_IMPL(opts_test::args) +{ + { + const char* argv[] = { "name", "-f", "filename.conf", "file1", "file2" }; + int argc = sizeof( argv ) / sizeof(argv[0]); + + Opts opts; + + opts.add( 'f', "config", "configuration file" ); + + opts.parse( argc, argv ); + + EXAM_CHECK( argc == 3 ); + EXAM_CHECK( argv[0] == "name" ); + EXAM_CHECK( argv[1] == "file1" ); + EXAM_CHECK( argv[2] == "file2" ); + } + + { + const char* argv[] = { "name", "file1", "file2", "-f", "filename.conf" }; + int argc = sizeof( argv ) / sizeof(argv[0]); + + Opts opts; + + opts.add( 'f', "config", "configuration file" ); + + opts.parse( argc, argv ); + + EXAM_CHECK( argc == 3 ); + EXAM_CHECK( argv[0] == "name" ); + EXAM_CHECK( argv[1] == "file1" ); + EXAM_CHECK( argv[2] == "file2" ); + } + + { + const char* argv[] = { "name", "file1", "-f", "filename.conf", "file2" }; + int argc = sizeof( argv ) / sizeof(argv[0]); + + Opts opts; + + opts.add( 'f', "config", "configuration file" ); + + opts.parse( argc, argv ); + + EXAM_CHECK( argc == 3 ); + EXAM_CHECK( argv[0] == "name" ); + EXAM_CHECK( argv[1] == "file1" ); + EXAM_CHECK( argv[2] == "file2" ); + } + + return EXAM_RESULT; +} + +int EXAM_IMPL(opts_test::stop) +{ + { + const char* argv[] = { "name", "-a", "--", "-f" }; + int argc = sizeof( argv ) / sizeof(argv[0]); + + Opts opts; + + opts.add( 'a', "a-option", "option a" ); + + opts.parse( argc, argv ); + + EXAM_CHECK( argc == 2 ); + EXAM_CHECK( argv[1] == "-f" ); + } + + return EXAM_RESULT; +} + +struct point +{ + point( int _x = 0, int _y = 0 ) : + x(_x), + y(_y) + { } + + int x; + int y; +}; + +istream& operator >>( istream& s, point& p ) +{ + s >> p.x >> p.y; + + return s; +} + +ostream& operator <<( ostream& s, const point& p ) +{ + s << p.x << ' ' << p.y; + + return s; +} + +int EXAM_IMPL(opts_test::user_defined) +{ + { + const char* argv[] = { "name", "-s", "1 2" }; + int argc = sizeof( argv ) / sizeof(argv[0]); + + Opts opts; + + opts.add( 's', "start-point", "start point", true ); + + opts.parse( argc, argv ); + + point p( 1, 1 ); + + opts.get( 's', p ); + + EXAM_CHECK( (p.x == 1) && (p.y = 2) ); + } + + return EXAM_RESULT; +} Added: trunk/complement/explore/lib/misc/ut/opts_test.h =================================================================== --- trunk/complement/explore/lib/misc/ut/opts_test.h (rev 0) +++ trunk/complement/explore/lib/misc/ut/opts_test.h 2008-05-01 11:24:15 UTC (rev 1869) @@ -0,0 +1,35 @@ +// -*- C++ -*- Time-stamp: <08/05/01 15:17:31 ptr> + +/* + * Copyright (c) 2008 + * Petr Ovtchenkov + * + * Licensed under the Academic Free License Version 3.0 + * + */ + +#ifndef __OPTS_TEST_H +#define __OPTS_TEST_H + +#define FIT_EXAM + +#include <exam/suite.h> + +class opts_test +{ + public: + // implementation + int EXAM_DECL(bool_option); + int EXAM_DECL(bool_option_long); + int EXAM_DECL(int_option); + int EXAM_DECL(int_option_long); + int EXAM_DECL(bad_option); + int EXAM_DECL(bad_argument); + int EXAM_DECL(multiple); + int EXAM_DECL(compound); + int EXAM_DECL(args); + int EXAM_DECL(stop); + int EXAM_DECL(user_defined); +}; + +#endif // __MISC_TEST_H This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |