1 // (C) Copyright Gennadiy Rozental 2001.
2 // Distributed under the Boost Software License, Version 1.0.
3 // (See accompanying file LICENSE_1_0.txt or copy at
4 // http://www.boost.org/LICENSE_1_0.txt)
6 // See http://www.boost.org/libs/test for the library home page.
10 // Version : $Revision$
12 // Description : formal parameter definition
13 // ***************************************************************************
15 #ifndef BOOST_TEST_UTILS_RUNTIME_PARAMETER_HPP
16 #define BOOST_TEST_UTILS_RUNTIME_PARAMETER_HPP
18 // Boost.Test Runtime parameters
19 #include <boost/test/utils/runtime/fwd.hpp>
20 #include <boost/test/utils/runtime/modifier.hpp>
21 #include <boost/test/utils/runtime/argument.hpp>
22 #include <boost/test/utils/runtime/argument_factory.hpp>
25 #include <boost/test/utils/class_properties.hpp>
26 #include <boost/test/utils/foreach.hpp>
27 #include <boost/test/utils/setcolor.hpp>
30 #include <boost/function/function2.hpp>
31 #include <boost/algorithm/cxx11/all_of.hpp>
36 #include <boost/test/detail/suppress_warnings.hpp>
42 std::ostream& commandline_pretty_print(
44 std::string const& prefix,
45 std::string const& to_print) {
47 const int split_at = 80;
49 std::string::size_type current = 0;
51 while(current < to_print.size()) {
53 // discards spaces at the beginning
54 std::string::size_type startpos = to_print.find_first_not_of(" \t\n", current);
55 current += startpos - current;
57 bool has_more_lines = (current + split_at) < to_print.size();
60 std::string::size_type endpos = to_print.find_last_of(" \t\n", current + split_at);
61 std::string sub(to_print.substr(current, endpos - current));
62 ostr << prefix << sub;
64 current += endpos - current;
68 ostr << prefix << to_print.substr(current, split_at);
75 // ************************************************************************** //
76 // ************** runtime::parameter_cla_id ************** //
77 // ************************************************************************** //
78 // set of attributes identifying the parameter in the command line
80 struct parameter_cla_id {
81 parameter_cla_id( cstring prefix, cstring tag, cstring value_separator, bool negatable )
82 : m_prefix( prefix.begin(), prefix.size() )
83 , m_tag( tag.begin(), tag.size() )
84 , m_value_separator( value_separator.begin(), value_separator.size() )
85 , m_negatable( negatable )
88 BOOST_TEST_I_ASSRT( algorithm::all_of( m_prefix.begin(), m_prefix.end(), valid_prefix_char ),
89 invalid_cla_id() << "Parameter " << m_tag
90 << " has invalid characters in prefix." );
92 BOOST_TEST_I_ASSRT( algorithm::all_of( m_tag.begin(), m_tag.end(), valid_name_char ),
93 invalid_cla_id() << "Parameter " << m_tag
94 << " has invalid characters in name." );
96 BOOST_TEST_I_ASSRT( algorithm::all_of( m_value_separator.begin(), m_value_separator.end(), valid_separator_char ),
97 invalid_cla_id() << "Parameter " << m_tag
98 << " has invalid characters in value separator." );
101 static bool valid_prefix_char( char c )
103 return c == '-' || c == '/' ;
105 static bool valid_separator_char( char c )
107 return c == '=' || c == ':' || c == ' ' || c == '\0';
109 static bool valid_name_char( char c )
111 return std::isalnum( c ) || c == '+' || c == '_' || c == '?';
114 std::string m_prefix;
116 std::string m_value_separator;
120 typedef std::vector<parameter_cla_id> param_cla_ids;
122 // ************************************************************************** //
123 // ************** runtime::basic_param ************** //
124 // ************************************************************************** //
126 cstring const help_prefix("////");
129 typedef function<void (cstring)> callback_type;
130 typedef unit_test::readwrite_property<bool> bool_property;
133 /// Constructor with modifiers
134 template<typename Modifiers>
135 basic_param( cstring name, bool is_optional, bool is_repeatable, Modifiers const& m )
136 : p_name( name.begin(), name.end() )
137 , p_description( nfp::opt_get( m, description, std::string() ) )
138 , p_help( nfp::opt_get( m, runtime::help, std::string() ) )
139 , p_env_var( nfp::opt_get( m, env_var, std::string() ) )
140 , p_value_hint( nfp::opt_get( m, value_hint, std::string() ) )
141 , p_optional( is_optional )
142 , p_repeatable( is_repeatable )
143 , p_has_optional_value( m.has( optional_value ) )
144 , p_has_default_value( m.has( default_value ) || is_repeatable )
145 , p_callback( nfp::opt_get( m, callback, callback_type() ) )
147 add_cla_id( help_prefix, name, ":" );
151 virtual ~basic_param() {}
154 std::string const p_name;
155 std::string const p_description;
156 std::string const p_help;
157 std::string const p_env_var;
158 std::string const p_value_hint;
159 bool const p_optional;
160 bool const p_repeatable;
161 bool_property p_has_optional_value;
162 bool_property p_has_default_value;
163 callback_type const p_callback;
165 /// interface for cloning typed parameters
166 virtual basic_param_ptr clone() const = 0;
169 param_cla_ids const& cla_ids() const { return m_cla_ids; }
170 void add_cla_id( cstring prefix, cstring tag, cstring value_separator )
172 add_cla_id_impl( prefix, tag, value_separator, false, true );
175 /// interface for producing argument values for this parameter
176 virtual void produce_argument( cstring token, bool negative_form, arguments_store& store ) const = 0;
177 virtual void produce_default( arguments_store& store ) const = 0;
179 /// interfaces for help message reporting
180 virtual void usage( std::ostream& ostr, cstring negation_prefix_, bool use_color = true )
182 namespace utils = unit_test::utils;
183 namespace ut_detail = unit_test::ut_detail;
189 BOOST_TEST_SCOPE_SETCOLOR( use_color, ostr, term_attr::BRIGHT, term_color::GREEN );
195 if( !p_description.empty() ) {
196 commandline_pretty_print(ostr, " ", p_description) << '\n';
199 BOOST_TEST_FOREACH( parameter_cla_id const&, id, cla_ids() ) {
200 if( id.m_prefix == help_prefix )
203 ostr << " " << id.m_prefix;
206 cla_name_help( ostr, id.m_tag, negation_prefix_, use_color );
208 cla_name_help( ostr, id.m_tag, "", use_color );
210 BOOST_TEST_SCOPE_SETCOLOR( use_color, ostr, term_attr::BRIGHT, term_color::YELLOW );
211 bool optional_value_ = false;
213 if( p_has_optional_value ) {
214 optional_value_ = true;
219 if( id.m_value_separator.empty() )
222 ostr << id.m_value_separator;
227 if( optional_value_ )
234 virtual void help( std::ostream& ostr, cstring negation_prefix_, bool use_color = true )
236 usage( ostr, negation_prefix_, use_color );
238 if( !p_help.empty() ) {
240 commandline_pretty_print(ostr, " ", p_help);
245 void add_cla_id_impl( cstring prefix,
247 cstring value_separator,
249 bool validate_value_separator )
251 BOOST_TEST_I_ASSRT( !tag.is_empty(),
252 invalid_cla_id() << "Parameter can't have an empty name." );
254 BOOST_TEST_I_ASSRT( !prefix.is_empty(),
255 invalid_cla_id() << "Parameter " << tag
256 << " can't have an empty prefix." );
258 BOOST_TEST_I_ASSRT( !value_separator.is_empty(),
259 invalid_cla_id() << "Parameter " << tag
260 << " can't have an empty value separator." );
262 // We trim value separator from all the spaces, so token end will indicate separator
263 value_separator.trim();
264 BOOST_TEST_I_ASSRT( !validate_value_separator || !value_separator.is_empty() || !p_has_optional_value,
265 invalid_cla_id() << "Parameter " << tag
266 << " with optional value attribute can't use space as value separator." );
268 m_cla_ids.push_back( parameter_cla_id( prefix, tag, value_separator, negatable ) );
272 /// interface for usage/help customization
273 virtual void cla_name_help( std::ostream& ostr, cstring cla_tag, cstring /*negation_prefix_*/, bool /*use_color*/ = true) const
277 virtual void value_help( std::ostream& ostr ) const
279 if( p_value_hint.empty() )
282 ostr << p_value_hint;
286 param_cla_ids m_cla_ids;
289 // ************************************************************************** //
290 // ************** runtime::parameter ************** //
291 // ************************************************************************** //
294 OPTIONAL_PARAM, // 0-1
295 REQUIRED_PARAM, // exactly 1
296 REPEATABLE_PARAM // 0-N
299 //____________________________________________________________________________//
301 template<typename ValueType, args_amount a = runtime::OPTIONAL_PARAM, bool is_enum = false>
302 class parameter : public basic_param {
304 /// Constructor with modifiers
305 #ifndef BOOST_NO_CXX11_FUNCTION_TEMPLATE_DEFAULT_ARGS
306 template<typename Modifiers=nfp::no_params_type>
307 parameter( cstring name, Modifiers const& m = nfp::no_params )
309 template<typename Modifiers>
310 parameter( cstring name, Modifiers const& m )
312 : basic_param( name, a != runtime::REQUIRED_PARAM, a == runtime::REPEATABLE_PARAM, m )
315 BOOST_TEST_I_ASSRT( !m.has( default_value ) || a == runtime::OPTIONAL_PARAM,
316 invalid_param_spec() << "Parameter " << name
317 << " is not optional and can't have default_value." );
319 BOOST_TEST_I_ASSRT( !m.has( optional_value ) || !this->p_repeatable,
320 invalid_param_spec() << "Parameter " << name
321 << " is repeatable and can't have optional_value." );
325 virtual basic_param_ptr clone() const
327 return basic_param_ptr( new parameter( *this ) );
329 virtual void produce_argument( cstring token, bool , arguments_store& store ) const
331 m_arg_factory.produce_argument( token, this->p_name, store );
333 virtual void produce_default( arguments_store& store ) const
335 if( !this->p_has_default_value )
338 m_arg_factory.produce_default( this->p_name, store );
342 typedef argument_factory<ValueType, is_enum, a == runtime::REPEATABLE_PARAM> factory_t;
343 factory_t m_arg_factory;
346 //____________________________________________________________________________//
348 class option : public basic_param {
350 /// Constructor with modifiers
351 #ifndef BOOST_NO_CXX11_FUNCTION_TEMPLATE_DEFAULT_ARGS
352 template<typename Modifiers=nfp::no_params_type>
353 option( cstring name, Modifiers const& m = nfp::no_params )
355 template<typename Modifiers>
356 option( cstring name, Modifiers const& m )
358 : basic_param( name, true, false, nfp::opt_append( nfp::opt_append( m, optional_value = true), default_value = false) )
359 , m_arg_factory( nfp::opt_append( nfp::opt_append( m, optional_value = true), default_value = false) )
363 void add_cla_id( cstring prefix, cstring tag, cstring value_separator, bool negatable = false )
365 add_cla_id_impl( prefix, tag, value_separator, negatable, false );
369 virtual basic_param_ptr clone() const
371 return basic_param_ptr( new option( *this ) );
374 virtual void produce_argument( cstring token, bool negative_form, arguments_store& store ) const
377 store.set( p_name, !negative_form );
379 BOOST_TEST_I_ASSRT( !negative_form,
380 format_error( p_name ) << "Can't set value to negative form of the argument." );
382 m_arg_factory.produce_argument( token, p_name, store );
386 virtual void produce_default( arguments_store& store ) const
388 m_arg_factory.produce_default( p_name, store );
390 virtual void cla_name_help( std::ostream& ostr, cstring cla_tag, cstring negation_prefix_, bool use_color = true ) const
392 namespace utils = unit_test::utils;
393 namespace ut_detail = unit_test::ut_detail;
395 if( !negation_prefix_.is_empty() ) {
396 BOOST_TEST_SCOPE_SETCOLOR( use_color, ostr, term_attr::BRIGHT, term_color::YELLOW );
397 ostr << '[' << negation_prefix_ << ']';
401 virtual void value_help( std::ostream& ostr ) const
403 if( p_value_hint.empty() )
404 ostr << "<boolean value>";
406 ostr << p_value_hint;
410 typedef argument_factory<bool, false, false> factory_t;
411 factory_t m_arg_factory;
414 //____________________________________________________________________________//
416 template<typename EnumType, args_amount a = runtime::OPTIONAL_PARAM>
417 class enum_parameter : public parameter<EnumType, a, true> {
418 typedef parameter<EnumType, a, true> base;
420 /// Constructor with modifiers
421 #ifndef BOOST_NO_CXX11_FUNCTION_TEMPLATE_DEFAULT_ARGS
422 template<typename Modifiers=nfp::no_params_type>
423 enum_parameter( cstring name, Modifiers const& m = nfp::no_params )
425 template<typename Modifiers>
426 enum_parameter( cstring name, Modifiers const& m )
430 #ifdef BOOST_TEST_CLA_NEW_API
431 auto const& values = m[enum_values<EnumType>::value];
432 auto it = values.begin();
434 std::vector<std::pair<cstring, EnumType> > const& values = m[enum_values<EnumType>::value];
435 typename std::vector<std::pair<cstring, EnumType> >::const_iterator it = values.begin();
437 while( it != values.end() ) {
438 m_valid_names.push_back( it->first );
444 virtual basic_param_ptr clone() const
446 return basic_param_ptr( new enum_parameter( *this ) );
449 virtual void value_help( std::ostream& ostr ) const
451 if( this->p_value_hint.empty() ) {
454 BOOST_TEST_FOREACH( cstring, name, m_valid_names ) {
464 ostr << this->p_value_hint;
468 std::vector<cstring> m_valid_names;
472 // ************************************************************************** //
473 // ************** runtime::parameters_store ************** //
474 // ************************************************************************** //
476 class parameters_store {
478 bool operator()( cstring lh, cstring rh ) const
480 return std::lexicographical_compare(lh.begin(), lh.end(),
481 rh.begin(), rh.end());
486 typedef std::map<cstring, basic_param_ptr, lg_compare> storage_type;
488 /// Adds parameter into the persistent store
489 void add( basic_param const& in )
491 basic_param_ptr p = in.clone();
493 BOOST_TEST_I_ASSRT( m_parameters.insert( std::make_pair( cstring(p->p_name), p ) ).second,
494 duplicate_param() << "Parameter " << p->p_name << " is duplicate." );
497 /// Returns true if there is no parameters registered
498 bool is_empty() const { return m_parameters.empty(); }
499 /// Returns map of all the registered parameter
500 storage_type const& all() const { return m_parameters; }
501 /// Returns true if parameter with psecified name is registered
502 bool has( cstring name ) const
504 return m_parameters.find( name ) != m_parameters.end();
506 /// Returns map of all the registered parameter
507 basic_param_ptr get( cstring name ) const
509 storage_type::const_iterator const& found = m_parameters.find( name );
510 BOOST_TEST_I_ASSRT( found != m_parameters.end(),
511 unknown_param() << "Parameter " << name << " is unknown." );
513 return found->second;
518 storage_type m_parameters;
521 } // namespace runtime
524 #include <boost/test/detail/enable_warnings.hpp>
526 #endif // BOOST_TEST_UTILS_RUNTIME_PARAMETER_HPP