summaryrefslogtreecommitdiff
path: root/boost/test/utils/runtime/cla/parser.ipp
blob: 995411a73f0deab2ebb6e4c77fa215f8a5b47931 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
//  (C) Copyright Gennadiy Rozental 2005-2008.
//  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.
//
//  File        : $RCSfile$
//
//  Version     : $Revision: 54633 $
//
//  Description : implements parser - public interface for CLA parsing and accessing
// ***************************************************************************

#ifndef BOOST_RT_CLA_PARSER_IPP_062904GER
#define BOOST_RT_CLA_PARSER_IPP_062904GER

// Boost.Runtime.Parameter
#include <boost/test/utils/runtime/config.hpp>
#include <boost/test/utils/runtime/trace.hpp>
#include <boost/test/utils/runtime/argument.hpp>

#include <boost/test/utils/runtime/cla/argv_traverser.hpp>
#include <boost/test/utils/runtime/cla/parameter.hpp>
#include <boost/test/utils/runtime/cla/modifier.hpp>
#include <boost/test/utils/runtime/cla/validation.hpp>
#include <boost/test/utils/runtime/cla/parser.hpp>

// Boost.Test
#include <boost/test/utils/basic_cstring/io.hpp>
#include <boost/test/utils/foreach.hpp>

// Boost
#include <boost/lexical_cast.hpp>

namespace boost {

namespace BOOST_RT_PARAM_NAMESPACE {

namespace cla {

// ************************************************************************** //
// **************             runtime::cla::parser             ************** //
// ************************************************************************** //

BOOST_RT_PARAM_INLINE
parser::parser( cstring program_name )
{
    assign_op( m_program_name, program_name, 0 );
}

//____________________________________________________________________________//

BOOST_RT_PARAM_INLINE parser::param_iterator
parser::first_param() const
{
    return m_parameters.begin();
}

//____________________________________________________________________________//

BOOST_RT_PARAM_INLINE parser::param_iterator
parser::last_param() const
{
    return m_parameters.end();
}

//____________________________________________________________________________//

BOOST_RT_PARAM_INLINE argument const&
parser::valid_argument( cstring string_id ) const
{
    const_argument_ptr arg = (*this)[string_id];

    BOOST_RT_PARAM_VALIDATE_LOGIC( !!arg, "Actual argument for parameter " << string_id << " is not present" );

    return *arg;
}

//____________________________________________________________________________//

BOOST_RT_PARAM_INLINE parser&
parser::operator<<( parameter_ptr new_param )
{
    BOOST_TEST_FOREACH( parameter_ptr, old_param, m_parameters ) {
        BOOST_RT_PARAM_VALIDATE_LOGIC( !old_param->conflict_with( *new_param ),
            BOOST_RT_PARAM_LITERAL( "Definition of parameter " )                << new_param->id_2_report() << 
            BOOST_RT_PARAM_LITERAL( " conflicts with defintion of parameter " ) << old_param->id_2_report() );
    }

    m_parameters.push_back( new_param );

    return *this;
}

//____________________________________________________________________________//

BOOST_RT_PARAM_INLINE void
parser::parse( int& argc, char_type** argv )
{
    if( m_program_name.empty() ) {
        m_program_name.assign( argv[0] );
        dstring::size_type pos = m_program_name.find_last_of( BOOST_RT_PARAM_LITERAL( "/\\" ) );

        if( pos != static_cast<dstring::size_type>(cstring::npos) )
            m_program_name.erase( 0, pos+1 );
    }

    m_traverser.init( argc, argv );

    try {
        while( !m_traverser.eoi() ) {
            parameter_ptr found_param;

            BOOST_RT_PARAM_TRACE( "Total " << m_parameters.size() << " parameters registered" );

            BOOST_TEST_FOREACH( parameter_ptr const&, curr_param, m_parameters ) {
                BOOST_RT_PARAM_TRACE( "Try parameter " << curr_param->id_2_report() );

                if( curr_param->matching( m_traverser, !found_param ) ) {
                    BOOST_RT_PARAM_TRACE( "Match found" );
                    BOOST_RT_CLA_VALIDATE_INPUT( !found_param, (m_traverser.rollback(),m_traverser), "Ambiguous input" );

                    found_param = curr_param;
                }

                m_traverser.rollback();
            }

            if( !found_param ) {
                BOOST_RT_PARAM_TRACE( "No match found" );
                BOOST_RT_CLA_VALIDATE_INPUT( m_traverser.handle_mismatch(), m_traverser,
                                             BOOST_RT_PARAM_LITERAL( "Unexpected input" ) );

                continue;
            }

            BOOST_RT_PARAM_TRACE( "Parse argument value" );
            found_param->produce_argument( m_traverser );

            m_traverser.commit();
        }

        BOOST_TEST_FOREACH( parameter_ptr const&, curr_param, m_parameters ) {
            if( !curr_param->p_optional && !curr_param->actual_argument() ) {
                curr_param->produce_argument( *this );

                BOOST_RT_PARAM_VALIDATE_LOGIC( curr_param->actual_argument(),
                    BOOST_RT_PARAM_LITERAL( "Required argument for parameter " ) << curr_param->id_2_report()
                        << BOOST_RT_PARAM_LITERAL( " is missing" ) );
            }
        }
    }
    catch( bad_lexical_cast const& ) {
        BOOST_RT_PARAM_REPORT_LOGIC_ERROR( 
            BOOST_RT_PARAM_LITERAL( "String to value convertion error during input parsing" ) );
    }

    m_traverser.remainder( argc, argv );
}

//____________________________________________________________________________//

BOOST_RT_PARAM_INLINE const_argument_ptr
parser::operator[]( cstring string_id ) const
{
    parameter_ptr found_param;

    BOOST_TEST_FOREACH( parameter_ptr const&, curr_param, m_parameters ) {
        if( curr_param->responds_to( string_id ) ) {
            BOOST_RT_PARAM_VALIDATE_LOGIC( !found_param,
                                           BOOST_RT_PARAM_LITERAL( "Ambiguous parameter string id: " ) << string_id );

            found_param = curr_param;
        }
    }

    return found_param ? found_param->actual_argument() : argument_ptr();
}

//____________________________________________________________________________//

BOOST_RT_PARAM_INLINE cstring
parser::get( cstring string_id ) const
{
    return get<cstring>( string_id );
}

//____________________________________________________________________________//

BOOST_RT_PARAM_INLINE void
parser::usage( out_stream& ostr )
{
    if( m_program_name.empty() )
        assign_op( m_program_name, BOOST_RT_PARAM_CSTRING_LITERAL( "<program>" ), 0 );

    format_stream fs;

    fs << m_program_name;

    BOOST_TEST_FOREACH( parameter_ptr const&, curr_param, m_parameters ) {
        fs << BOOST_RT_PARAM_LITERAL( ' ' );

        if( curr_param->p_optional )
            fs << BOOST_RT_PARAM_LITERAL( '[' );

        curr_param->usage_info( fs );

        if( curr_param->p_optional )
            fs << BOOST_RT_PARAM_LITERAL( ']' );

        if( curr_param->p_multiplicable ) {
            fs << BOOST_RT_PARAM_CSTRING_LITERAL( " ... " );
            
            if( curr_param->p_optional )
                fs << BOOST_RT_PARAM_LITERAL( '[' );

            curr_param->usage_info( fs );

            if( curr_param->p_optional )
                fs << BOOST_RT_PARAM_LITERAL( ']' );
        }
    }

    ostr << BOOST_RT_PARAM_CSTRING_LITERAL( "Usage:\n" ) << fs.str() << std::endl;
}

//____________________________________________________________________________//

BOOST_RT_PARAM_INLINE void
parser::help( out_stream& ostr )
{
    usage( ostr );

    bool need_where = true;

    BOOST_TEST_FOREACH( parameter_ptr const&, curr_param, m_parameters ) {
        if( curr_param->p_description->empty() )
            continue;

        if( need_where ) {
            ostr << BOOST_RT_PARAM_CSTRING_LITERAL( "where:\n" );
            need_where = false;
        }

        ostr << curr_param->id_2_report() << BOOST_RT_PARAM_CSTRING_LITERAL( " - " ) << curr_param->p_description << std::endl;
    }
}

//____________________________________________________________________________//

} // namespace cla

} // namespace BOOST_RT_PARAM_NAMESPACE

} // namespace boost

#endif // BOOST_RT_CLA_PARSER_IPP_062904GER