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 
6 //  See http://www.boost.org/libs/test for the library home page.
7 //
8 //  File        : $RCSfile$
9 //
10 //  Version     : $Revision: 57992 $
11 //
12 //  Description : defines parser - public interface for CLA parsing and accessing
13 // ***************************************************************************
14 
15 #ifndef BOOST_RT_CLA_PARSER_HPP_062604GER
16 #define BOOST_RT_CLA_PARSER_HPP_062604GER
17 
18 // Boost.Runtime.Parameter
19 #include <boost/test/utils/runtime/config.hpp>
20 #include <boost/test/utils/runtime/fwd.hpp>
21 #include <boost/test/utils/runtime/argument.hpp>
22 
23 #include <boost/test/utils/runtime/cla/fwd.hpp>
24 #include <boost/test/utils/runtime/cla/modifier.hpp>
25 #include <boost/test/utils/runtime/cla/argv_traverser.hpp>
26 
27 // Boost
28 #include <boost/optional.hpp>
29 
30 // STL
31 #include <list>
32 
33 namespace boost {
34 
35 namespace BOOST_RT_PARAM_NAMESPACE {
36 
37 namespace cla {
38 
39 // ************************************************************************** //
40 // **************             runtime::cla::parser             ************** //
41 // ************************************************************************** //
42 
43 namespace cla_detail {
44 
45 template<typename Modifier>
46 class global_mod_parser {
47 public:
global_mod_parser(parser & p,Modifier const & m)48     global_mod_parser( parser& p, Modifier const& m )
49     : m_parser( p )
50     , m_modifiers( m )
51     {}
52 
53     template<typename Param>
54     global_mod_parser const&
operator <<(shared_ptr<Param> param) const55     operator<<( shared_ptr<Param> param ) const
56     {
57         param->accept_modifier( m_modifiers );
58 
59         m_parser << param;
60 
61         return *this;
62     }
63 
64 private:
65     // Data members;
66     parser&             m_parser;
67     Modifier const&     m_modifiers;
68 };
69 
70 }
71 
72 // ************************************************************************** //
73 // **************             runtime::cla::parser             ************** //
74 // ************************************************************************** //
75 
76 class parser {
77 public:
78     typedef std::list<parameter_ptr>::const_iterator param_iterator;
79 
80     // Constructor
81     explicit            parser( cstring program_name = cstring() );
82 
83     // parameter list construction interface
84     parser&             operator<<( parameter_ptr param );
85 
86     // parser and global parameters modifiers
87     template<typename Modifier>
88     cla_detail::global_mod_parser<Modifier>
operator -(Modifier const & m)89     operator-( Modifier const& m )
90     {
91         nfp::optionally_assign( m_traverser.p_separator.value, m, input_separator );
92         nfp::optionally_assign( m_traverser.p_ignore_mismatch.value, m, ignore_mismatch_m );
93 
94         return cla_detail::global_mod_parser<Modifier>( *this, m );
95     }
96 
97     // input processing method
98     void                parse( int& argc, char_type** argv );
99 
100     // parameters access
101     param_iterator      first_param() const;
102     param_iterator      last_param() const;
103 
104     // arguments access
105     const_argument_ptr  operator[]( cstring string_id ) const;
106     cstring             get( cstring string_id ) const;
107 
108     template<typename T>
get(cstring string_id) const109     T const&            get( cstring string_id ) const
110     {
111         return arg_value<T>( valid_argument( string_id ) );
112     }
113 
114     template<typename T>
get(cstring string_id,boost::optional<T> & res) const115     void                get( cstring string_id, boost::optional<T>& res ) const
116     {
117         const_argument_ptr actual_arg = (*this)[string_id];
118 
119         if( actual_arg )
120             res = arg_value<T>( *actual_arg );
121         else
122             res.reset();
123     }
124 
125     // help/usage
126     void                usage( out_stream& ostr );
127     void                help(  out_stream& ostr );
128 
129 private:
130     argument const&     valid_argument( cstring string_id ) const;
131 
132     // Data members
133     argv_traverser              m_traverser;
134     std::list<parameter_ptr>    m_parameters;
135     dstring                     m_program_name;
136 };
137 
138 //____________________________________________________________________________//
139 
140 } // namespace cla
141 
142 } // namespace BOOST_RT_PARAM_NAMESPACE
143 
144 } // namespace boost
145 
146 #ifndef BOOST_RT_PARAM_OFFLINE
147 
148 #  define BOOST_RT_PARAM_INLINE inline
149 #  include <boost/test/utils/runtime/cla/parser.ipp>
150 
151 #endif
152 
153 #endif // BOOST_RT_CLA_PARSER_HPP_062604GER
154