cmdline.hpp 5.6 KB

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