parser.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // (C) Copyright Gennadiy Rozental 2005-2008.
  2. // Use, modification, and distribution are subject to the
  3. // Boost Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. // See http://www.boost.org/libs/test for the library home page.
  6. //
  7. // File : $RCSfile$
  8. //
  9. // Version : $Revision: 57992 $
  10. //
  11. // Description : defines parser - public interface for CLA parsing and accessing
  12. // ***************************************************************************
  13. #ifndef BOOST_RT_CLA_PARSER_HPP_062604GER
  14. #define BOOST_RT_CLA_PARSER_HPP_062604GER
  15. // Boost.Runtime.Parameter
  16. #include <boost/test/utils/runtime/config.hpp>
  17. #include <boost/test/utils/runtime/fwd.hpp>
  18. #include <boost/test/utils/runtime/argument.hpp>
  19. #include <boost/test/utils/runtime/cla/fwd.hpp>
  20. #include <boost/test/utils/runtime/cla/modifier.hpp>
  21. #include <boost/test/utils/runtime/cla/argv_traverser.hpp>
  22. // Boost
  23. #include <boost/optional.hpp>
  24. // STL
  25. #include <list>
  26. namespace boost {
  27. namespace BOOST_RT_PARAM_NAMESPACE {
  28. namespace cla {
  29. // ************************************************************************** //
  30. // ************** runtime::cla::parser ************** //
  31. // ************************************************************************** //
  32. namespace cla_detail {
  33. template<typename Modifier>
  34. class global_mod_parser {
  35. public:
  36. global_mod_parser( parser& p, Modifier const& m )
  37. : m_parser( p )
  38. , m_modifiers( m )
  39. {}
  40. template<typename Param>
  41. global_mod_parser const&
  42. operator<<( shared_ptr<Param> param ) const
  43. {
  44. param->accept_modifier( m_modifiers );
  45. m_parser << param;
  46. return *this;
  47. }
  48. private:
  49. // Data members;
  50. parser& m_parser;
  51. Modifier const& m_modifiers;
  52. };
  53. }
  54. // ************************************************************************** //
  55. // ************** runtime::cla::parser ************** //
  56. // ************************************************************************** //
  57. class parser {
  58. public:
  59. typedef std::list<parameter_ptr>::const_iterator param_iterator;
  60. // Constructor
  61. explicit parser( cstring program_name = cstring() );
  62. // parameter list construction interface
  63. parser& operator<<( parameter_ptr param );
  64. // parser and global parameters modifiers
  65. template<typename Modifier>
  66. cla_detail::global_mod_parser<Modifier>
  67. operator-( Modifier const& m )
  68. {
  69. nfp::optionally_assign( m_traverser.p_separator.value, m, input_separator );
  70. nfp::optionally_assign( m_traverser.p_ignore_mismatch.value, m, ignore_mismatch_m );
  71. return cla_detail::global_mod_parser<Modifier>( *this, m );
  72. }
  73. // input processing method
  74. void parse( int& argc, char_type** argv );
  75. // parameters access
  76. param_iterator first_param() const;
  77. param_iterator last_param() const;
  78. // arguments access
  79. const_argument_ptr operator[]( cstring string_id ) const;
  80. cstring get( cstring string_id ) const;
  81. template<typename T>
  82. T const& get( cstring string_id ) const
  83. {
  84. return arg_value<T>( valid_argument( string_id ) );
  85. }
  86. template<typename T>
  87. void get( cstring string_id, boost::optional<T>& res ) const
  88. {
  89. const_argument_ptr actual_arg = (*this)[string_id];
  90. if( actual_arg )
  91. res = arg_value<T>( *actual_arg );
  92. else
  93. res.reset();
  94. }
  95. // help/usage
  96. void usage( out_stream& ostr );
  97. void help( out_stream& ostr );
  98. private:
  99. argument const& valid_argument( cstring string_id ) const;
  100. // Data members
  101. argv_traverser m_traverser;
  102. std::list<parameter_ptr> m_parameters;
  103. dstring m_program_name;
  104. };
  105. //____________________________________________________________________________//
  106. } // namespace cla
  107. } // namespace BOOST_RT_PARAM_NAMESPACE
  108. } // namespace boost
  109. #ifndef BOOST_RT_PARAM_OFFLINE
  110. # define BOOST_RT_PARAM_INLINE inline
  111. # include <boost/test/utils/runtime/cla/parser.ipp>
  112. #endif
  113. #endif // BOOST_RT_CLA_PARSER_HPP_062604GER