diff --git a/example/Jamfile b/example/Jamfile index 7ef3eb32..c013f5b1 100644 --- a/example/Jamfile +++ b/example/Jamfile @@ -7,6 +7,8 @@ subproject libs/test/example ; +DEPENDS test : all ; + rule boost-test-example ( example-name : lib-name ) { exe $(example-name) : $(example-name).cpp ../build/$(lib-name) @@ -24,4 +26,5 @@ boost-test-example unit_test_example3 : boost_unit_test_framework ; boost-test-example unit_test_example4 : boost_unit_test_framework ; boost-test-example unit_test_example5 : boost_unit_test_framework ; boost-test-example test_case_template_example : boost_unit_test_framework ; +boost-test-example named_param_example : boost_unit_test_framework ; diff --git a/example/cla/Jamfile b/example/cla/Jamfile new file mode 100755 index 00000000..a7406d8b --- /dev/null +++ b/example/cla/Jamfile @@ -0,0 +1,47 @@ +# (C) Copyright Gennadiy Rozental 2001-2005. +# Use, modification, and distribution are subject to the +# Boost Software License, Version 1.0. (See accompanying file +# LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +# +# See http://www.boost.org/libs/test for the library home page. + +subproject libs/test/example/cla ; + +DEPENDS test : all ; + +rule boost-runtime-param-example ( example-name ) +{ + exe $(example-name) : $(example-name).cpp + : $(BOOST_ROOT) + on + <*>-w-8080 ; +} + +boost-runtime-param-example assign_to ; +boost-runtime-param-example basic_float ; +boost-runtime-param-example basic_int ; +boost-runtime-param-example basic_list ; +boost-runtime-param-example basic_string ; +boost-runtime-param-example basic_udt ; +boost-runtime-param-example char_parameter ; +boost-runtime-param-example custom_handler ; +boost-runtime-param-example custom_interpreter ; +boost-runtime-param-example custom_parameter ; +boost-runtime-param-example default_value ; +boost-runtime-param-example dual_name ; +boost-runtime-param-example global ; +boost-runtime-param-example guess_name ; +boost-runtime-param-example help ; +boost-runtime-param-example ignore_mismatch ; +boost-runtime-param-example input_separator ; +boost-runtime-param-example multiplicable ; +boost-runtime-param-example name ; +boost-runtime-param-example optional ; +boost-runtime-param-example optional_value ; +boost-runtime-param-example positional ; +boost-runtime-param-example prefix ; +boost-runtime-param-example reference ; +boost-runtime-param-example remainder ; +boost-runtime-param-example separator ; +boost-runtime-param-example usage ; +boost-runtime-param-example wide_string ; diff --git a/example/cla/assign_to.cpp b/example/cla/assign_to.cpp new file mode 100755 index 00000000..9990c023 --- /dev/null +++ b/example/cla/assign_to.cpp @@ -0,0 +1,41 @@ +// (C) Copyright Gennadiy Rozental 2001-2004. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// See http://www.boost.org/libs/test for the library home page. + +// Boost.Runtime.Param +#include +#include + +namespace rt = boost::runtime; +namespace cla = boost::runtime::cla; + +// STL +#include + +int main() { + char* argv[] = { "basic", "-abcd", "25" }; + int argc = sizeof(argv)/sizeof(char*); + + try { + cla::parser P; + + int i = 0; + + P << cla::named_parameter( "abcd" ) - (cla::assign_to = i); + + P.parse( argc, argv ); + + std::cout << "abcd = " << i << std::endl; + } + catch( rt::logic_error const& ex ) { + std::cout << "Logic error: " << ex.msg() << std::endl; + return -1; + } + + return 0; +} + +// EOF diff --git a/example/cla/basic_float.cpp b/example/cla/basic_float.cpp new file mode 100755 index 00000000..d4f73ae6 --- /dev/null +++ b/example/cla/basic_float.cpp @@ -0,0 +1,40 @@ +// (C) Copyright Gennadiy Rozental 2001-2004. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// See http://www.boost.org/libs/test for the library home page. + +// Boost.Runtime.Param +#include +#include + +namespace rt = boost::runtime; +namespace cla = boost::runtime::cla; + +// STL +#include + +int main() { + char* argv[] = { "basic", "-abcd", "25.45" }; + int argc = sizeof(argv)/sizeof(char*); + + try { + cla::parser P; + + P << cla::named_parameter( "abcd" ); + + P.parse( argc, argv ); + + std::cout << "abcd = " << P.get( "abcd" ) << std::endl; + } + catch( rt::logic_error const& ex ) { + std::cout << "Logic error: " << ex.msg() << std::endl; + return -1; + } + + return 0; +} + +// EOF + diff --git a/example/cla/basic_int.cpp b/example/cla/basic_int.cpp new file mode 100755 index 00000000..79433dbc --- /dev/null +++ b/example/cla/basic_int.cpp @@ -0,0 +1,39 @@ +// (C) Copyright Gennadiy Rozental 2001-2004. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// See http://www.boost.org/libs/test for the library home page. + +// Boost.Runtime.Param +#include +#include + +namespace rt = boost::runtime; +namespace cla = boost::runtime::cla; + +// STL +#include + +int main() { + char* argv[] = { "basic", "-abcd", "25" }; + int argc = sizeof(argv)/sizeof(char*); + + try { + cla::parser P; + + P << cla::named_parameter( "abcd" ); + + P.parse( argc, argv ); + + std::cout << "abcd = " << P.get( "abcd" ) << std::endl; + } + catch( rt::logic_error const& ex ) { + std::cout << "Logic error: " << ex.msg() << std::endl; + return -1; + } + + return 0; +} + +// EOF diff --git a/example/cla/basic_list.cpp b/example/cla/basic_list.cpp new file mode 100755 index 00000000..ace9dd61 --- /dev/null +++ b/example/cla/basic_list.cpp @@ -0,0 +1,44 @@ +// (C) Copyright Gennadiy Rozental 2001-2004. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// See http://www.boost.org/libs/test for the library home page. + +// Boost.Runtime.Param +#include +#include + +namespace rt = boost::runtime; +namespace cla = boost::runtime::cla; + +// STL +#include +#include + +int main() { + char* argv[] = { "basic", "-abcd", "25,12,45,67,1" }; + int argc = sizeof(argv)/sizeof(char*); + + try { + cla::parser P; + + P << cla::named_parameter >( "abcd" ); + + P.parse( argc, argv ); + + std::list arg_values = P.get >( "abcd" ); + + std::cout << "abcd = "; + std::copy( arg_values.begin(), arg_values.end(), std::ostream_iterator( std::cout, " " ) ); + std::cout << std::endl; + } + catch( rt::logic_error const& ex ) { + std::cout << "Logic error: " << ex.msg() << std::endl; + return -1; + } + + return 0; +} + +// EOF diff --git a/example/cla/basic_string.cpp b/example/cla/basic_string.cpp new file mode 100755 index 00000000..4e228d5e --- /dev/null +++ b/example/cla/basic_string.cpp @@ -0,0 +1,39 @@ +// (C) Copyright Gennadiy Rozental 2001-2004. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// See http://www.boost.org/libs/test for the library home page. + +// Boost.Runtime.Param +#include +#include + +namespace rt = boost::runtime; +namespace cla = boost::runtime::cla; + +// STL +#include + +int main() { + char* argv[] = { "basic", "-abcd", "25" }; + int argc = sizeof(argv)/sizeof(char*); + + try { + cla::parser P; + + P << cla::named_parameter( "abcd" ); + + P.parse( argc, argv ); + + std::cout << "abcd = " << P.get( "abcd" ) << std::endl; + } + catch( rt::logic_error const& ex ) { + std::cout << "Logic error: " << ex.msg() << std::endl; + return -1; + } + + return 0; +} + +// EOF diff --git a/example/cla/basic_udt.cpp b/example/cla/basic_udt.cpp new file mode 100755 index 00000000..3d0275da --- /dev/null +++ b/example/cla/basic_udt.cpp @@ -0,0 +1,72 @@ +// (C) Copyright Gennadiy Rozental 2001-2004. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// See http://www.boost.org/libs/test for the library home page. + +// Boost.Runtime.Param +#include +#include + +namespace rt = boost::runtime; +namespace cla = boost::runtime::cla; + +// STL +#include + +// Library code +struct FullName +{ + std::string first_name; + std::string last_name; +}; + +std::istream& +operator>>( std::istream& istr, FullName& f ) +{ + std::string token; + istr >> token; + + std::size_t pos = token.find( ',' ); + if( pos != std::string::npos ) { + f.first_name.assign( token.begin(), token.begin()+pos ); + f.last_name.assign( token.begin()+pos+1, token.end() ); + } + else { + f.first_name = token; + } + + return istr; +} + +std::ostream& +operator<<( std::ostream& ostr, FullName const& f ) +{ + return ostr << f.first_name << ':' << f.last_name; +} + +// End of library code + +int main() { + char* argv[] = { "basic", "-abcd", "John,Doe" }; + int argc = sizeof(argv)/sizeof(char*); + + try { + cla::parser P; + + P << cla::named_parameter( "abcd" ); + + P.parse( argc, argv ); + + std::cout << "abcd = " << P.get( "abcd" ) << std::endl; + } + catch( rt::logic_error const& ex ) { + std::cout << "Logic error: " << ex.msg() << std::endl; + return -1; + } + + return 0; +} + +// EOF diff --git a/example/cla/char_parameter.cpp b/example/cla/char_parameter.cpp new file mode 100755 index 00000000..a64dd424 --- /dev/null +++ b/example/cla/char_parameter.cpp @@ -0,0 +1,41 @@ +// (C) Copyright Gennadiy Rozental 2001-2004. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// See http://www.boost.org/libs/test for the library home page. + +// Boost.Runtime.Param +#include +#include + +namespace rt = boost::runtime; +namespace cla = boost::runtime::cla; + +// STL +#include + +int main() { + char* argv[] = { "basic", "-k", "14", "-l", "debug" }; + int argc = sizeof(argv)/sizeof(char*); + + try { + cla::parser P; + + P << cla::char_parameter( 'k' ) + << cla::char_parameter( 'l' ); + + P.parse( argc, argv ); + + std::cout << "k = " << P.get( "k" ) << std::endl; + std::cout << "l = " << P.get( "l" ) << std::endl; + } + catch( rt::logic_error const& ex ) { + std::cout << "Logic error: " << ex.msg() << std::endl; + return -1; + } + + return 0; +} + +// EOF diff --git a/example/cla/custom_handler.cpp b/example/cla/custom_handler.cpp new file mode 100755 index 00000000..8f2903b0 --- /dev/null +++ b/example/cla/custom_handler.cpp @@ -0,0 +1,45 @@ +// (C) Copyright Gennadiy Rozental 2001-2004. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// See http://www.boost.org/libs/test for the library home page. + +// Boost.Runtime.Param +#include +#include + +namespace rt = boost::runtime; +namespace cla = boost::runtime::cla; + +// STL +#include + +void validate_abcd( cla::parameter const&, int& value ) +{ + if( value < 0 ) + value += 40; +} + +int main() { + char* argv[] = { "basic", "-abcd", "-25" }; + int argc = sizeof(argv)/sizeof(char*); + + try { + cla::parser P; + + P << cla::named_parameter( "abcd" ) - (cla::handler = &validate_abcd); + + P.parse( argc, argv ); + + std::cout << "abcd = " << P.get( "abcd" ) << std::endl; + } + catch( rt::logic_error const& ex ) { + std::cout << "Logic error: " << ex.msg() << std::endl; + return -1; + } + + return 0; +} + +// EOF diff --git a/example/cla/custom_interpreter.cpp b/example/cla/custom_interpreter.cpp new file mode 100755 index 00000000..65842721 --- /dev/null +++ b/example/cla/custom_interpreter.cpp @@ -0,0 +1,54 @@ +// (C) Copyright Gennadiy Rozental 2001-2004. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// See http://www.boost.org/libs/test for the library home page. + +// Boost.Runtime.Param +#include +#include + +namespace rt = boost::runtime; +namespace cla = boost::runtime::cla; + +// STL +#include + +void +named_integer( rt::cla::argv_traverser& source, boost::optional& value ) +{ + if( source.token() == "one" ) { + value = 1; + source.next_token(); + } + else if( source.token() == "two" ) { + value = 2; + source.next_token(); + } + else + value = 0; +} + +int main() { + char* argv[] = { "basic", "-abcd", "one" }; + int argc = sizeof(argv)/sizeof(char*); + + try { + cla::parser P; + + P << cla::named_parameter( "abcd" ) - (cla::interpreter = &named_integer); + + P.parse( argc, argv ); + + std::cout << "abcd = " << P.get( "abcd" ) << std::endl; + } + catch( rt::logic_error const& ex ) { + std::cout << "Logic error: " << ex.msg() << std::endl; + return -1; + } + + return 0; +} + +// EOF diff --git a/example/cla/custom_parameter.cpp b/example/cla/custom_parameter.cpp new file mode 100755 index 00000000..1770ff21 --- /dev/null +++ b/example/cla/custom_parameter.cpp @@ -0,0 +1,290 @@ +// (C) Copyright Gennadiy Rozental 2001-2004. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// See http://www.boost.org/libs/test for the library home page. + +// Boost.Runtime.Param +#include +#include + +namespace rt = boost::runtime; +namespace cla = boost::runtime::cla; + +// STL +#include +#include + +//_____________________________________________________________________// + +struct Point : std::pair { + bool parse( rt::cstring& in ) { + in.trim_left(); + + if( first_char( in ) != '(' ) + return false; + + in.trim_left( 1 ); + rt::cstring::size_type pos = in.find( ")" ); + + if( pos == rt::cstring::npos ) + return false; + + rt::cstring ss( in.begin(), pos ); + pos = ss.find( "," ); + + if( pos == rt::cstring::npos ) + return false; + + rt::cstring f( ss.begin(), pos ); + rt::cstring s( ss.begin()+pos+1, ss.end() ); + + f.trim(); + s.trim(); + + try { + first = boost::lexical_cast( f ); + second = boost::lexical_cast( s ); + } + catch( boost::bad_lexical_cast const& ) { + return false; + } + + in.trim_left( ss.end()+1 ); + return true; + } +}; + +std::ostream& operator<<( std::ostream& ostr, Point const& p ) +{ + ostr << '(' << p.first << ',' << p.second << ')'; + + return ostr; +} + +struct Segment : std::pair { + bool parse( rt::cstring& in ) { + in.trim_left(); + + if( first_char( in ) != '[' ) + return false; + + in.trim_left( 1 ); + + if( !first.parse( in ) ) + return false; + + in.trim_left(); + + if( first_char( in ) != ',' ) + return false; + + in.trim_left( 1 ); + + if( !second.parse( in ) ) + return false; + + in.trim_left(); + + if( first_char( in ) != ']' ) + return false; + + in.trim_left( 1 ); + + return true; + } +}; + +std::ostream& operator<<( std::ostream& ostr, Segment const& p ) +{ + ostr << '[' << p.first << ',' << p.second << ']'; + + return ostr; +} + +struct Circle : std::pair { + bool parse( rt::cstring& in ) { + in.trim_left(); + + if( first_char( in ) != '[' ) + return false; + + in.trim_left( 1 ); + + if( !first.parse( in ) ) + return false; + + in.trim_left(); + + if( first_char( in ) != ',' ) + return false; + + in.trim_left( 1 ); + + rt::cstring::size_type pos = in.find( "]" ); + + if( pos == rt::cstring::npos ) + return false; + + rt::cstring ss( in.begin(), pos ); + ss.trim(); + + try { + second = boost::lexical_cast( ss ); + } + catch( boost::bad_lexical_cast const& ) { + return false; + } + + in.trim_left( pos+1 ); + + return true; + } +}; + +std::ostream& operator<<( std::ostream& ostr, Circle const& p ) +{ + ostr << '[' << p.first << ',' << p.second << ']'; + + return ostr; +} + +//_____________________________________________________________________// + +template +class ShapeIdPolicy : public cla::identification_policy { + rt::cstring m_name; + rt::cstring m_usage_str; +public: + explicit ShapeIdPolicy( rt::cstring name ) + : cla::identification_policy( boost::rtti::type_id >() ) + , m_name( name ) {} + + virtual bool responds_to( rt::cstring name ) const { return m_name == name; } + virtual bool conflict_with( cla::identification_policy const& ) const { return false; } + virtual rt::cstring id_2_report() const { return m_name; } + virtual void usage_info( rt::format_stream& fs ) const { fs << m_name; } + + virtual bool matching( cla::parameter const& p, cla::argv_traverser& tr, bool primary ) const + { + T s; + + rt::cstring in = tr.input(); + return s.parse( in ); + } +}; + +//_____________________________________________________________________// + +template +class ShapeArgumentFactory : public cla::argument_factory { + rt::cstring m_usage_str; +public: + explicit ShapeArgumentFactory( rt::cstring usage ) : m_usage_str( usage ) {} + + // Argument factory interface + virtual rt::argument_ptr produce_using( cla::parameter& p, cla::argv_traverser& tr ) + { + T s; + + rt::cstring in = tr.input(); + s.parse( in ); + tr.trim( in.begin() - tr.input().begin() ); + + if( !p.actual_argument() ) { + rt::argument_ptr res; + + rt::typed_argument >* new_arg = new rt::typed_argument >( p ); + + new_arg->p_value.value.push_back( s ); + res.reset( new_arg ); + + return res; + } + else { + std::list& arg_values = rt::arg_value >( *p.actual_argument() ); + arg_values.push_back( s ); + + return p.actual_argument(); + } + } + virtual rt::argument_ptr produce_using( cla::parameter& p, cla::parser const& ) { return rt::argument_ptr(); } + virtual void argument_usage_info( rt::format_stream& fs ) { fs << m_usage_str; } +}; + +//_____________________________________________________________________// + +struct SegmentParam : cla::parameter { + SegmentParam() + : cla::parameter( m_id_policy, m_arg_factory ) + , m_id_policy( "segment" ) + , m_arg_factory( ":((P1x,P1y), (P2x,P2y)) ... ((P1x,P1y), (P2x,P2y))" ) + {} + + ShapeIdPolicy m_id_policy; + ShapeArgumentFactory m_arg_factory; +}; + +inline boost::shared_ptr +segment_param() { return boost::shared_ptr( new SegmentParam ); } + +//_____________________________________________________________________// + +struct CircleParam : cla::parameter { + CircleParam() + : cla::parameter( m_id_policy, m_arg_factory ) + , m_id_policy( "circle" ) + , m_arg_factory( ":((P1x,P1y), R) ... ((P1x,P1y), R)" ) + {} + + ShapeIdPolicy m_id_policy; + ShapeArgumentFactory m_arg_factory; +}; + +inline boost::shared_ptr +circle_param() { return boost::shared_ptr( new CircleParam ); } + +//_____________________________________________________________________// + +int main() { + char* argv[] = { "basic", "[(1,", "1)", ",", "(7,", "-1", ")]", "[(", "1,1)", ",7", "]", "[(3,", "1", ")", ",", "2]", + "[", "(2,7", "),", "(5", ",1", ")]" }; + int argc = sizeof(argv)/sizeof(char*); + + try { + cla::parser P; + + P << circle_param() - cla::optional + << segment_param() - cla::optional; + + P.parse( argc, argv ); + + boost::optional > segments; + boost::optional > circles; + + P.get( "segment", segments ); + + if( segments ) { + std::cout << "segments : "; + std::copy( segments->begin(), segments->end(), std::ostream_iterator( std::cout, "; " ) ); + std::cout << std::endl; + } + + P.get( "circle", circles ); + + if( circles ) { + std::cout << "circles : "; + std::copy( circles->begin(), circles->end(), std::ostream_iterator( std::cout, "; " ) ); + std::cout << std::endl; + } + } + catch( rt::logic_error const& ex ) { + std::cout << "Logic error: " << ex.msg() << std::endl; + return -1; + } + + return 0; +} + +// EOF diff --git a/example/cla/default_value.cpp b/example/cla/default_value.cpp new file mode 100755 index 00000000..037723bd --- /dev/null +++ b/example/cla/default_value.cpp @@ -0,0 +1,39 @@ +// (C) Copyright Gennadiy Rozental 2001-2004. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// See http://www.boost.org/libs/test for the library home page. + +// Boost.Runtime.Param +#include +#include + +namespace rt = boost::runtime; +namespace cla = boost::runtime::cla; + +// STL +#include + +int main() { + char* argv[] = { "basic" }; + int argc = sizeof(argv)/sizeof(char*); + + try { + cla::parser P; + + P << cla::named_parameter( "abcd" ) - ( cla::default_value = 10 ); + + P.parse( argc, argv ); + + std::cout << "abcd = " << P.get( "abcd" ) << std::endl; + } + catch( rt::logic_error const& ex ) { + std::cout << "Logic error: " << ex.msg() << std::endl; + return -1; + } + + return 0; +} + +// EOF diff --git a/example/cla/dual_name.cpp b/example/cla/dual_name.cpp new file mode 100755 index 00000000..b340b73a --- /dev/null +++ b/example/cla/dual_name.cpp @@ -0,0 +1,42 @@ +// (C) Copyright Gennadiy Rozental 2001-2004. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// See http://www.boost.org/libs/test for the library home page. + +// Boost.Runtime.Param +#include +#include + +namespace rt = boost::runtime; +namespace cla = boost::runtime::cla; + +// STL +#include + +int main() { + char* argv[] = { "basic", "/abcd", "25", "-k=1" }; + int argc = sizeof(argv)/sizeof(char*); + + try { + cla::parser P; + + P << cla::dual_name_parameter( "abcd|a" ) - (cla::prefix = "/|-") + << cla::dual_name_parameter() - (cla::name = "klmn|k", cla::separator = "|="); + + P.parse( argc, argv ); + + std::cout << "abcd = " << P.get( "abcd" ) << std::endl; + + std::cout << "klmn = " << P.get( "k" ) << std::endl; + } + catch( rt::logic_error const& ex ) { + std::cout << "Logic error: " << ex.msg() << std::endl; + return -1; + } + + return 0; +} + +// EOF diff --git a/example/cla/global.cpp b/example/cla/global.cpp new file mode 100755 index 00000000..bc819817 --- /dev/null +++ b/example/cla/global.cpp @@ -0,0 +1,46 @@ +// (C) Copyright Gennadiy Rozental 2001-2004. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// See http://www.boost.org/libs/test for the library home page. + +// Boost.Runtime.Param +#include +#include +#include + +namespace rt = boost::runtime; +namespace cla = boost::runtime::cla; + +// STL +#include + +int main() { + char* argv[] = { "basic", "--abcd=25", "1.23", "--klmn", "wert" }; + int argc = sizeof(argv)/sizeof(char*); + + try { + cla::parser P; + float f; + + P - (cla::prefix = "--", cla::guess_name) + << cla::named_parameter( "abcd" ) - (cla::separator = "=") + << cla::named_parameter( "klmn" ) + << cla::positional_parameter() - (cla::assign_to = f); + + P.parse( argc, argv ); + + std::cout << "abcd = " << P.get( "abcd" ) << std::endl; + std::cout << "klmn = " << P.get( "klmn" ) << std::endl; + std::cout << "f = " << f << std::endl; + } + catch( rt::logic_error const& ex ) { + std::cout << "Logic error: " << ex.msg() << std::endl; + return -1; + } + + return 0; +} + +// EOF diff --git a/example/cla/guess_name.cpp b/example/cla/guess_name.cpp new file mode 100755 index 00000000..a7108c83 --- /dev/null +++ b/example/cla/guess_name.cpp @@ -0,0 +1,39 @@ +// (C) Copyright Gennadiy Rozental 2001-2004. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// See http://www.boost.org/libs/test for the library home page. + +// Boost.Runtime.Param +#include +#include + +namespace rt = boost::runtime; +namespace cla = boost::runtime::cla; + +// STL +#include + +int main() { + char* argv[] = { "basic", "-a", "25" }; + int argc = sizeof(argv)/sizeof(char*); + + try { + cla::parser P; + + P << cla::named_parameter( "abcd" ) - cla::guess_name; + + P.parse( argc, argv ); + + std::cout << "abcd = " << P.get( "ab" ) << std::endl; + } + catch( rt::logic_error const& ex ) { + std::cout << "Logic error: " << ex.msg() << std::endl; + return -1; + } + + return 0; +} + +// EOF diff --git a/example/cla/help.cpp b/example/cla/help.cpp new file mode 100755 index 00000000..5c114bd8 --- /dev/null +++ b/example/cla/help.cpp @@ -0,0 +1,43 @@ +// (C) Copyright Gennadiy Rozental 2001-2004. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// See http://www.boost.org/libs/test for the library home page. + +// Boost.Runtime.Param +#include +#include +#include + +namespace rt = boost::runtime; +namespace cla = boost::runtime::cla; + +// STL +#include + +int main() { + char* argv[] = { "/usr/local/bin/basic1", }; + int argc = sizeof(argv)/sizeof(char*); + + cla::parser P; + + try { + P - (cla::prefix = "--", cla::separator = "=") + << cla::dual_name_parameter( "quiet|q" ) - (cla::description = "verbosity level") + << cla::named_parameter( "hostname" ) - (cla::guess_name, cla::description = "ABC server host name") + << cla::named_parameter( "port" ) - (cla::guess_name, cla::description = "ABC server port") + << cla::dual_name_parameter( "help|?" ) - (cla::description = "this help message"); + + P.parse( argc, argv ); + } + catch( rt::logic_error const& ex ) { + std::cout << "Logic error: " << ex.msg() << std::endl; + P.help( std::cout ); + return -1; + } + + return 0; +} + +// EOF diff --git a/example/cla/ignore_mismatch.cpp b/example/cla/ignore_mismatch.cpp new file mode 100755 index 00000000..e9aee7ee --- /dev/null +++ b/example/cla/ignore_mismatch.cpp @@ -0,0 +1,40 @@ +// (C) Copyright Gennadiy Rozental 2001-2004. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// See http://www.boost.org/libs/test for the library home page. + +// Boost.Runtime.Param +#include +#include + +namespace rt = boost::runtime; +namespace cla = boost::runtime::cla; + +// STL +#include + +int main() { + char* argv[] = { "basic", "-abc", "25", "-abcd", "12" }; + int argc = sizeof(argv)/sizeof(char*); + + try { + cla::parser P; + + P - cla::ignore_mismatch + << cla::named_parameter( "abcd" ); + + P.parse( argc, argv ); + + std::cout << "abcd = " << P.get( "abcd" ) << std::endl; + } + catch( rt::logic_error const& ex ) { + std::cout << "Logic error: " << ex.msg() << std::endl; + return -1; + } + + return 0; +} + +// EOF diff --git a/example/cla/input_separator.cpp b/example/cla/input_separator.cpp new file mode 100755 index 00000000..23dc90ca --- /dev/null +++ b/example/cla/input_separator.cpp @@ -0,0 +1,62 @@ +// (C) Copyright Gennadiy Rozental 2001-2004. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// See http://www.boost.org/libs/test for the library home page. + +// Boost.Runtime.Param +#include +#include + +namespace rt = boost::runtime; +namespace cla = boost::runtime::cla; + +// STL +#include + +struct FullName +{ + std::string first_name; + std::string last_name; +}; + +std::istream& +operator>>( std::istream& istr, FullName& f ) +{ + return istr >> f.first_name >> std::ws >> f.last_name; +} + +std::ostream& +operator<<( std::ostream& ostr, FullName const& f ) +{ + return ostr << f.first_name << ' ' << f.last_name; +} + +int main() { + char* argv[] = { "basic", "name=John Doe:name=Joan Doe:name=Your Name" }; + int argc = sizeof(argv)/sizeof(char*); + + try { + cla::parser P; + + P - ( cla::input_separator = ':' ) + << cla::named_parameter( "name" ) - ( cla::multiplicable, cla::separator = "=", cla::prefix = "" ); + + P.parse( argc, argv ); + + std::list arg_values = P.get >( "name" ); + + std::cout << "names: "; + std::copy( arg_values.begin(), arg_values.end(), std::ostream_iterator( std::cout, ", " ) ); + std::cout << std::endl; + } + catch( rt::logic_error const& ex ) { + std::cout << "Logic error: " << ex.msg() << std::endl; + return -1; + } + + return 0; +} + +// EOF diff --git a/example/cla/multiplicable.cpp b/example/cla/multiplicable.cpp new file mode 100755 index 00000000..dc147fe5 --- /dev/null +++ b/example/cla/multiplicable.cpp @@ -0,0 +1,44 @@ +// (C) Copyright Gennadiy Rozental 2001-2004. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// See http://www.boost.org/libs/test for the library home page. + +// Boost.Runtime.Param +#include +#include + +namespace rt = boost::runtime; +namespace cla = boost::runtime::cla; + +// STL +#include +#include + +int main() { + char* argv[] = { "basic", "-abcd", "25", "-abcd", "20", "-abcd", "15", "-abcd", "10", "-abcd", "5" }; + int argc = sizeof(argv)/sizeof(char*); + + try { + cla::parser P; + + P << cla::named_parameter( "abcd" ) - cla::multiplicable; + + P.parse( argc, argv ); + + std::list arg_values = P.get< std::list >( "abcd" ); + + std::cout << "abcd = "; + std::copy( arg_values.begin(), arg_values.end(), std::ostream_iterator( std::cout, " " ) ); + std::cout << std::endl; + } + catch( rt::logic_error const& ex ) { + std::cout << "Logic error: " << ex.msg() << std::endl; + return -1; + } + + return 0; +} + +// EOF diff --git a/example/cla/name.cpp b/example/cla/name.cpp new file mode 100755 index 00000000..b67aa363 --- /dev/null +++ b/example/cla/name.cpp @@ -0,0 +1,39 @@ +// (C) Copyright Gennadiy Rozental 2001-2004. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// See http://www.boost.org/libs/test for the library home page. + +// Boost.Runtime.Param +#include +#include + +namespace rt = boost::runtime; +namespace cla = boost::runtime::cla; + +// STL +#include + +int main() { + char* argv[] = { "basic", "-abcd", "-0.12" }; + int argc = sizeof(argv)/sizeof(char*); + + try { + cla::parser P; + + P << cla::named_parameter() - (cla::name = "abcd"); + + P.parse( argc, argv ); + + std::cout << "abcd = " << P.get( "abcd" ) << std::endl; + } + catch( rt::logic_error const& ex ) { + std::cout << "Logic error: " << ex.msg() << std::endl; + return -1; + } + + return 0; +} + +// EOF diff --git a/example/cla/optional.cpp b/example/cla/optional.cpp new file mode 100755 index 00000000..49a48f30 --- /dev/null +++ b/example/cla/optional.cpp @@ -0,0 +1,66 @@ +// (C) Copyright Gennadiy Rozental 2001-2004. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// See http://www.boost.org/libs/test for the library home page. + +// Boost.Runtime.Param +#include +#include + +namespace rt = boost::runtime; +namespace cla = boost::runtime::cla; + +// STL +#include + +int main() { + char* argv[] = { "basic", "-abc", "25" }; + int argc = 1; + + cla::parser P; + + try { + P << cla::named_parameter( "abcd" ) - cla::optional; + + P.parse( argc, argv ); + + if( P["abcd"] ) + std::cout << "abcd = " << P.get( "abcd" ) << std::endl; + else + std::cout << "not present" << std::endl; + + std::string s = P.get( "abcd" ); + + } + catch( rt::logic_error const& ex ) { + std::cout << "Logic error: " << ex.msg() << std::endl; + return -1; + } + + /////////////////////////////////////////////////////////// + + try { + cla::parser P1; + + P1 << cla::named_parameter( "abc" ) - cla::optional; + + argc = sizeof(argv)/sizeof(char*); + + P1.parse( argc, argv ); + + if( P1["abc"] ) + std::cout << "abc = " << P1.get( "abc" ) << std::endl; + else + std::cout << "not present" << std::endl; + } + catch( rt::logic_error const& ex ) { + std::cout << "Logic error: " << ex.msg() << std::endl; + return -1; + } + + return 0; +} + +// EOF diff --git a/example/cla/optional_value.cpp b/example/cla/optional_value.cpp new file mode 100755 index 00000000..a2c189d0 --- /dev/null +++ b/example/cla/optional_value.cpp @@ -0,0 +1,107 @@ +// (C) Copyright Gennadiy Rozental 2001-2004. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// See http://www.boost.org/libs/test for the library home page. + +// Boost.Runtime.Param +#include +#include + +namespace rt = boost::runtime; +namespace cla = boost::runtime::cla; + +// STL +#include +#include + +namespace boost { + +template +inline std::ostream& +operator<<( std::ostream& ostr, boost::optional const& v ) +{ + if( v ) + ostr << *v; + + return ostr; +} + +} + +int main() { + { + char* argv[] = { "basic", "-abcd", "-klmn", "10" }; + int argc = sizeof(argv)/sizeof(char*); + + try { + cla::parser P; + + P << cla::named_parameter( "abcd" ) - cla::optional_value + << cla::named_parameter( "klmn" ); + + P.parse( argc, argv ); + + boost::optional v = P.get >( "abcd" ); + + std::cout << "abcd " << ( v ? "present" : "not present" ) << std::endl; + std::cout << "klmn = " << P.get( "klmn" ) << std::endl; + } + catch( rt::logic_error const& ex ) { + std::cout << "Logic error: " << ex.msg() << std::endl; + return -1; + } + } + { + char* argv[] = { "basic", "-abcd", "25", "-klmn", "10" }; + int argc = sizeof(argv)/sizeof(char*); + + try { + cla::parser P; + + P << cla::named_parameter( "abcd" ) - cla::optional_value + << cla::named_parameter( "klmn" ); + + P.parse( argc, argv ); + + boost::optional v = P.get >( "abcd" ); + + if( v ) + std::cout << "*******\n""abcd = " << *v << std::endl; + + std::cout << "klmn = " << P.get( "klmn" ) << std::endl; + } + catch( rt::logic_error const& ex ) { + std::cout << "Logic error: " << ex.msg() << std::endl; + return -1; + } + } + { + char* argv[] = { "basic", "-abcd", "25", "-abcd", "-abcd", "10" }; + int argc = sizeof(argv)/sizeof(char*); + + try { + cla::parser P; + + P << cla::named_parameter( "abcd" ) - (cla::optional_value, cla::multiplicable); + + P.parse( argc, argv ); + + typedef std::list > value_type; + value_type v = P.get( "abcd" ); + + std::cout << "*******\n""abcd = "; + std::copy( v.begin(), v.end(), std::ostream_iterator >( std::cout, ", " ) ); + std::cout << std::endl; + } + catch( rt::logic_error const& ex ) { + std::cout << "Logic error: " << ex.msg() << std::endl; + return -1; + } + } + + return 0; +} + +// EOF diff --git a/example/cla/positional.cpp b/example/cla/positional.cpp new file mode 100755 index 00000000..ee9149ff --- /dev/null +++ b/example/cla/positional.cpp @@ -0,0 +1,46 @@ +// (C) Copyright Gennadiy Rozental 2001-2004. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// See http://www.boost.org/libs/test for the library home page. + +// Boost.Runtime.Param +#include +#include + +namespace rt = boost::runtime; +namespace cla = boost::runtime::cla; + +// STL +#include + +int main() { + char* argv[] = { "basic", "--klmn-- 14" }; + int argc = sizeof(argv)/sizeof(char*); + + try { + cla::parser P; + int i; + + P << cla::positional_parameter( "abcd" ) + << cla::positional_parameter() - (cla::assign_to = i) + << cla::positional_parameter( "klmn" ) - (cla::default_value = std::string("file")) + << cla::positional_parameter( "oprt" ) - cla::optional; + + P.parse( argc, argv ); + + std::cout << "abcd = " << P.get( "abcd" ) << std::endl; + std::cout << "i = " << i << std::endl; + std::cout << "klmn = " << P.get( "klmn" ) << std::endl; + std::cout << "oprt " << (P["oprt"] ? "present" : "not present" ) << std::endl; + } + catch( rt::logic_error const& ex ) { + std::cout << "Logic error: " << ex.msg() << std::endl; + return -1; + } + + return 0; +} + +// EOF diff --git a/example/cla/prefix.cpp b/example/cla/prefix.cpp new file mode 100755 index 00000000..2e5405f1 --- /dev/null +++ b/example/cla/prefix.cpp @@ -0,0 +1,41 @@ +// (C) Copyright Gennadiy Rozental 2001-2004. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// See http://www.boost.org/libs/test for the library home page. + +// Boost.Runtime.Param +#include +#include + +namespace rt = boost::runtime; +namespace cla = boost::runtime::cla; + +// STL +#include + +int main() { + char* argv[] = { "basic", "/abcd", "25", "-klmn", "abs" }; + int argc = sizeof(argv)/sizeof(char*); + + try { + cla::parser P; + + P << cla::named_parameter( "abcd" ) - ( cla::prefix = "/" ) + << cla::named_parameter( "klmn" ); + + P.parse( argc, argv ); + + std::cout << "abcd = " << P.get( "abcd" ) << std::endl; + std::cout << "klnm = " << P.get( "klmn" ) << std::endl; + } + catch( rt::logic_error const& ex ) { + std::cout << "Logic error: " << ex.msg() << std::endl; + return -1; + } + + return 0; +} + +// EOF diff --git a/example/cla/reference.cpp b/example/cla/reference.cpp new file mode 100755 index 00000000..463fec78 --- /dev/null +++ b/example/cla/reference.cpp @@ -0,0 +1,40 @@ +// (C) Copyright Gennadiy Rozental 2001-2004. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// See http://www.boost.org/libs/test for the library home page. + +// Boost.Runtime.Param +#include +#include + +namespace rt = boost::runtime; +namespace cla = boost::runtime::cla; + +// STL +#include + +int main() { + char* argv[] = { "basic", "-qwe", "25" }; + int argc = sizeof(argv)/sizeof(char*); + + try { + cla::parser P; + + P << cla::named_parameter( "abcd" ) - (cla::default_refer_to = "qwe") + << cla::named_parameter( "qwe" ); + + P.parse( argc, argv ); + + std::cout << "abcd = " << P.get( "abcd" ) << std::endl; + } + catch( rt::logic_error const& ex ) { + std::cout << "Logic error: " << ex.msg() << std::endl; + return -1; + } + + return 0; +} + +// EOF diff --git a/example/cla/remainder.cpp b/example/cla/remainder.cpp new file mode 100755 index 00000000..163c37a4 --- /dev/null +++ b/example/cla/remainder.cpp @@ -0,0 +1,46 @@ +// (C) Copyright Gennadiy Rozental 2001-2004. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// See http://www.boost.org/libs/test for the library home page. + +// Boost.Runtime.Param +#include +#include + +namespace rt = boost::runtime; +namespace cla = boost::runtime::cla; + +// STL +#include + +int main() { + char* argv[] = { "basic", "-abd", "as", "-cd", "aswdf", "--da", "25", "-ffd", "(fgh,", "asd)" }; + int argc = sizeof(argv)/sizeof(char*); + + try { + cla::parser P; + + P - cla::ignore_mismatch + << cla::named_parameter( "da" ) - ( cla::prefix = "--" ) + << cla::named_parameter( "abd" ); + + P.parse( argc, argv ); + + std::cout << "da = " << P.get( "da" ) << std::endl; + std::cout << "abd = " << P.get( "abd" ) << std::endl; + + std::cout << "argc = " << argc << std::endl; + for( int i = 0; i +#include + +namespace rt = boost::runtime; +namespace cla = boost::runtime::cla; + +// STL +#include + +int main() { + char* argv[] = { "basic", "-abcd=25" }; + int argc = sizeof(argv)/sizeof(char*); + + try { + cla::parser P; + + P << cla::named_parameter( "abcd" ) - ( cla::separator = "=" ); + + P.parse( argc, argv ); + + std::cout << "abcd = " << P.get( "abcd" ) << std::endl; + } + catch( rt::logic_error const& ex ) { + std::cout << "Logic error: " << ex.msg() << std::endl; + return -1; + } + + return 0; +} + +// EOF diff --git a/example/cla/usage.cpp b/example/cla/usage.cpp new file mode 100755 index 00000000..b230d99a --- /dev/null +++ b/example/cla/usage.cpp @@ -0,0 +1,45 @@ +// (C) Copyright Gennadiy Rozental 2001-2004. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// See http://www.boost.org/libs/test for the library home page. + +// Boost.Runtime.Param +#include +#include +#include + +namespace rt = boost::runtime; +namespace cla = boost::runtime::cla; + +// STL +#include + +int main() { + char* argv[] = { "..\\..\\src\\basic1" }; + int argc = sizeof(argv)/sizeof(char*); + + cla::parser P; + + try { + P << cla::named_parameter( "ab" ) + << cla::named_parameter( "cd" ) - (cla::prefix = "--",cla::separator = "=", cla::optional) + << cla::dual_name_parameter( "klnm|k" ) + << cla::named_parameter( "op" ) - cla::multiplicable + << cla::named_parameter >( "rs" ) + << cla::named_parameter( "tu" ) + << cla::named_parameter( "yu" ) - cla::optional_value; + + P.parse( argc, argv ); + } + catch( rt::logic_error const& ex ) { + std::cout << "Logic error: " << ex.msg() << std::endl; + P.usage( std::cout ); + return -1; + } + + return 0; +} + +// EOF diff --git a/example/cla/validation/Jamfile b/example/cla/validation/Jamfile new file mode 100755 index 00000000..1e6335d5 --- /dev/null +++ b/example/cla/validation/Jamfile @@ -0,0 +1,31 @@ +# (C) Copyright Gennadiy Rozental 2001-2005. +# Use, modification, and distribution are subject to the +# Boost Software License, Version 1.0. (See accompanying file +# LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +# +# See http://www.boost.org/libs/test for the library home page. + +subproject libs/test/example/cla/validation ; + +DEPENDS test : all ; + +rule boost-runtime-param-example ( example-name ) +{ + exe $(example-name) : $(example-name).cpp + : $(BOOST_ROOT) + on + <*>-w-8080 ; +} + +boost-runtime-param-example access_unknown ; +boost-runtime-param-example ambiguous_access ; +boost-runtime-param-example ambiguous_input ; +boost-runtime-param-example definition_conflict ; +boost-runtime-param-example invalid_short_name ; +boost-runtime-param-example multiple_generators ; +boost-runtime-param-example multiple_value_handlers ; +boost-runtime-param-example opt_with_default_value ; +boost-runtime-param-example optional_plus_multiplicable ; +boost-runtime-param-example required_missing ; +boost-runtime-param-example unexpected_input ; +boost-runtime-param-example unexpected_repetition ; diff --git a/example/cla/validation/access_unknown.cpp b/example/cla/validation/access_unknown.cpp new file mode 100755 index 00000000..28e59bdf --- /dev/null +++ b/example/cla/validation/access_unknown.cpp @@ -0,0 +1,37 @@ +// (C) Copyright Gennadiy Rozental 2001-2004. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// See http://www.boost.org/libs/test for the library home page. + +// Boost.Runtime.Param +#include +#include + +namespace rt = boost::runtime; +namespace cla = boost::runtime::cla; + +// STL +#include + +int main() { + char* argv[] = { "basic", "-abcd", "25" }; + int argc = sizeof(argv)/sizeof(char*); + + try { + cla::parser P; + + P << cla::named_parameter( "abcd" ); + + P.parse( argc, argv ); + + P["ab"]; + } + catch( rt::logic_error const& ex ) { + std::cout << "Logic error: " << ex.msg() << std::endl; + return -1; + } + + return 0; +} diff --git a/example/cla/validation/ambiguous_access.cpp b/example/cla/validation/ambiguous_access.cpp new file mode 100755 index 00000000..156278a0 --- /dev/null +++ b/example/cla/validation/ambiguous_access.cpp @@ -0,0 +1,39 @@ +// (C) Copyright Gennadiy Rozental 2001-2004. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// See http://www.boost.org/libs/test for the library home page. + +// Boost.Runtime.Param +#include +#include + +namespace rt = boost::runtime; +namespace cla = boost::runtime::cla; + +// STL +#include + +int main() { + char* argv[] = { "basic", "-abc", "25" }; + int argc = sizeof(argv)/sizeof(char*); + + + try { + cla::parser P; + + P << cla::named_parameter( "abfv" ) - (cla::guess_name, cla::optional) + << cla::named_parameter( "abcd" ) - cla::guess_name; + + P.parse( argc, argv ); + + P["ab"]; + } + catch( rt::logic_error const& ex ) { + std::cout << "Logic error: " << ex.msg() << std::endl; + return -1; + } + + return 0; +} diff --git a/example/cla/validation/ambiguous_input.cpp b/example/cla/validation/ambiguous_input.cpp new file mode 100755 index 00000000..e666f24a --- /dev/null +++ b/example/cla/validation/ambiguous_input.cpp @@ -0,0 +1,36 @@ +// (C) Copyright Gennadiy Rozental 2001-2004. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// See http://www.boost.org/libs/test for the library home page. + +// Boost.Runtime.Param +#include +#include + +namespace rt = boost::runtime; +namespace cla = boost::runtime::cla; + +// STL +#include + +int main() { + char* argv[] = { "basic", "-ab", "25" }; + int argc = sizeof(argv)/sizeof(char*); + + try { + cla::parser P; + + P << cla::named_parameter( "abfv" ) - cla::guess_name + << cla::named_parameter( "abcd" ) - cla::guess_name; + + P.parse( argc, argv ); + } + catch( rt::logic_error const& ex ) { + std::cout << "Logic error: " << ex.msg() << std::endl; + return -1; + } + + return 0; +} diff --git a/example/cla/validation/definition_conflict.cpp b/example/cla/validation/definition_conflict.cpp new file mode 100755 index 00000000..6cb4050d --- /dev/null +++ b/example/cla/validation/definition_conflict.cpp @@ -0,0 +1,71 @@ +// (C) Copyright Gennadiy Rozental 2001-2004. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// See http://www.boost.org/libs/test for the library home page. + +// Boost.Runtime.Param +#include +#include +#include + +namespace rt = boost::runtime; +namespace cla = boost::runtime::cla; + +// STL +#include + +int main() { + char* argv[] = { "basic", "-abcd", "25" }; + int argc = sizeof(argv)/sizeof(char*); + + + try { + cla::parser P; + + P << cla::named_parameter( "abcd" ) + << cla::named_parameter( "abcd" ); + + P.parse( argc, argv ); + } + catch( rt::logic_error const& ex ) { + std::cout << "Logic error: " << ex.msg() << std::endl; + } + + try { + cla::parser P; + + P << cla::named_parameter( "abcd" ) - cla::guess_name + << cla::named_parameter( "ab" ); + + P.parse( argc, argv ); + } + catch( rt::logic_error const& ex ) { + std::cout << "Logic error: " << ex.msg() << std::endl; + } + try { + cla::parser P; + + P << cla::named_parameter( "abcd" ) - cla::guess_name + << cla::named_parameter( "abdf" ) - cla::guess_name; + + P.parse( argc, argv ); + } + catch( rt::logic_error const& ex ) { + std::cout << "Logic error: " << ex.msg() << std::endl; + } + try { + cla::parser P; + + P << cla::named_parameter( "abcd" ) - cla::guess_name + << cla::char_parameter( 'a' ); + + P.parse( argc, argv ); + } + catch( rt::logic_error const& ex ) { + std::cout << "Logic error: " << ex.msg() << std::endl; + } + + return 0; +} diff --git a/example/cla/validation/invalid_short_name.cpp b/example/cla/validation/invalid_short_name.cpp new file mode 100755 index 00000000..beed6ac3 --- /dev/null +++ b/example/cla/validation/invalid_short_name.cpp @@ -0,0 +1,33 @@ +// (C) Copyright Gennadiy Rozental 2001-2004. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// See http://www.boost.org/libs/test for the library home page. + +// Boost.Runtime.Param +#include +#include + +namespace rt = boost::runtime; +namespace cla = boost::runtime::cla; + +// STL +#include + +int main() { + char* argv[] = { "basic", "--klmn", "25" }; + int argc = sizeof(argv)/sizeof(char*); + + try { + cla::parser P; + + P << cla::dual_name_parameter() - (cla::name = "klmn|kl"); + } + catch( rt::logic_error const& ex ) { + std::cout << "Logic error: " << ex.msg() << std::endl; + return -1; + } + + return 0; +} diff --git a/example/cla/validation/multiple_generators.cpp b/example/cla/validation/multiple_generators.cpp new file mode 100755 index 00000000..2a51d634 --- /dev/null +++ b/example/cla/validation/multiple_generators.cpp @@ -0,0 +1,35 @@ +// (C) Copyright Gennadiy Rozental 2001-2004. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// See http://www.boost.org/libs/test for the library home page. + +// Boost.Runtime.Param +#include +#include + +namespace rt = boost::runtime; +namespace cla = boost::runtime::cla; + +// STL +#include + +int main() { + char* argv[] = { "basic", "-abcd", "25" }; + int argc = sizeof(argv)/sizeof(char*); + + cla::parser P; + + P << cla::named_parameter( "abcd" ) - ( cla::default_value = 1, cla::default_value = 2 ); + + try { + P.parse( argc, argv ); + } + catch( rt::logic_error const& ex ) { + std::cout << "Logic error: " << ex.msg() << std::endl; + return -1; + } + + return 0; +} diff --git a/example/cla/validation/multiple_value_handlers.cpp b/example/cla/validation/multiple_value_handlers.cpp new file mode 100755 index 00000000..92d77bca --- /dev/null +++ b/example/cla/validation/multiple_value_handlers.cpp @@ -0,0 +1,36 @@ +// (C) Copyright Gennadiy Rozental 2001-2004. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// See http://www.boost.org/libs/test for the library home page. + +// Boost.Runtime.Param +#include +#include + +namespace rt = boost::runtime; +namespace cla = boost::runtime::cla; + +// STL +#include + +int main() { + char* argv[] = { "basic", "-abcd", "25" }; + int argc = sizeof(argv)/sizeof(char*); + + try { + cla::parser P; + + int i, j; + + P << cla::named_parameter( "abcd" ) - (cla::assign_to = i,cla::assign_to = j) ; + // !! does not work + } + catch( rt::logic_error const& ex ) { + std::cout << "Logic error: " << ex.msg() << std::endl; + return -1; + } + + return 0; +} diff --git a/example/cla/validation/opt_with_default_value.cpp b/example/cla/validation/opt_with_default_value.cpp new file mode 100755 index 00000000..348a253f --- /dev/null +++ b/example/cla/validation/opt_with_default_value.cpp @@ -0,0 +1,35 @@ +// (C) Copyright Gennadiy Rozental 2001-2004. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// See http://www.boost.org/libs/test for the library home page. + +// Boost.Runtime.Param +#include +#include + +namespace rt = boost::runtime; +namespace cla = boost::runtime::cla; + +// STL +#include + +int main() { + char* argv[] = { "basic", "-abcd", "25" }; + int argc = sizeof(argv)/sizeof(char*); + + try { + cla::parser P; + + P << cla::named_parameter( "abcd" ) - ( cla::optional, cla::default_value = 5 ); + + P.parse( argc, argv ); + } + catch( rt::logic_error const& ex ) { + std::cout << "Logic error: " << ex.msg() << std::endl; + return -1; + } + + return 0; +} diff --git a/example/cla/validation/optional_plus_multiplicable.cpp b/example/cla/validation/optional_plus_multiplicable.cpp new file mode 100755 index 00000000..f4fd29cd --- /dev/null +++ b/example/cla/validation/optional_plus_multiplicable.cpp @@ -0,0 +1,80 @@ +// (C) Copyright Gennadiy Rozental 2001-2004. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// See http://www.boost.org/libs/test for the library home page. + +// Boost.Runtime.Param +#include +#include + +namespace rt = boost::runtime; +namespace cla = boost::runtime::cla; + +// STL +#include + +int main() { + { + char* argv[] = { "basic" }; + int argc = sizeof(argv)/sizeof(char*); + + try { + cla::parser P; + P << cla::named_parameter( "abcd" ) - (cla::optional, cla::multiplicable); + P.parse( argc, argv ); + + std::cout << "abcd = " << (P["abcd"] ? "present" : "not present" ); + std::cout << std::endl; + } + catch( rt::logic_error const& ex ) { + std::cout << "Logic error: " << ex.msg() << std::endl; + return -1; + } + } + + { + char* argv[] = { "basic", "-abcd", "25" }; + int argc = sizeof(argv)/sizeof(char*); + + try { + cla::parser P; + P << cla::named_parameter( "abcd" ) - (cla::optional, cla::multiplicable); + P.parse( argc, argv ); + + std::list arg_values = P.get >( "abcd" ); + + std::cout << "abcd = "; + std::copy( arg_values.begin(), arg_values.end(), std::ostream_iterator( std::cout, " " ) ); + std::cout << std::endl; + } + catch( rt::logic_error const& ex ) { + std::cout << "Logic error: " << ex.msg() << std::endl; + return -1; + } + } + + { + char* argv[] = { "basic", "-abcd", "25", "-abcd", "50" }; + int argc = sizeof(argv)/sizeof(char*); + + try { + cla::parser P; + P << cla::named_parameter( "abcd" ) - (cla::optional, cla::multiplicable); + P.parse( argc, argv ); + + std::list arg_values = P.get >( "abcd" ); + + std::cout << "abcd = "; + std::copy( arg_values.begin(), arg_values.end(), std::ostream_iterator( std::cout, " " ) ); + std::cout << std::endl; + } + catch( rt::logic_error const& ex ) { + std::cout << "Logic error: " << ex.msg() << std::endl; + return -1; + } + } + + return 0; +} diff --git a/example/cla/validation/required_missing.cpp b/example/cla/validation/required_missing.cpp new file mode 100755 index 00000000..c8c0a019 --- /dev/null +++ b/example/cla/validation/required_missing.cpp @@ -0,0 +1,35 @@ +// (C) Copyright Gennadiy Rozental 2001-2004. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// See http://www.boost.org/libs/test for the library home page. + +// Boost.Runtime.Param +#include +#include + +namespace rt = boost::runtime; +namespace cla = boost::runtime::cla; + +// STL +#include + +int main() { + char* argv[] = { "basic" }; + int argc = sizeof(argv)/sizeof(char*); + + try { + cla::parser P; + + P << cla::named_parameter( "abcd" ); + + P.parse( argc, argv ); + } + catch( rt::logic_error const& ex ) { + std::cout << "Logic error: " << ex.msg() << std::endl; + return -1; + } + + return 0; +} diff --git a/example/cla/validation/unexpected_input.cpp b/example/cla/validation/unexpected_input.cpp new file mode 100755 index 00000000..126084f8 --- /dev/null +++ b/example/cla/validation/unexpected_input.cpp @@ -0,0 +1,35 @@ +// (C) Copyright Gennadiy Rozental 2001-2004. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// See http://www.boost.org/libs/test for the library home page. + +// Boost.Runtime.Param +#include +#include + +namespace rt = boost::runtime; +namespace cla = boost::runtime::cla; + +// STL +#include + +int main() { + char* argv[] = { "basic", "-ab", "25" }; + int argc = sizeof(argv)/sizeof(char*); + + try { + cla::parser P; + + P << cla::named_parameter( "abcd" ); + + P.parse( argc, argv ); + } + catch( rt::logic_error const& ex ) { + std::cout << "Logic error: " << ex.msg() << std::endl; + return -1; + } + + return 0; +} diff --git a/example/cla/validation/unexpected_repetition.cpp b/example/cla/validation/unexpected_repetition.cpp new file mode 100755 index 00000000..a0bf262b --- /dev/null +++ b/example/cla/validation/unexpected_repetition.cpp @@ -0,0 +1,35 @@ +// (C) Copyright Gennadiy Rozental 2001-2004. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// See http://www.boost.org/libs/test for the library home page. + +// Boost.Runtime.Param +#include +#include + +namespace rt = boost::runtime; +namespace cla = boost::runtime::cla; + +// STL +#include + +int main() { + char* argv[] = { "basic", "-abcd", "25", "-abcd", "26" }; + int argc = sizeof(argv)/sizeof(char*); + + try { + cla::parser P; + + P << cla::named_parameter( "abcd" ); + + P.parse( argc, argv ); + } + catch( rt::logic_error const& ex ) { + std::cout << "Logic error: " << ex.msg() << std::endl; + return -1; + } + + return 0; +} diff --git a/example/cla/wide_string.cpp b/example/cla/wide_string.cpp new file mode 100755 index 00000000..7cceb304 --- /dev/null +++ b/example/cla/wide_string.cpp @@ -0,0 +1,40 @@ +// (C) Copyright Gennadiy Rozental 2001-2004. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// See http://www.boost.org/libs/test for the library home page. + +// Boost.Runtime.Param +#define BOOST_RT_PARAM_WIDE_STRING +#include +#include + +namespace rt = boost::wide_runtime; +namespace cla = boost::wide_runtime::cla; + +// STL +#include + +int main() { + wchar_t* argv[] = { L"basic", L"-рсту", L"25" }; + int argc = sizeof(argv)/sizeof(char*); + + try { + cla::parser P; + + P << cla::named_parameter( L"рсту" ); + + P.parse( argc, argv ); + + std::wcout << L"рсту = " << P.get( L"рсту" ) << std::endl; + } + catch( rt::logic_error const& ex ) { + std::wcout << L"Logic Error: " << ex.msg() << std::endl; + return -1; + } + + return 0; +} + +// EOF diff --git a/example/env/Jamfile b/example/env/Jamfile new file mode 100755 index 00000000..4d7cab5e --- /dev/null +++ b/example/env/Jamfile @@ -0,0 +1,25 @@ +# (C) Copyright Gennadiy Rozental 2001-2005. +# Use, modification, and distribution are subject to the +# Boost Software License, Version 1.0. (See accompanying file +# LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +# +# See http://www.boost.org/libs/test for the library home page. + +subproject libs/test/example/env ; + +DEPENDS test : all ; + +rule boost-runtime-param-example ( example-name ) +{ + exe $(example-name) : $(example-name).cpp + : $(BOOST_ROOT) + on + <*>-w-8080 ; +} + +boost-runtime-param-example custom_interpreter_env ; +boost-runtime-param-example env_var_default_value ; +boost-runtime-param-example environment_ex ; +boost-runtime-param-example global_id ; +boost-runtime-param-example modifier_combination ; +boost-runtime-param-example variable_ex ; diff --git a/example/env/custom_interpreter_env.cpp b/example/env/custom_interpreter_env.cpp new file mode 100755 index 00000000..9429cb41 --- /dev/null +++ b/example/env/custom_interpreter_env.cpp @@ -0,0 +1,42 @@ +// (C) Copyright Gennadiy Rozental 2001-2004. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// See http://www.boost.org/libs/test for the library home page. + +// Boost.Runtime.Param +#include +#include + +namespace rt = boost::runtime; +namespace env = boost::runtime::env; + +// STL +#include + +void +named_integer( rt::cstring source, boost::optional& value ) +{ + if( source == "one" ) + value = 1; + else if( source == "two" ) + value = 2; + else + value = 0; +} + +env::variable NUMBER1( "NUMBER1", env::interpreter = &named_integer ); + +int main() { + + std::cout << NUMBER1 << std::endl; + + std::cout << env::var( "NUMBER2", ( env::interpreter = &named_integer, env::default_value = 10 )) + << std::endl; + + return 0; +} + +// EOF + diff --git a/example/env/env_var_default_value.cpp b/example/env/env_var_default_value.cpp new file mode 100755 index 00000000..0620c55f --- /dev/null +++ b/example/env/env_var_default_value.cpp @@ -0,0 +1,27 @@ +// (C) Copyright Gennadiy Rozental 2001-2004. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// See http://www.boost.org/libs/test for the library home page. + +// Boost.Runtime.Param +#include +#include + +namespace rt = boost::runtime; +namespace env = boost::runtime::env; + +// STL +#include + +int main() { + env::variable TEMP( "abc", env::default_value = 5 ); + + std::cout << TEMP << std::endl; + + return 0; +} + +// EOF + diff --git a/example/env/environment_ex.cpp b/example/env/environment_ex.cpp new file mode 100755 index 00000000..58ec394d --- /dev/null +++ b/example/env/environment_ex.cpp @@ -0,0 +1,32 @@ +// (C) Copyright Gennadiy Rozental 2001-2004. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// See http://www.boost.org/libs/test for the library home page. + +// Boost.Runtime.Param +#include +#include + +namespace rt = boost::runtime; +namespace env = boost::runtime::env; + +// STL +#include + +int main() { + std::cout << env::get("NUMBER_OF_PROCESSORS") << '\n' << env::get("TEMP") << std::endl; + + boost::optional n; + + env::get( "NUMBER_OF_PROCESSORS", n ); + + if( n ) + std::cout << n << std::endl; + + return 0; +} + +// EOF + diff --git a/example/env/global_id.cpp b/example/env/global_id.cpp new file mode 100755 index 00000000..ab9395bb --- /dev/null +++ b/example/env/global_id.cpp @@ -0,0 +1,25 @@ +// (C) Copyright Gennadiy Rozental 2001-2004. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// See http://www.boost.org/libs/test for the library home page. + +// Boost.Runtime.Param +#include +#include + +namespace rt = boost::runtime; +namespace env = boost::runtime::env; + +// STL +#include + +int main() { + env::variable<> TEMP( "TEMP", env::global_id = "temp_dir_location" ); + + return 0; +} + +// EOF + diff --git a/example/env/modifier_combination.cpp b/example/env/modifier_combination.cpp new file mode 100755 index 00000000..e646d7da --- /dev/null +++ b/example/env/modifier_combination.cpp @@ -0,0 +1,27 @@ +// (C) Copyright Gennadiy Rozental 2001-2004. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// See http://www.boost.org/libs/test for the library home page. + +// Boost.Runtime.Param +#include +#include + +namespace rt = boost::runtime; +namespace env = boost::runtime::env; + +// STL +#include + +int main() { + env::variable TEMP( "abc", (env::global_id = "temp_dir_location", env::default_value = 5) ); + + std::cout << TEMP << std::endl; + + return 0; +} + +// EOF + diff --git a/example/env/validation/Jamfile b/example/env/validation/Jamfile new file mode 100755 index 00000000..b723d307 --- /dev/null +++ b/example/env/validation/Jamfile @@ -0,0 +1,20 @@ +# (C) Copyright Gennadiy Rozental 2001-2005. +# Use, modification, and distribution are subject to the +# Boost Software License, Version 1.0. (See accompanying file +# LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +# +# See http://www.boost.org/libs/test for the library home page. + +subproject libs/test/example/env/validation ; + +DEPENDS test : all ; + +rule boost-runtime-param-example ( example-name ) +{ + exe $(example-name) : $(example-name).cpp + : $(BOOST_ROOT) + on + <*>-w-8080 ; +} + +boost-runtime-param-example need_typed_access ; diff --git a/example/env/validation/need_typed_access.cpp b/example/env/validation/need_typed_access.cpp new file mode 100755 index 00000000..a3abc4c7 --- /dev/null +++ b/example/env/validation/need_typed_access.cpp @@ -0,0 +1,41 @@ +// (C) Copyright Gennadiy Rozental 2001-2004. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// See http://www.boost.org/libs/test for the library home page. + +// Boost.Runtime.Param +#include +#include + +namespace rt = boost::runtime; +namespace env = boost::runtime::env; + +// STL +#include + +// Boost.Runtime.Param +#include +namespace rt = boost::runtime; +namespace env = boost::runtime::environment; + +// Boost.Test +#include + +// STL +#include + +int main() { + try { + std::cout << env::var("ABC").value(); + } + catch ( rt::logic_error const& ex ) { + std::cout << ex.msg(); + } + + return 0; +} + +// EOF + diff --git a/example/env/validation/res b/example/env/validation/res new file mode 100644 index 00000000..d199b93c --- /dev/null +++ b/example/env/validation/res @@ -0,0 +1,18 @@ + +D:\Source Code\boost\libs\test\example\env\validation>rem msvc msvc-stlport gcc gcc-stlport borland cwpro8 vc7.1-stlport borland-5.6.4 intel-win32-8_1 mingw + +D:\Source Code\boost\libs\test\example\env\validation>set target=all + +D:\Source Code\boost\libs\test\example\env\validation>if "all" == "" (set target=test ) + +D:\Source Code\boost\libs\test\example\env\validation>bjam "-sTOOLS=intel-win32-8_1" "-sSTLPORT_VERSION=4.6.2" "-sBCC_564_ROOT=C:\Compilers\Borland\CBuilder6" "-sMSVC_ROOT=C:\Compilers\Microsoft Visual Studio\VC98" "-sSTLPORT_ROOT=D:\Source Code\STLport-4.6.2" "-sSTLPORT_PATH=D:\Source Code" "-sCW_ROOT=C:\Compilers\Metrowerks\CodeWarrior" "-sMINGW_ROOT_DIRECTORY=C:\Compilers\MinGW" "-sVC71_ROOT=C:\Compilers\Microsoft Visual Studio .NET 2003\Vc7" "-sINTEL_PATH=C:\Compilers\Intel\CPP\Compiler80\Ia32" "-sINTEL_BASE_MSVC_TOOLSET=vc-7_1" all +...found 309 targets... +...updating 6 targets... +MkDir1 ..\..\..\..\..\bin\boost\libs\test\example\env\validation\need_typed_access.exe\intel-win32-8_1 +MkDir1 ..\..\..\..\..\bin\boost\libs\test\example\env\validation\need_typed_access.exe\intel-win32-8_1\debug +MkDir1 ..\..\..\..\..\bin\boost\libs\test\example\env\validation\need_typed_access.exe\intel-win32-8_1\debug\threading-multi +vc-C++ ..\..\..\..\..\bin\boost\libs\test\example\env\validation\need_typed_access.exe\intel-win32-8_1\debug\threading-multi\need_typed_access.obj +need_typed_access.cpp +vc-Link ..\..\..\..\..\bin\boost\libs\test\example\env\validation\need_typed_access.exe\intel-win32-8_1\debug\threading-multi\need_typed_access.exe +xilink: executing 'link' +...updated 6 targets... diff --git a/example/env/variable_ex.cpp b/example/env/variable_ex.cpp new file mode 100755 index 00000000..1d2a5f57 --- /dev/null +++ b/example/env/variable_ex.cpp @@ -0,0 +1,67 @@ +// (C) Copyright Gennadiy Rozental 2001-2004. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// See http://www.boost.org/libs/test for the library home page. + +// Boost.Runtime.Param +#include +#include + +namespace rt = boost::runtime; +namespace env = boost::runtime::env; + +// STL +#include + +env::variable<> TEMP( "TEMP" ); + +env::variable ProcNumber( "NUMBER_OF_PROCESSORS" ); + +env::variable abc( "abccccc" ); + +void print_int( env::variable_base const& var ) { + std::cout << var.name() << (var.has_value() ? " is present: " : " is not present: " ) << var.value() << std::endl; +} + +int main() { + std::cout << TEMP << '\n' << ProcNumber << std::endl; + + rt::cstring val = TEMP.value(); + std::cout << " val=" << val << std::endl; + + int n = ProcNumber.value(); + + std::cout << " n=" << n << std::endl; + + boost::optional opt_n; + ProcNumber.value( opt_n ); + + std::cout << " n=" << opt_n << std::endl; + + print_int( ProcNumber ); + + if( ProcNumber == 1 ) + std::cout << "ProcNumber = 1\n"; + + if( 2 != ProcNumber ) + std::cout << "ProcNumber != 2\n"; + + if( abc != 1 ) + std::cout << "abc != 1\n"; + + abc = 1; + + if( abc == 1 ) + std::cout << "abc == 1\n"; + + TEMP = "../tmp"; + + std::cout << TEMP << std::endl; + + return 0; +} + +// EOF + diff --git a/example/named_param_example.cpp b/example/named_param_example.cpp new file mode 100755 index 00000000..12e298c7 --- /dev/null +++ b/example/named_param_example.cpp @@ -0,0 +1,120 @@ +// (C) Copyright Gennadiy Rozental 2001-2004. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// See http://www.boost.org/libs/test for the library home page. + +// Library Code +#include + +using namespace boost::nfp; + +//////////////////////////////////////////////////////////////// +// Example: + +#include +#include + +namespace test { + typed_keyword name; + typed_keyword index; + keyword value; + keyword instance; + keyword ref; + + template + void foo1( char const* n, ValueType v, int i ) + { + std::cout << n << '[' << i << "]=" << v << std::endl; + } + + template + void foo(Params const& params) + { + int i = params[index]; + foo1( params[name], params[value], i ); + } + + template + void boo(Params const& params) + { + foo1( params[name], params[value], params.has(index) ? params[index] : 0 ); + } + + template + void doo(Params const& params) + { + char const* nm; + if( params.has(name) ) + nm = params[name]; + else + nm = "abc"; + foo1( nm, params[value], params.has(index) ? params[index] : 0 ); + } + + template + void moo1( T* t ) + { + std::cout << "non shared " << *t << std::endl; + } + + template + void moo1( boost::shared_ptr const& t ) + { + std::cout << "shared " << *t << std::endl; + } + + template + void moo(Params const& params) + { + moo1( params[instance] ); + } + + template + void goo(Params const& params) + { + params[ref] = 6; + } +} + +int main() +{ + using test::foo; + using test::boo; + using test::moo; + using test::doo; + using test::goo; + using test::name; + using test::value; + using test::index; + using test::instance; + using test::ref; + + foo(( name = "foo", index = 0, value = 2.5 )); + foo(( value = 'a', index = 1, name = "foo" )); + foo(( name = "foo", value = "abc", index = 1 )); + + try { + foo(( name = "foo", value = "abc" )); + } + catch( nfp_detail::access_to_invalid_parameter const& ) { + std::cout << "Got access_to_invalid_parameter" << std::endl; + } + + boo(( name = "boo", value = "abc" )); + boo(( name = "boo", index = 1, value = "abc" )); + doo(( value = "abc" )); + doo(( value = 1.56, name = "ytr" )); + + int i = 5; + + moo( instance = &i ); + moo( instance = boost::shared_ptr( new float(1.2) ) ); + + goo( ref = i ); + + return 0; +} + +// EOF