Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/libs/program_options/src/cmdline.cpp @ 29

Last change on this file since 29 was 29, checked in by landauf, 16 years ago

updated boost from 1_33_1 to 1_34_1

File size: 16.1 KB
Line 
1// Copyright Vladimir Prus 2002-2004.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt
4// or copy at http://www.boost.org/LICENSE_1_0.txt)
5
6#define BOOST_PROGRAM_OPTIONS_SOURCE
7#include <boost/program_options/config.hpp>
8
9#include <boost/config.hpp>
10
11#include <boost/program_options/detail/cmdline.hpp>
12#include <boost/program_options/errors.hpp>
13#include <boost/program_options/value_semantic.hpp>
14#include <boost/program_options/options_description.hpp>
15#include <boost/program_options/positional_options.hpp>
16#include <boost/throw_exception.hpp>
17
18#include <boost/bind.hpp>
19
20#include <string>
21#include <utility>
22#include <vector>
23#include <cassert>
24#include <cstring>
25#include <cctype>
26
27#include <cstdio>
28
29#include <iostream>
30
31namespace boost { namespace program_options {
32
33    using namespace std;
34    using namespace boost::program_options::command_line_style;
35   
36    invalid_command_line_syntax::
37    invalid_command_line_syntax(const std::string& tokens, kind_t kind)
38    : invalid_syntax(tokens, error_message(kind)), m_kind(kind)
39    {}
40
41    std::string
42    invalid_command_line_syntax::error_message(kind_t kind)
43    {
44        // Initially, store the message in 'const char*' variable,
45        // to avoid conversion to std::string in all cases.
46        const char* msg;
47        switch(kind)
48        {
49        case long_not_allowed:
50            msg = "long options are not allowed";
51            break;
52        case long_adjacent_not_allowed:
53            msg = "parameters adjacent to long options not allowed";
54            break;
55        case short_adjacent_not_allowed:
56            msg = "parameters adjust to short options are not allowed";
57            break;
58        case empty_adjacent_parameter:
59            msg = "adjacent parameter is empty";
60            break;
61        case missing_parameter:
62            msg = "required parameter is missing";
63            break;
64        case extra_parameter:
65            msg = "extra parameter";
66            break;
67        default:
68            msg = "unknown error";
69        }
70        return msg;
71    }
72
73    invalid_command_line_syntax::kind_t
74    invalid_command_line_syntax::kind() const
75    {
76        return m_kind;
77    }
78
79
80}}
81
82
83namespace boost { namespace program_options { namespace detail {
84
85    // vc6 needs this, but borland chokes when this is added.
86#if BOOST_WORKAROUND(_MSC_VER, < 1300)
87    using namespace std;
88    using namespace program_options;
89#endif
90
91
92    cmdline::cmdline(const std::vector<std::string>& args)
93    {
94        init(args);
95    }
96
97    cmdline::cmdline(int argc, const char*const * argv)
98    {
99#if defined(BOOST_NO_TEMPLATED_ITERATOR_CONSTRUCTORS)
100        vector<string> args;
101        copy(argv+1, argv+argc, inserter(args, args.end()));
102        init(args);
103#else
104        init(vector<string>(argv+1, argv+argc));
105#endif
106    }
107
108    void
109    cmdline::init(const std::vector<std::string>& args)
110    {
111        this->args = args;       
112        m_style = command_line_style::default_style;
113        m_desc = 0;
114        m_positional = 0;
115        m_allow_unregistered = false;
116    }
117
118    void 
119    cmdline::style(int style)
120    {
121        if (style == 0) 
122            style = default_style;       
123
124        check_style(style);
125        this->m_style = style_t(style);
126    }
127   
128    void 
129    cmdline::allow_unregistered()
130    {
131        this->m_allow_unregistered = true;
132    }
133
134    void 
135    cmdline::check_style(int style) const
136    {
137        bool allow_some_long = 
138            (style & allow_long) || (style & allow_long_disguise);
139
140        const char* error = 0;
141        if (allow_some_long && 
142            !(style & long_allow_adjacent) && !(style & long_allow_next))
143            error = "style disallows parameters for long options";
144
145        if (!error && (style & allow_short) &&
146            !(style & short_allow_adjacent) && !(style & short_allow_next))
147            error = "style disallows parameters for short options";
148
149        if (!error && (style & allow_short) &&
150            !(style & allow_dash_for_short) && !(style & allow_slash_for_short))
151            error = "style disallows all characters for short options";
152
153        if (error)
154            throw invalid_command_line_style(error);
155
156        // Need to check that if guessing and long disguise are enabled
157        // -f will mean the same as -foo
158    }
159
160    void 
161    cmdline::set_options_description(const options_description& desc)
162    {
163        m_desc = &desc;
164    }
165
166    void 
167    cmdline::set_positional_options(
168        const positional_options_description& positional)
169    {
170        m_positional = &positional;
171    }
172
173
174    vector<option>
175    cmdline::run()
176    {
177        // The parsing is done by having a set of 'style parsers'
178        // and trying then in order. Each parser is passed a vector
179        // of unparsed tokens and can consume some of them (by
180        // removing elements on front) and return a vector of options.
181        //
182        // We try each style parser in turn, untill some input
183        // is consumed. The returned vector of option may contain the
184        // result of just syntactic parsing of token, say --foo will
185        // be parsed as option with name 'foo', and the style parser
186        // is not required to care if that option is defined, and how
187        // many tokens the value may take.
188        // So, after vector is returned, we validate them.
189        assert(m_desc);
190
191        vector<style_parser> style_parsers;     
192
193        if (m_style_parser)
194            style_parsers.push_back(m_style_parser);
195
196        if (m_additional_parser)
197            style_parsers.push_back(
198                bind(&cmdline::handle_additional_parser, this, _1));
199
200        if (m_style & allow_long)
201            style_parsers.push_back(
202                bind(&cmdline::parse_long_option, this, _1));
203
204        if ((m_style & allow_long_disguise))
205            style_parsers.push_back(
206                bind(&cmdline::parse_disguised_long_option, this, _1));
207
208        if ((m_style & allow_short) && (m_style & allow_dash_for_short))
209            style_parsers.push_back(
210                bind(&cmdline::parse_short_option, this, _1));
211
212        if ((m_style & allow_short) && (m_style & allow_slash_for_short))
213            style_parsers.push_back(bind(&cmdline::parse_dos_option, this, _1));
214
215        style_parsers.push_back(bind(&cmdline::parse_terminator, this, _1));
216
217        vector<option> result;
218        while(!args.empty())
219        {
220            bool ok = false;
221            for(unsigned i = 0; i < style_parsers.size(); ++i)
222            {
223                unsigned current_size = args.size();
224                vector<option> next = style_parsers[i](args);
225
226                // Check that option names
227                // are valid, and that all values are in place.
228                if (!next.empty())
229                {
230                    vector<string> e;
231                    for(unsigned k = 0; k < next.size()-1; ++k) {
232                        finish_option(next[k], e);
233                    }
234                    // For the last option, pass the unparsed tokens
235                    // so that they can be added to next.back()'s values
236                    // if appropriate.
237                    finish_option(next.back(), args);
238                    for (unsigned j = 0; j < next.size(); ++j)
239                        result.push_back(next[j]);                   
240                }
241                               
242                if (args.size() != current_size) {
243                    ok = true;
244                    break;               
245                } 
246            }
247           
248            if (!ok) {
249                option opt;
250                opt.value.push_back(args[0]);
251                opt.original_tokens.push_back(args[0]);
252                result.push_back(opt);
253                args.erase(args.begin());
254            }
255        }
256
257        // Assign position keys to positional options.
258        int position_key = 0;
259        for(unsigned i = 0; i < result.size(); ++i) {
260            if (result[i].string_key.empty())
261                result[i].position_key = position_key++;
262        }
263
264        if (m_positional)
265        {
266            unsigned position = 0;
267            for (unsigned i = 0; i < result.size(); ++i) {
268                option& opt = result[i];
269                if (opt.position_key != -1) {
270                    if (position >= m_positional->max_total_count())
271                    {
272                        throw too_many_positional_options_error(
273                            "too many positional options");
274                    }
275                    opt.string_key = m_positional->name_for_position(position);
276                    ++position;
277                }
278            }
279        }
280
281        return result;
282    }
283
284    void
285    cmdline::finish_option(option& opt,
286                           vector<string>& other_tokens)
287    {                                   
288        if (opt.string_key.empty())
289            return;
290
291        // First check that the option is valid, and get its description.
292        // TODO: case-sensitivity.
293        const option_description* xd = 
294            m_desc->find_nothrow(opt.string_key, (m_style & allow_guessing));
295
296        if (!xd)
297        {
298            if (m_allow_unregistered) {
299                opt.unregistered = true;
300                return;
301            } else {
302                boost::throw_exception(unknown_option(opt.string_key));
303            }               
304        }
305        const option_description& d = *xd;
306
307        // Canonize the name
308        opt.string_key = d.key(opt.string_key);
309
310        // We check that the min/max number of tokens for the option
311        // agrees with the number of tokens we have. The 'adjacent_value'
312        // (the value in --foo=1) counts as a separate token, and if present
313        // must be consumed. The following tokens on the command line may be
314        // left unconsumed.
315
316        // We don't check if those tokens look like option, or not!
317
318        unsigned min_tokens = d.semantic()->min_tokens();
319        unsigned max_tokens = d.semantic()->max_tokens();
320       
321        unsigned present_tokens = opt.value.size() + other_tokens.size();
322       
323        if (present_tokens >= min_tokens)
324        {
325            if (!opt.value.empty() && max_tokens == 0) {
326                throw invalid_command_line_syntax(opt.string_key,
327                    invalid_command_line_syntax::extra_parameter);                                                               
328            }
329           
330            max_tokens -= opt.value.size();
331
332            // Everything's OK, move the values to the result.           
333            for(;!other_tokens.empty() && max_tokens--; ) {
334                opt.value.push_back(other_tokens[0]);
335                opt.original_tokens.push_back(other_tokens[0]);
336                other_tokens.erase(other_tokens.begin());
337            }
338        }
339        else
340        {
341            throw invalid_command_line_syntax(opt.string_key,
342                invalid_command_line_syntax::missing_parameter); 
343
344        }
345    }
346
347    std::vector<option> 
348    cmdline::parse_long_option(std::vector<string>& args)
349    {
350        vector<option> result;
351        const std::string& tok = args[0];
352        if (tok.size() >= 3 && tok[0] == '-' && tok[1] == '-')
353        {   
354            string name, adjacent;
355
356            string::size_type p = tok.find('=');
357            if (p != tok.npos)
358            {
359                name = tok.substr(2, p-2);
360                adjacent = tok.substr(p+1);
361                if (adjacent.empty())
362                    throw invalid_command_line_syntax(name,
363                      invalid_command_line_syntax::empty_adjacent_parameter);
364            }
365            else
366            {
367                name = tok.substr(2);
368            }
369            option opt;
370            opt.string_key = name;
371            if (!adjacent.empty())
372                opt.value.push_back(adjacent);
373            opt.original_tokens.push_back(tok);
374            result.push_back(opt);
375            args.erase(args.begin());
376        }
377        return result;
378    }
379
380
381    std::vector<option> 
382    cmdline::parse_short_option(std::vector<string>& args)
383    {
384        const std::string& tok = args[0];
385        if (tok.size() >= 2 && tok[0] == '-' && tok[1] != '-')
386        {   
387            vector<option> result;
388
389            string name = tok.substr(0,2);
390            string adjacent = tok.substr(2);
391
392            // Short options can be 'grouped', so that
393            // "-d -a" becomes "-da". Loop, processing one
394            // option at a time. We exit the loop when either
395            // we've processed all the token, or when the remainder
396            // of token is considered to be value, not further grouped
397            // option.
398            for(;;) {
399                const option_description* d
400                    = m_desc->find_nothrow(name, false);
401
402                // FIXME: check for 'allow_sticky'.
403                if (d && (m_style & allow_sticky) &&
404                    d->semantic()->max_tokens() == 0 && !adjacent.empty()) {
405                    // 'adjacent' is in fact further option.
406                    option opt;
407                    opt.string_key = name;
408                    result.push_back(opt);
409
410                    if (adjacent.empty())
411                    {
412                        args.erase(args.begin());
413                        break;
414                    }
415
416                    name = string("-") + adjacent[0];
417                    adjacent.erase(adjacent.begin());
418                } else {
419                   
420                    option opt;
421                    opt.string_key = name;
422                    opt.original_tokens.push_back(tok);
423                    if (!adjacent.empty())
424                        opt.value.push_back(adjacent);
425                    result.push_back(opt);
426                    args.erase(args.begin());                   
427                    break;
428                }
429            }
430            return result;
431        }
432        return std::vector<option>();
433    }
434
435    std::vector<option> 
436    cmdline::parse_dos_option(std::vector<string>& args)
437    {
438        vector<option> result;
439        const std::string& tok = args[0];
440        if (tok.size() >= 2 && tok[0] == '/')
441        {   
442            string name = "-" + tok.substr(1,1);
443            string adjacent = tok.substr(2);
444
445            option opt;
446            opt.string_key = name;
447            if (!adjacent.empty())
448                opt.value.push_back(adjacent);
449            opt.original_tokens.push_back(tok);
450            result.push_back(opt);
451            args.erase(args.begin());
452        }
453        return result;
454    }
455
456    std::vector<option> 
457    cmdline::parse_disguised_long_option(std::vector<string>& args)
458    {
459        const std::string& tok = args[0];
460        if (tok.size() >= 2 && 
461            ((tok[0] == '-' && tok[1] != '-') ||
462             ((m_style & allow_slash_for_short) && tok[0] == '/')))           
463        {
464            if (m_desc->find_nothrow(tok.substr(1, tok.find('=')-1), 
465                                     m_style & allow_guessing)) {
466                args[0].insert(0, "-");
467                if (args[0][1] == '/')
468                    args[0][1] = '-';
469                return parse_long_option(args);
470            }
471        }
472        return vector<option>();
473    }
474
475    std::vector<option> 
476    cmdline::parse_terminator(std::vector<std::string>& args)
477    {
478        vector<option> result;
479        const std::string& tok = args[0];
480        if (tok == "--")
481        {
482            for(unsigned i = 1; i < args.size(); ++i)
483            {
484                option opt;
485                opt.value.push_back(args[i]);
486                result.push_back(opt);
487            }
488            args.clear();
489        }
490        return result;
491    }
492
493    std::vector<option> 
494    cmdline::handle_additional_parser(std::vector<std::string>& args)
495    {
496        vector<option> result;
497        pair<string, string> r = m_additional_parser(args[0]);
498        if (!r.first.empty()) {
499            option next;
500            next.string_key = r.first;
501            if (!r.second.empty())
502                next.value.push_back(r.second);
503            result.push_back(next);
504            args.erase(args.begin());
505        }
506        return result;
507    }
508
509    void 
510    cmdline::set_additional_parser(additional_parser p)
511    {
512        m_additional_parser = p;
513    }
514
515    void 
516    cmdline::extra_style_parser(style_parser s)
517    {
518        m_style_parser = s;
519    }
520
521
522
523}}}
Note: See TracBrowser for help on using the repository browser.