rationale.dox 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /** @page rationale Rationale
  2. @section code_size Focus on code size
  3. The program options library has two important properties:
  4. - runtime performance is not important. After all, command line processing
  5. is done only once, and the amount of data is small.
  6. - code size matters. Since parsing command line is utility task, users
  7. won't be glad to have lots of code linked to every binary which has
  8. options.
  9. For the above reasons, the the library is designed so that it can be easily
  10. used as shared library, with minimum code on the side of main application.
  11. In particular, templates are used only where necessary, for example for
  12. validation of user-defined types. In other places, boost::function is
  13. used to allow customization, but keep templates out of the public
  14. interface.
  15. @section string_vs_enums Strings vs. enums
  16. In some places, the library uses strings to convey information that
  17. could be represented by enumerations or values. For example,
  18. the program_options::option_description class allows to add "?" to the
  19. parameter name to specify that the parameter is optional. For another
  20. example, while program_options::cmdline class allows to obtain the
  21. index of option, it does not require to specify an index for each option,
  22. and it's possible to tell options by their names.
  23. Such interface appears to be much more usable. If we were using
  24. enumeration for different properties of parameter, there would be
  25. another argument to many functions, the need to type long, possible
  26. qualified names, and little advantage.
  27. That little advantage is that if you type a wrong enumeration name,
  28. you'd get a compile error. If you type '!' instead of '?' after parameter
  29. name, you'd get incorrect behaviour. However, such errors are deemed
  30. rare.
  31. @section char_vs_string const char* vs. std::string
  32. Most of the interface uses const char* where std::string seems a natural
  33. choice. The reason is that those functions are called many times: for
  34. example to declare all options. They are typically called with string
  35. literals, and implicit conversion to string appears to take a lot of
  36. code space. Providing both std::string and const char* version would
  37. considerably bloat the interface. Since passing std::string is considered
  38. rare, only const char* versions are provided.
  39. @section init_syntax Initialization syntax
  40. The syntax used for creating options_description instance was designed to
  41. be as easy as possible in the most common case. Consider:
  42. @code
  43. desc.add_options()
  44. ("verbose", "", "verbosity level")
  45. ("magic", "int", "magic value").notify(some_func)
  46. ;
  47. @endcode
  48. Here, most common properties of options: name, presense of parameter and
  49. description, are specified very concisely, and additional properties can
  50. be given quite naturally, too.
  51. Another possibility would be:
  52. @code
  53. option_description d1(...), d2(...);
  54. desc.add(d1 & d2);
  55. @endcode
  56. or
  57. @code
  58. option_description d1(...), d2(...);
  59. desc = d1, d2;
  60. @endcode
  61. The drawback is the need to explicitly create new objects and give names
  62. to them. The latter problem can be helped if objects are created inside
  63. expressions:
  64. @code
  65. desc = option_description(...), option_description(...)
  66. @endcode
  67. but there's still extra typing.
  68. @section help_handling Handling of --help
  69. It was suggested by Gennadiy Rozental that occurrence of <tt>--help</tt>
  70. on command line results in throwing an exception. Actually, the
  71. &quot;special&quot; option must have been configurable. This was not
  72. implemented, because applications might reasonable want to process
  73. the rest of command line even of <tt>--help</tt> was seen. For example,
  74. <tt>--verbose</tt> option can control how much help should be output,
  75. or there may be several subcommand with different help screens.
  76. */