recipes.dox 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /** @page recipes Recipes
  2. Here, we'll give solution for some desires which seem common.
  3. @section recipe_parameter_validation How to check for correct option value types and assign them?
  4. There's the boost::program_options::parameter function. It
  5. returns a object, which, if passed as the second parameter
  6. to boost::program_options::option_description constructor,
  7. establishes correct validation routine. A simple example
  8. is
  9. @code
  10. options_description desc;
  11. desc.add_options()
  12. ("foo", parameter<int>("arg"), "obscure option")
  13. ;
  14. @endcode
  15. If you pass an address of <tt>int</tt> variable as the second
  16. parameter of the <tt>parameter</tt> function, that variable will
  17. be assigned the options's value.
  18. @sa @ref variables_map
  19. @section recipe_lazy What if I don't want to declare any options?
  20. I'm not sure this is good idea. In particular, mistyped options
  21. will be silently ignored, leading to possible user surprises.
  22. Futher, the boost::program_options::cmdline class was specially
  23. designed to be very lightweight.
  24. Anyway, there's a version of the parse_command_line function
  25. which does not take an options_description instance. Also, the
  26. cmdline class ctor accepts an 'allow_unregistered' parameter.
  27. In both cases, all options will be allowed, and treated as if
  28. they have optional parameter.
  29. Note that with the default style,
  30. @verbatim
  31. --foo bar
  32. @endverbatim
  33. will be taken as option "foo" with value "bar", which is
  34. probably not correct. You should disable option parameter in
  35. the next token to avoid problems.
  36. @sa boost::program_options::cmdline
  37. @section recipe_multiple_modules I have several separate modules which must controlled by options. What am I to do?
  38. There are several solutions.
  39. @subsection sb1 Everything's global
  40. You can create a single instance of the <tt>options_description</tt> class
  41. somewhere near <tt>main</tt>. All the modules will export their own
  42. options using other <tt>options_description</tt> instances which can
  43. be added to the main one. After that, you'd parse command line and
  44. config files. The parsing results will be stored in one variables_map,
  45. which will be passed to all modules, which can work with their own
  46. options.
  47. @subsection sb2 Private option data
  48. Assume one of the modules does not like to see irrelevant options.
  49. For example, it outputs a configuration file for other program, and
  50. irrelevant options will confuse that program.
  51. It's possible to give the module only the options that it has
  52. registered. First, the module provides an options_description instance
  53. which is added to the global one. Second the command line is parsed
  54. to produce an options_and_arguments instance. Lastly, the <tt>store</tt>
  55. function is called. If passed the options_description instance previously
  56. returned by the module, it will store only options specified in that
  57. instance.
  58. @sa @ref multiple_modules
  59. @subsection sb3 Unique option names
  60. The most general solution would be to give unique names to options
  61. for different modules. One module will declare option "module1.server",
  62. and another would declare "module2.internal_checks". Of course, there
  63. can be global options like "verbosity", declared by <tt>main</tt> and
  64. used by all modules.
  65. This solution avoids all possible name clashes between modules. On
  66. the other hand, longer option names can be less user-friendly. This
  67. problem can be alleviated if module prefix is used only for less
  68. common option, needed for fine-tuning.
  69. */