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)
7 #ifndef BOOST_CMDLINE_VP_2003_05_19
8 #define BOOST_CMDLINE_VP_2003_05_19
10 #include <boost/program_options/config.hpp>
11 #include <boost/program_options/errors.hpp>
12 #include <boost/program_options/cmdline.hpp>
13 #include <boost/program_options/option.hpp>
14 #include <boost/program_options/options_description.hpp>
15 #include <boost/program_options/positional_options.hpp>
18 #include <boost/detail/workaround.hpp>
20 #include <boost/function.hpp>
25 #if defined(BOOST_MSVC)
26 # pragma warning (push)
27 # pragma warning (disable:4251) // class 'std::vector<_Ty>' needs to have dll-interface to be used by clients of class 'boost::program_options::positional_options_description'
30 namespace boost { namespace program_options { namespace detail {
32 /** Command line parser class. Main requirements were:
33 - Powerful enough to support all common uses.
34 - Simple and easy to learn/use.
35 - Minimal code size and external dependencies.
36 - Extensible for custom syntaxes.
38 First all options are registered. After that, elements of command line
39 are extracted using operator++.
41 For each element, user can find
42 - if it's an option or an argument
45 - option value(s), if any
47 Sometimes the registered option name is not equal to the encountered
48 one, for example, because name abbreviation is supported. Therefore
49 two option names can be obtained:
51 - the one found at the command line
53 There are lot of style options, which can be used to tune the command
54 line parsing. In addition, it's possible to install additional parser
55 which will process custom option styles.
57 @todo mininal match length for guessing?
59 class BOOST_PROGRAM_OPTIONS_DECL cmdline {
62 typedef ::boost::program_options::command_line_style::style_t style_t;
64 typedef function1<std::pair<std::string, std::string>,
68 typedef function1<std::vector<option>, std::vector<std::string>&>
71 /** Constructs a command line parser for (argc, argv) pair. Uses
72 style options passed in 'style', which should be binary or'ed values
73 of style_t enum. It can also be zero, in which case a "default"
74 style will be used. If 'allow_unregistered' is true, then allows
75 unregistered options. They will be assigned index 1 and are
76 assumed to have optional parameter.
78 cmdline(const std::vector<std::string>& args);
81 cmdline(int argc, const char*const * argv);
83 void style(int style);
85 /** returns the canonical option prefix associated with the command_line_style
86 * In order of precedence:
87 * allow_long : allow_long
88 * allow_long_disguise : allow_long_disguise
89 * allow_dash_for_short : allow_short | allow_dash_for_short
90 * allow_slash_for_short: allow_short | allow_slash_for_short
92 * This is mainly used for the diagnostic messages in exceptions
94 int get_canonical_option_prefix();
96 void allow_unregistered();
98 void set_options_description(const options_description& desc);
99 void set_positional_options(
100 const positional_options_description& m_positional);
102 std::vector<option> run();
104 std::vector<option> parse_long_option(std::vector<std::string>& args);
105 std::vector<option> parse_short_option(std::vector<std::string>& args);
106 std::vector<option> parse_dos_option(std::vector<std::string>& args);
107 std::vector<option> parse_disguised_long_option(
108 std::vector<std::string>& args);
109 std::vector<option> parse_terminator(
110 std::vector<std::string>& args);
111 std::vector<option> handle_additional_parser(
112 std::vector<std::string>& args);
115 /** Set additional parser. This will be called for each token
116 of command line. If first string in pair is not empty,
117 then the token is considered matched by this parser,
118 and the first string will be considered an option name
119 (which can be long or short), while the second will be
120 option's parameter (if not empty).
121 Note that additional parser can match only one token.
123 void set_additional_parser(additional_parser p);
125 void extra_style_parser(style_parser s);
127 void check_style(int style) const;
129 bool is_style_active(style_t style) const;
131 void init(const std::vector<std::string>& args);
134 finish_option(option& opt,
135 std::vector<std::string>& other_tokens,
136 const std::vector<style_parser>& style_parsers);
139 std::vector<std::string> args;
141 bool m_allow_unregistered;
143 const options_description* m_desc;
144 const positional_options_description* m_positional;
146 additional_parser m_additional_parser;
147 style_parser m_style_parser;
150 void test_cmdline_detail();
154 #if defined(BOOST_MSVC)
155 # pragma warning (pop)