1 // Copyright Vladimir Prus 2002-2004.
\r
2 // Distributed under the Boost Software License, Version 1.0.
\r
3 // (See accompanying file LICENSE_1_0.txt
\r
4 // or copy at http://www.boost.org/LICENSE_1_0.txt)
\r
7 #ifndef BOOST_CMDLINE_VP_2003_05_19
\r
8 #define BOOST_CMDLINE_VP_2003_05_19
\r
10 #include <boost/program_options/config.hpp>
\r
11 #include <boost/program_options/errors.hpp>
\r
12 #include <boost/program_options/cmdline.hpp>
\r
13 #include <boost/program_options/option.hpp>
\r
14 #include <boost/program_options/options_description.hpp>
\r
15 #include <boost/program_options/positional_options.hpp>
\r
18 #include <boost/detail/workaround.hpp>
\r
20 #include <boost/function.hpp>
\r
25 namespace boost { namespace program_options { namespace detail {
\r
27 /** Command line parser class. Main requirements were:
\r
28 - Powerful enough to support all common uses.
\r
29 - Simple and easy to learn/use.
\r
30 - Minimal code size and external dependencies.
\r
31 - Extensible for custom syntaxes.
\r
33 First all options are registered. After that, elements of command line
\r
34 are extracted using operator++.
\r
36 For each element, user can find
\r
37 - if it's an option or an argument
\r
38 - name of the option
\r
39 - index of the option
\r
40 - option value(s), if any
\r
42 Sometimes the registered option name is not equal to the encountered
\r
43 one, for example, because name abbreviation is supported. Therefore
\r
44 two option names can be obtained:
\r
45 - the registered one
\r
46 - the one found at the command line
\r
48 There are lot of style options, which can be used to tune the command
\r
49 line parsing. In addition, it's possible to install additional parser
\r
50 which will process custom option styles.
\r
52 @todo mininal match length for guessing?
\r
54 class BOOST_PROGRAM_OPTIONS_DECL cmdline {
\r
57 typedef ::boost::program_options::command_line_style::style_t style_t;
\r
59 typedef function1<std::pair<std::string, std::string>,
\r
60 const std::string&>
\r
63 typedef function1<std::vector<option>, std::vector<std::string>&>
\r
66 /** Constructs a command line parser for (argc, argv) pair. Uses
\r
67 style options passed in 'style', which should be binary or'ed values
\r
68 of style_t enum. It can also be zero, in which case a "default"
\r
69 style will be used. If 'allow_unregistered' is true, then allows
\r
70 unregistered options. They will be assigned index 1 and are
\r
71 assumed to have optional parameter.
\r
73 cmdline(const std::vector<std::string>& args);
\r
76 cmdline(int argc, const char*const * argv);
\r
78 void style(int style);
\r
79 void allow_unregistered();
\r
81 void set_options_description(const options_description& desc);
\r
82 void set_positional_options(
\r
83 const positional_options_description& m_positional);
\r
85 std::vector<option> run();
\r
87 std::vector<option> parse_long_option(std::vector<std::string>& args);
\r
88 std::vector<option> parse_short_option(std::vector<std::string>& args);
\r
89 std::vector<option> parse_dos_option(std::vector<std::string>& args);
\r
90 std::vector<option> parse_disguised_long_option(
\r
91 std::vector<std::string>& args);
\r
92 std::vector<option> parse_terminator(
\r
93 std::vector<std::string>& args);
\r
94 std::vector<option> handle_additional_parser(
\r
95 std::vector<std::string>& args);
\r
98 /** Set additional parser. This will be called for each token
\r
99 of command line. If first string in pair is not empty,
\r
100 then the token is considered matched by this parser,
\r
101 and the first string will be considered an option name
\r
102 (which can be long or short), while the second will be
\r
103 option's parameter (if not empty).
\r
104 Note that additional parser can match only one token.
\r
106 void set_additional_parser(additional_parser p);
\r
108 void extra_style_parser(style_parser s);
\r
110 void check_style(int style) const;
\r
112 bool is_style_active(style_t style) const;
\r
114 void init(const std::vector<std::string>& args);
\r
117 finish_option(option& opt,
\r
118 std::vector<std::string>& other_tokens,
\r
119 const std::vector<style_parser>& style_parsers);
\r
121 // Copies of input.
\r
122 std::vector<std::string> args;
\r
124 bool m_allow_unregistered;
\r
126 const options_description* m_desc;
\r
127 const positional_options_description* m_positional;
\r
129 additional_parser m_additional_parser;
\r
130 style_parser m_style_parser;
\r
133 void test_cmdline_detail();
\r