program_options.dox 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /** @mainpage Program options documentation
  2. @section scope Scope
  3. Briefly, the library should allow program developers to obtain
  4. <em>program options</em>, i.e. (name,value) pairs from the user,
  5. via conventional methods such as command line and config file.
  6. Necessary facilities include:
  7. - parse command line
  8. - parse config files
  9. - perform semantic validation on input, such as checking for correct
  10. type of parameters, and storing values.
  11. - combine all inputs together, so that all program options can
  12. be obtained in one place.
  13. @section goals Goals
  14. The fundamental goals for this library were:
  15. - it should be more convenient to use it than parse command line by hand,
  16. even when the number of possible options is 2,
  17. - all popular command line styles should be supported,
  18. - "you pay for what you use" principle is important: simple utilities
  19. need not be forced to depend on excessive amount of code.
  20. - it must be possible to validate option values, convert them to required
  21. types, and store either in program variables, or in data structures
  22. maintained by the library.
  23. - data from command line and config file should be usable together, and
  24. alternative program option sources (such as registry) should be
  25. possible.
  26. @section design_overview Design overview
  27. To meet the stated goals, the library uses a layered architecture.
  28. -# At the bottom, there are two parser classes,
  29. boost::program_options::cmdline and
  30. boost::program_options::config_file.
  31. They are responsible for syntax matters only and provide simple
  32. iterator-like interface.
  33. -# The boost::program_options::options_and_arguments holds the result of parsing command line or
  34. config file. It is still concerned with syntax only and holds precisely
  35. what is found on command line. There's a couple of associated parse
  36. functions (
  37. @ref parse_cmdline_func "1",
  38. @ref parse_config_file_func "2"),
  39. which relieve the user from the need to iterate over options
  40. and arguments manually.
  41. -# The class boost::program_options::options_description is a high-level
  42. description of allowed
  43. program options, which does not depend on concrete parser class. In
  44. addition, it can be used to provide help message. There are parse
  45. functions which return options_and_arguments given options_description.
  46. -# The options_description class also has semantic responsibilities. It's
  47. possible to specify validators for option, their default values, and the
  48. like. There's a function boost::program_options::perform_semantic_actions,
  49. which handles this information and returns a map of option values.
  50. -# Finally, at the top, there boost::program_options::variables_map class.
  51. It's possible to
  52. store options in it, and obtain them later. Another feature is that
  53. different variable_map instances can be linked together, so that both
  54. command line and config file data is used. Additional option sources can
  55. be added at this level.
  56. @section futher_reading Futher reading
  57. To get further information about the library, you might want to read
  58. the documentation for the classes referenced above. Another possibility
  59. is to look through the examples:
  60. - @ref options_description "simple usage"
  61. - @ref variables_map "parsing with validation and assignment to program variables"
  62. - @ref multiple_sources "using command line and config file together"
  63. - @ref custom_syntax "customized options syntax"
  64. - @ref real_example "real example"
  65. - @ref custom_validator "custom validator"
  66. - @ref multiple_modules "possible approach for multi-module programs"
  67. - @ref cmdline "low level cmdline parsing"
  68. Finally, you might want the check out the @ref recipes "recipes" page.
  69. */
  70. /** @page examples Examples
  71. - @ref options_description "simple usage"
  72. - @ref variables_map "parsing with validation and assignment to program variables"
  73. - @ref multiple_sources "using command line and config file together"
  74. - @ref custom_syntax "customized options syntax"
  75. - @ref real_example "real example"
  76. - @ref custom_validator "custom validator"
  77. - @ref multiple_modules "possible approach for multi-module programs"
  78. - @ref cmdline "low level cmdline parsing"
  79. */
  80. /** @page options_description Options description
  81. Example of quite a simple usage. Options are registered and the
  82. command line is parsed. The user is responsible to interpreting the
  83. option values. This also how automatic help message.
  84. @include options_description.cpp
  85. */
  86. /** @page variables_map Variables map
  87. In this example, the <tt>parameter</tt> function is used to enable
  88. validation of options (i.e. checking that they are of correct type).
  89. The option values are also stored in program variables.
  90. @include variables_map.cpp
  91. */
  92. /** @page multiple_sources Multiple sources
  93. It is possible for program options to come from different sources.
  94. Here, the command line and a config file are used, and the values
  95. specified in both are combined, with preferrence given to the
  96. command line.
  97. @include multiple_sources.cpp
  98. */
  99. /** @page custom_syntax Custom syntax
  100. Some applications use a custom syntax for the command line. In this
  101. example, the gcc style of &quot;-fbar&quot;/&quot;-f&quot; is handled.
  102. @include custom_syntax.cpp
  103. */
  104. /** @page real_example A real example
  105. Shows how to use custom option description class and custom formatter.
  106. Also validates some option relationship.
  107. @include real.cpp
  108. */
  109. /** @page multiple_modules Multiple modules
  110. Large programs are likely to have several modules which want to use
  111. some options. One possible approach is show here.
  112. @sa @ref recipe_multiple_modules
  113. @include multiple_modules.cpp
  114. */
  115. /** @page custom_validator Custom validator
  116. It's possible to plug in arbitrary function for converting the string
  117. value from the command line to the value used in your program. The
  118. example below illustrates this.
  119. @include regex.cpp
  120. */
  121. /** @page cmdline The cmdline class
  122. When validation or automatic help message are not needed, it's possible
  123. to use low-level boost::program_options::cmdline class, like shown
  124. in this example.
  125. @include cmdline.cpp
  126. */