Chris@16
|
1 // Copyright Vladimir Prus 2002-2004.
|
Chris@16
|
2 // Distributed under the Boost Software License, Version 1.0.
|
Chris@16
|
3 // (See accompanying file LICENSE_1_0.txt
|
Chris@16
|
4 // or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
5
|
Chris@16
|
6
|
Chris@16
|
7 #ifndef BOOST_CMDLINE_VP_2003_05_19
|
Chris@16
|
8 #define BOOST_CMDLINE_VP_2003_05_19
|
Chris@16
|
9
|
Chris@16
|
10 #include <boost/program_options/config.hpp>
|
Chris@16
|
11 #include <boost/program_options/errors.hpp>
|
Chris@16
|
12 #include <boost/program_options/cmdline.hpp>
|
Chris@16
|
13 #include <boost/program_options/option.hpp>
|
Chris@16
|
14 #include <boost/program_options/options_description.hpp>
|
Chris@16
|
15 #include <boost/program_options/positional_options.hpp>
|
Chris@16
|
16
|
Chris@16
|
17
|
Chris@16
|
18 #include <boost/detail/workaround.hpp>
|
Chris@16
|
19
|
Chris@16
|
20 #include <boost/function.hpp>
|
Chris@16
|
21
|
Chris@16
|
22 #include <string>
|
Chris@16
|
23 #include <vector>
|
Chris@16
|
24
|
Chris@16
|
25 #if defined(BOOST_MSVC)
|
Chris@16
|
26 # pragma warning (push)
|
Chris@16
|
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'
|
Chris@16
|
28 #endif
|
Chris@16
|
29
|
Chris@16
|
30 namespace boost { namespace program_options { namespace detail {
|
Chris@16
|
31
|
Chris@16
|
32 /** Command line parser class. Main requirements were:
|
Chris@16
|
33 - Powerful enough to support all common uses.
|
Chris@16
|
34 - Simple and easy to learn/use.
|
Chris@16
|
35 - Minimal code size and external dependencies.
|
Chris@16
|
36 - Extensible for custom syntaxes.
|
Chris@16
|
37
|
Chris@16
|
38 First all options are registered. After that, elements of command line
|
Chris@16
|
39 are extracted using operator++.
|
Chris@16
|
40
|
Chris@16
|
41 For each element, user can find
|
Chris@16
|
42 - if it's an option or an argument
|
Chris@16
|
43 - name of the option
|
Chris@16
|
44 - index of the option
|
Chris@16
|
45 - option value(s), if any
|
Chris@16
|
46
|
Chris@16
|
47 Sometimes the registered option name is not equal to the encountered
|
Chris@16
|
48 one, for example, because name abbreviation is supported. Therefore
|
Chris@16
|
49 two option names can be obtained:
|
Chris@16
|
50 - the registered one
|
Chris@16
|
51 - the one found at the command line
|
Chris@16
|
52
|
Chris@16
|
53 There are lot of style options, which can be used to tune the command
|
Chris@16
|
54 line parsing. In addition, it's possible to install additional parser
|
Chris@16
|
55 which will process custom option styles.
|
Chris@16
|
56
|
Chris@16
|
57 @todo mininal match length for guessing?
|
Chris@16
|
58 */
|
Chris@16
|
59 class BOOST_PROGRAM_OPTIONS_DECL cmdline {
|
Chris@16
|
60 public:
|
Chris@16
|
61
|
Chris@16
|
62 typedef ::boost::program_options::command_line_style::style_t style_t;
|
Chris@16
|
63
|
Chris@16
|
64 typedef function1<std::pair<std::string, std::string>,
|
Chris@16
|
65 const std::string&>
|
Chris@16
|
66 additional_parser;
|
Chris@16
|
67
|
Chris@16
|
68 typedef function1<std::vector<option>, std::vector<std::string>&>
|
Chris@16
|
69 style_parser;
|
Chris@16
|
70
|
Chris@16
|
71 /** Constructs a command line parser for (argc, argv) pair. Uses
|
Chris@16
|
72 style options passed in 'style', which should be binary or'ed values
|
Chris@16
|
73 of style_t enum. It can also be zero, in which case a "default"
|
Chris@16
|
74 style will be used. If 'allow_unregistered' is true, then allows
|
Chris@16
|
75 unregistered options. They will be assigned index 1 and are
|
Chris@16
|
76 assumed to have optional parameter.
|
Chris@16
|
77 */
|
Chris@16
|
78 cmdline(const std::vector<std::string>& args);
|
Chris@16
|
79
|
Chris@16
|
80 /** @overload */
|
Chris@16
|
81 cmdline(int argc, const char*const * argv);
|
Chris@16
|
82
|
Chris@16
|
83 void style(int style);
|
Chris@16
|
84
|
Chris@16
|
85 /** returns the canonical option prefix associated with the command_line_style
|
Chris@16
|
86 * In order of precedence:
|
Chris@16
|
87 * allow_long : allow_long
|
Chris@16
|
88 * allow_long_disguise : allow_long_disguise
|
Chris@16
|
89 * allow_dash_for_short : allow_short | allow_dash_for_short
|
Chris@16
|
90 * allow_slash_for_short: allow_short | allow_slash_for_short
|
Chris@16
|
91 *
|
Chris@16
|
92 * This is mainly used for the diagnostic messages in exceptions
|
Chris@16
|
93 */
|
Chris@16
|
94 int get_canonical_option_prefix();
|
Chris@16
|
95
|
Chris@16
|
96 void allow_unregistered();
|
Chris@16
|
97
|
Chris@16
|
98 void set_options_description(const options_description& desc);
|
Chris@16
|
99 void set_positional_options(
|
Chris@16
|
100 const positional_options_description& m_positional);
|
Chris@16
|
101
|
Chris@16
|
102 std::vector<option> run();
|
Chris@16
|
103
|
Chris@16
|
104 std::vector<option> parse_long_option(std::vector<std::string>& args);
|
Chris@16
|
105 std::vector<option> parse_short_option(std::vector<std::string>& args);
|
Chris@16
|
106 std::vector<option> parse_dos_option(std::vector<std::string>& args);
|
Chris@16
|
107 std::vector<option> parse_disguised_long_option(
|
Chris@16
|
108 std::vector<std::string>& args);
|
Chris@16
|
109 std::vector<option> parse_terminator(
|
Chris@16
|
110 std::vector<std::string>& args);
|
Chris@16
|
111 std::vector<option> handle_additional_parser(
|
Chris@16
|
112 std::vector<std::string>& args);
|
Chris@16
|
113
|
Chris@16
|
114
|
Chris@16
|
115 /** Set additional parser. This will be called for each token
|
Chris@16
|
116 of command line. If first string in pair is not empty,
|
Chris@16
|
117 then the token is considered matched by this parser,
|
Chris@16
|
118 and the first string will be considered an option name
|
Chris@16
|
119 (which can be long or short), while the second will be
|
Chris@16
|
120 option's parameter (if not empty).
|
Chris@16
|
121 Note that additional parser can match only one token.
|
Chris@16
|
122 */
|
Chris@16
|
123 void set_additional_parser(additional_parser p);
|
Chris@16
|
124
|
Chris@16
|
125 void extra_style_parser(style_parser s);
|
Chris@16
|
126
|
Chris@16
|
127 void check_style(int style) const;
|
Chris@16
|
128
|
Chris@16
|
129 bool is_style_active(style_t style) const;
|
Chris@16
|
130
|
Chris@16
|
131 void init(const std::vector<std::string>& args);
|
Chris@16
|
132
|
Chris@16
|
133 void
|
Chris@16
|
134 finish_option(option& opt,
|
Chris@16
|
135 std::vector<std::string>& other_tokens,
|
Chris@16
|
136 const std::vector<style_parser>& style_parsers);
|
Chris@16
|
137
|
Chris@16
|
138 // Copies of input.
|
Chris@16
|
139 std::vector<std::string> args;
|
Chris@16
|
140 style_t m_style;
|
Chris@16
|
141 bool m_allow_unregistered;
|
Chris@16
|
142
|
Chris@16
|
143 const options_description* m_desc;
|
Chris@16
|
144 const positional_options_description* m_positional;
|
Chris@16
|
145
|
Chris@16
|
146 additional_parser m_additional_parser;
|
Chris@16
|
147 style_parser m_style_parser;
|
Chris@16
|
148 };
|
Chris@16
|
149
|
Chris@16
|
150 void test_cmdline_detail();
|
Chris@16
|
151
|
Chris@16
|
152 }}}
|
Chris@16
|
153
|
Chris@16
|
154 #if defined(BOOST_MSVC)
|
Chris@16
|
155 # pragma warning (pop)
|
Chris@16
|
156 #endif
|
Chris@16
|
157
|
Chris@16
|
158 #endif
|
Chris@16
|
159
|