[857] | 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 |
|
---|
| 7 | #ifndef BOOST_CMDLINE_VP_2003_05_19
|
---|
| 8 | #define BOOST_CMDLINE_VP_2003_05_19
|
---|
| 9 |
|
---|
| 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>
|
---|
| 16 |
|
---|
| 17 |
|
---|
| 18 | #include <boost/detail/workaround.hpp>
|
---|
| 19 |
|
---|
| 20 | #include <boost/function.hpp>
|
---|
| 21 |
|
---|
| 22 | #include <string>
|
---|
| 23 | #include <vector>
|
---|
| 24 |
|
---|
| 25 | namespace boost { namespace program_options { namespace detail {
|
---|
| 26 |
|
---|
| 27 | /** Command line parser class. Main requirements were:
|
---|
| 28 | - Powerful enough to support all common uses.
|
---|
| 29 | - Simple and easy to learn/use.
|
---|
| 30 | - Minimal code size and external dependencies.
|
---|
| 31 | - Extensible for custom syntaxes.
|
---|
| 32 |
|
---|
| 33 | First all options are registered. After that, elements of command line
|
---|
| 34 | are extracted using operator++.
|
---|
| 35 |
|
---|
| 36 | For each element, user can find
|
---|
| 37 | - if it's an option or an argument
|
---|
| 38 | - name of the option
|
---|
| 39 | - index of the option
|
---|
| 40 | - option value(s), if any
|
---|
| 41 |
|
---|
| 42 | Sometimes the registered option name is not equal to the encountered
|
---|
| 43 | one, for example, because name abbreviation is supported. Therefore
|
---|
| 44 | two option names can be obtained:
|
---|
| 45 | - the registered one
|
---|
| 46 | - the one found at the command line
|
---|
| 47 |
|
---|
| 48 | There are lot of style options, which can be used to tune the command
|
---|
| 49 | line parsing. In addition, it's possible to install additional parser
|
---|
| 50 | which will process custom option styles.
|
---|
| 51 |
|
---|
| 52 | @todo mininal match length for guessing?
|
---|
| 53 | */
|
---|
| 54 | class BOOST_PROGRAM_OPTIONS_DECL cmdline {
|
---|
| 55 | public:
|
---|
| 56 |
|
---|
| 57 | typedef ::boost::program_options::command_line_style::style_t style_t;
|
---|
| 58 |
|
---|
| 59 | typedef function1<std::pair<std::string, std::string>,
|
---|
| 60 | const std::string&>
|
---|
| 61 | additional_parser;
|
---|
| 62 |
|
---|
| 63 | typedef function1<std::vector<option>, std::vector<std::string>&>
|
---|
| 64 | style_parser;
|
---|
| 65 |
|
---|
| 66 | /** Constructs a command line parser for (argc, argv) pair. Uses
|
---|
| 67 | style options passed in 'style', which should be binary or'ed values
|
---|
| 68 | of style_t enum. It can also be zero, in which case a "default"
|
---|
| 69 | style will be used. If 'allow_unregistered' is true, then allows
|
---|
| 70 | unregistered options. They will be assigned index 1 and are
|
---|
| 71 | assumed to have optional parameter.
|
---|
| 72 | */
|
---|
| 73 | cmdline(const std::vector<std::string>& args);
|
---|
| 74 |
|
---|
| 75 | /** @overload */
|
---|
| 76 | cmdline(int argc, const char*const * argv);
|
---|
| 77 |
|
---|
| 78 | void style(int style);
|
---|
| 79 | void allow_unregistered();
|
---|
| 80 |
|
---|
| 81 | void set_options_description(const options_description& desc);
|
---|
| 82 | void set_positional_options(
|
---|
| 83 | const positional_options_description& m_positional);
|
---|
| 84 |
|
---|
| 85 | std::vector<option> run();
|
---|
| 86 |
|
---|
| 87 | std::vector<option> parse_long_option(std::vector<std::string>& args);
|
---|
| 88 | std::vector<option> parse_short_option(std::vector<std::string>& args);
|
---|
| 89 | std::vector<option> parse_dos_option(std::vector<std::string>& args);
|
---|
| 90 | std::vector<option> parse_disguised_long_option(
|
---|
| 91 | std::vector<std::string>& args);
|
---|
| 92 | std::vector<option> parse_terminator(
|
---|
| 93 | std::vector<std::string>& args);
|
---|
| 94 | std::vector<option> handle_additional_parser(
|
---|
| 95 | std::vector<std::string>& args);
|
---|
| 96 |
|
---|
| 97 |
|
---|
| 98 | /** Set additional parser. This will be called for each token
|
---|
| 99 | of command line. If first string in pair is not empty,
|
---|
| 100 | then the token is considered matched by this parser,
|
---|
| 101 | and the first string will be considered an option name
|
---|
| 102 | (which can be long or short), while the second will be
|
---|
| 103 | option's parameter (if not empty).
|
---|
| 104 | Note that additional parser can match only one token.
|
---|
| 105 | */
|
---|
| 106 | void set_additional_parser(additional_parser p);
|
---|
| 107 |
|
---|
| 108 | void extra_style_parser(style_parser s);
|
---|
| 109 |
|
---|
| 110 | void check_style(int style) const;
|
---|
| 111 |
|
---|
| 112 |
|
---|
| 113 | void init(const std::vector<std::string>& args);
|
---|
| 114 |
|
---|
| 115 | void
|
---|
| 116 | finish_option(option& opt,
|
---|
| 117 | std::vector<std::string>& other_tokens);
|
---|
| 118 |
|
---|
| 119 | // Copies of input.
|
---|
| 120 | std::vector<std::string> args;
|
---|
| 121 | style_t m_style;
|
---|
| 122 | bool m_allow_unregistered;
|
---|
| 123 |
|
---|
| 124 | const options_description* m_desc;
|
---|
| 125 | const positional_options_description* m_positional;
|
---|
| 126 |
|
---|
| 127 | additional_parser m_additional_parser;
|
---|
| 128 | style_parser m_style_parser;
|
---|
| 129 | };
|
---|
| 130 |
|
---|
| 131 | void test_cmdline_detail();
|
---|
| 132 |
|
---|
| 133 | }}}
|
---|
| 134 |
|
---|
| 135 | #endif
|
---|
| 136 |
|
---|