| 1 | <html> |
|---|
| 2 | <head> |
|---|
| 3 | <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> |
|---|
| 4 | <title>Library Overview</title> |
|---|
| 5 | <link rel="stylesheet" href="../boostbook.css" type="text/css"> |
|---|
| 6 | <meta name="generator" content="DocBook XSL Stylesheets V1.68.1"> |
|---|
| 7 | <link rel="start" href="../index.html" title="The Boost C++ Libraries BoostBook Documentation Subset"> |
|---|
| 8 | <link rel="up" href="../program_options.html" title="Chapter 10. Boost.Program_options"> |
|---|
| 9 | <link rel="prev" href="tutorial.html" title="Tutorial"> |
|---|
| 10 | <link rel="next" href="howto.html" title="How To"> |
|---|
| 11 | </head> |
|---|
| 12 | <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> |
|---|
| 13 | <table cellpadding="2" width="100%"> |
|---|
| 14 | <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../boost.png"></td> |
|---|
| 15 | <td align="center"><a href="../../../index.htm">Home</a></td> |
|---|
| 16 | <td align="center"><a href="../../../libs/libraries.htm">Libraries</a></td> |
|---|
| 17 | <td align="center"><a href="../../../people/people.htm">People</a></td> |
|---|
| 18 | <td align="center"><a href="../../../more/faq.htm">FAQ</a></td> |
|---|
| 19 | <td align="center"><a href="../../../more/index.htm">More</a></td> |
|---|
| 20 | </table> |
|---|
| 21 | <hr> |
|---|
| 22 | <div class="spirit-nav"> |
|---|
| 23 | <a accesskey="p" href="tutorial.html"><img src="../images/prev.png" alt="Prev"></a><a accesskey="u" href="../program_options.html"><img src="../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../images/home.png" alt="Home"></a><a accesskey="n" href="howto.html"><img src="../images/next.png" alt="Next"></a> |
|---|
| 24 | </div> |
|---|
| 25 | <div class="section" lang="en"> |
|---|
| 26 | <div class="titlepage"><div><div><h2 class="title" style="clear: both"> |
|---|
| 27 | <a name="program_options.overview"></a>Library Overview</h2></div></div></div> |
|---|
| 28 | <div class="toc"><dl> |
|---|
| 29 | <dt><span class="section"><a href="overview.html#id1591423">Options Description Component</a></span></dt> |
|---|
| 30 | <dt><span class="section"><a href="overview.html#id1592003">Parsers Component</a></span></dt> |
|---|
| 31 | <dt><span class="section"><a href="overview.html#id1592104">Storage Component</a></span></dt> |
|---|
| 32 | <dt><span class="section"><a href="overview.html#id1592208">Specific parsers</a></span></dt> |
|---|
| 33 | <dt><span class="section"><a href="overview.html#id1592393">Annotated List of Symbols</a></span></dt> |
|---|
| 34 | </dl></div> |
|---|
| 35 | <p>In the tutorial section, we saw several examples of library usage. |
|---|
| 36 | Here we will describe the overall library design including the primary |
|---|
| 37 | components and their function. |
|---|
| 38 | </p> |
|---|
| 39 | <p>The library has three main components: |
|---|
| 40 | </p> |
|---|
| 41 | <div class="itemizedlist"><ul type="disc"> |
|---|
| 42 | <li><p>The options description component, which describes the allowed options |
|---|
| 43 | and what to do with the values of the options. |
|---|
| 44 | </p></li> |
|---|
| 45 | <li><p>The parsers component, which uses this information to find option names |
|---|
| 46 | and values in the input sources and return them. |
|---|
| 47 | </p></li> |
|---|
| 48 | <li><p>The storage component, which provides the |
|---|
| 49 | interface to access the value of an option. It also converts the string |
|---|
| 50 | representation of values that parsers return into desired C++ types. |
|---|
| 51 | </p></li> |
|---|
| 52 | </ul></div> |
|---|
| 53 | <p> |
|---|
| 54 | </p> |
|---|
| 55 | <p>To be a little more concrete, the <code class="computeroutput">options_description</code> |
|---|
| 56 | class is from the options description component, the |
|---|
| 57 | <code class="computeroutput">parse_command_line</code> function is from the parsers component, and the |
|---|
| 58 | <code class="computeroutput">variables_map</code> class is from the storage component. </p> |
|---|
| 59 | <p>In the tutorial we've learned how those components can be used by the |
|---|
| 60 | <code class="computeroutput">main</code> function to parse the command line and config |
|---|
| 61 | file. Before going into the details of each component, a few notes about |
|---|
| 62 | the world outside of <code class="computeroutput">main</code>. |
|---|
| 63 | </p> |
|---|
| 64 | <p> |
|---|
| 65 | For that outside world, the storage component is the most important. It |
|---|
| 66 | provides a class which stores all option values and that class can be |
|---|
| 67 | freely passed around your program to modules which need access to the |
|---|
| 68 | options. All the other components can be used only in the place where |
|---|
| 69 | the actual parsing is the done. However, it might also make sense for the |
|---|
| 70 | individual program modules to describe their options and pass them to the |
|---|
| 71 | main module, which will merge all options. Of course, this is only |
|---|
| 72 | important when the number of options is large and declaring them in one |
|---|
| 73 | place becomes troublesome. |
|---|
| 74 | </p> |
|---|
| 75 | <div class="section" lang="en"> |
|---|
| 76 | <div class="titlepage"><div><div><h3 class="title"> |
|---|
| 77 | <a name="id1591423"></a>Options Description Component</h3></div></div></div> |
|---|
| 78 | <div class="toc"><dl> |
|---|
| 79 | <dt><span class="section"><a href="overview.html#id1591676">Syntactic Information</a></span></dt> |
|---|
| 80 | <dt><span class="section"><a href="overview.html#id1591838">Semantic Information</a></span></dt> |
|---|
| 81 | <dt><span class="section"><a href="overview.html#id1591885">Positional Options</a></span></dt> |
|---|
| 82 | </dl></div> |
|---|
| 83 | <p>The options description component has three main classes: |
|---|
| 84 | <code class="computeroutput"><a href="../boost/program_options/option_description.html" title="Class option_description">option_description</a></code>, <code class="computeroutput"><a href="../boost/program_options/value_semantic.html" title="Class value_semantic">value_semantic</a></code> and <code class="computeroutput"><a href="../boost/program_options/options_description.html" title="Class options_description">options_description</a></code>. The |
|---|
| 85 | first two together describe a single option. The <code class="computeroutput"><a href="../boost/program_options/option_description.html" title="Class option_description">option_description</a></code> |
|---|
| 86 | class contains the option's name, description and a pointer to <code class="computeroutput"><a href="../boost/program_options/value_semantic.html" title="Class value_semantic">value_semantic</a></code>, |
|---|
| 87 | which, in turn, knows the type of the option's value and can parse the value, |
|---|
| 88 | apply the default value, and so on. The <code class="computeroutput"><a href="../boost/program_options/options_description.html" title="Class options_description">options_description</a></code> class is a |
|---|
| 89 | container for instances of <code class="computeroutput"><a href="../boost/program_options/option_description.html" title="Class option_description">option_description</a></code>. |
|---|
| 90 | </p> |
|---|
| 91 | <p>For almost every library, those classes could be created in a |
|---|
| 92 | conventional way: that is, you'd create new options using constructors and |
|---|
| 93 | then call the <code class="computeroutput">add</code> method of <code class="computeroutput"><a href="../boost/program_options/options_description.html" title="Class options_description">options_description</a></code>. However, |
|---|
| 94 | that's overly verbose for declaring 20 or 30 options. This concern led |
|---|
| 95 | to creation of the syntax that you've already seen: |
|---|
| 96 | </p> |
|---|
| 97 | <pre class="programlisting"> |
|---|
| 98 | options_description desc; |
|---|
| 99 | desc.add_options() |
|---|
| 100 | ("help", "produce help") |
|---|
| 101 | ("optimization", value<int>()->default_value(10), "optimization level") |
|---|
| 102 | ; |
|---|
| 103 | </pre> |
|---|
| 104 | <p> |
|---|
| 105 | </p> |
|---|
| 106 | <p>The call to the <code class="computeroutput">value</code> function creates an instance of |
|---|
| 107 | a class derived from the <code class="computeroutput">value_semantic</code> class: <code class="computeroutput">typed_value</code>. |
|---|
| 108 | That class contains the code to parse |
|---|
| 109 | values of a specific type, and contains a number of methods which can be |
|---|
| 110 | called by the user to specify additional information. (This |
|---|
| 111 | essentially emulates named parameters of the constructor.) Calls to |
|---|
| 112 | <code class="computeroutput">operator()</code> on the object returned by <code class="computeroutput">add_options</code> |
|---|
| 113 | forward arguments to the constructor of the <code class="computeroutput">option_description</code> |
|---|
| 114 | class and add the new instance. |
|---|
| 115 | </p> |
|---|
| 116 | <p> |
|---|
| 117 | Note that in addition to the |
|---|
| 118 | <code class="computeroutput">value</code>, library provides the <code class="computeroutput">bool_switch</code> |
|---|
| 119 | function, and user can write his own function which will return |
|---|
| 120 | other subclasses of <code class="computeroutput">value_semantic</code> with |
|---|
| 121 | different behaviour. For the remainder of this section, we'll talk only |
|---|
| 122 | about the <code class="computeroutput">value</code> function. |
|---|
| 123 | </p> |
|---|
| 124 | <p>The information about an option is divided into syntactic and |
|---|
| 125 | semantic. Syntactic information includes the name of the option and the |
|---|
| 126 | number of tokens which can be used to specify the value. This |
|---|
| 127 | information is used by parsers to group tokens into (name, value) pairs, |
|---|
| 128 | where value is just a vector of strings |
|---|
| 129 | (<code class="computeroutput">std::vector<std::string></code>). The semantic layer |
|---|
| 130 | is responsible for converting the value of the option into more usable C++ |
|---|
| 131 | types. |
|---|
| 132 | </p> |
|---|
| 133 | <p>This separation is an important part of library design. The parsers |
|---|
| 134 | use only the syntactic layer, which takes away some of the freedom to |
|---|
| 135 | use overly complex structures. For example, it's not easy to parse |
|---|
| 136 | syntax like: </p> |
|---|
| 137 | <pre class="screen">calc --expression=1 + 2/3</pre> |
|---|
| 138 | <p> because it's not |
|---|
| 139 | possible to parse </p> |
|---|
| 140 | <pre class="screen">1 + 2/3</pre> |
|---|
| 141 | <p> without knowing that it's a C |
|---|
| 142 | expression. With a little help from the user the task becomes trivial, |
|---|
| 143 | and the syntax clear: </p> |
|---|
| 144 | <pre class="screen">calc --expression="1 + 2/3"</pre> |
|---|
| 145 | <p> |
|---|
| 146 | </p> |
|---|
| 147 | <div class="section" lang="en"> |
|---|
| 148 | <div class="titlepage"><div><div><h4 class="title"> |
|---|
| 149 | <a name="id1591676"></a>Syntactic Information</h4></div></div></div> |
|---|
| 150 | <div class="toc"><dl><dt><span class="section"><a href="overview.html#id1591766">Description formatting</a></span></dt></dl></div> |
|---|
| 151 | <p>The syntactic information is provided by the |
|---|
| 152 | <code class="computeroutput"><a href="../boost/program_options/options_description.html" title="Class options_description">boost::program_options::options_description</a></code> class |
|---|
| 153 | and some methods of the |
|---|
| 154 | <code class="computeroutput"><a href="../boost/program_options/value_semantic.html" title="Class value_semantic">boost::program_options::value_semantic</a></code> class |
|---|
| 155 | and includes: |
|---|
| 156 | </p> |
|---|
| 157 | <div class="itemizedlist"><ul type="disc"> |
|---|
| 158 | <li><p> |
|---|
| 159 | name of the option, used to identify the option inside the |
|---|
| 160 | program, |
|---|
| 161 | </p></li> |
|---|
| 162 | <li><p> |
|---|
| 163 | description of the option, which can be presented to the user, |
|---|
| 164 | </p></li> |
|---|
| 165 | <li><p> |
|---|
| 166 | the allowed number of source tokens that comprise options's |
|---|
| 167 | value, which is used during parsing. |
|---|
| 168 | </p></li> |
|---|
| 169 | </ul></div> |
|---|
| 170 | <p> |
|---|
| 171 | </p> |
|---|
| 172 | <p>Consider the following example: |
|---|
| 173 | </p> |
|---|
| 174 | <pre class="programlisting"> |
|---|
| 175 | options_description desc; |
|---|
| 176 | desc.add_options() |
|---|
| 177 | ("help", "produce help message") |
|---|
| 178 | ("compression", value<string>(), "compression level") |
|---|
| 179 | ("verbose", value<string>()->zero_token(), "verbosity level") |
|---|
| 180 | ("email", value<string>()->multitoken(), "email to send to") |
|---|
| 181 | ; |
|---|
| 182 | </pre> |
|---|
| 183 | <p> |
|---|
| 184 | For the first parameter, we specify only the name and the |
|---|
| 185 | description. No value can be specified in the parsed source. |
|---|
| 186 | For the first option, the user must specify a value, using a single |
|---|
| 187 | token. For the third option, the user may either provide a single token |
|---|
| 188 | for the value, or no token at all. For the last option, the value can |
|---|
| 189 | span several tokens. For example, the following command line is OK: |
|---|
| 190 | </p> |
|---|
| 191 | <pre class="screen"> |
|---|
| 192 | test --help --compression 10 --verbose --email beadle@mars beadle2@mars |
|---|
| 193 | </pre> |
|---|
| 194 | <p> |
|---|
| 195 | </p> |
|---|
| 196 | <div class="section" lang="en"> |
|---|
| 197 | <div class="titlepage"><div><div><h5 class="title"> |
|---|
| 198 | <a name="id1591766"></a>Description formatting</h5></div></div></div> |
|---|
| 199 | <p> |
|---|
| 200 | Sometimes the description can get rather long, for example, when |
|---|
| 201 | several option's values need separate documentation. Below we |
|---|
| 202 | describe some simple formatting mechanisms you can use. |
|---|
| 203 | </p> |
|---|
| 204 | <p>The description string has one or more paragraphs, separated by |
|---|
| 205 | the newline character ('\n'). When an option is output, the library |
|---|
| 206 | will compute the indentation for options's description. Each of the |
|---|
| 207 | paragraph is output as a separate line with that intentation. If |
|---|
| 208 | a paragraph does not fit on one line it is spanned over multiple |
|---|
| 209 | lines (which will have the same indentation). |
|---|
| 210 | </p> |
|---|
| 211 | <p>You may specify additional indent for the first specified by |
|---|
| 212 | inserting spaces at the beginning of a paragraph. For example: |
|---|
| 213 | </p> |
|---|
| 214 | <pre class="programlisting"> |
|---|
| 215 | options.add_options() |
|---|
| 216 | ("help", " A long help msg a long help msg a long help msg a long help |
|---|
| 217 | msg a long help msg a long help msg a long help msg a long help msg ") |
|---|
| 218 | ; |
|---|
| 219 | </pre> |
|---|
| 220 | <p> |
|---|
| 221 | will specify a four-space indent for the first line. The output will |
|---|
| 222 | look like: |
|---|
| 223 | </p> |
|---|
| 224 | <pre class="screen"> |
|---|
| 225 | --help A long help msg a long |
|---|
| 226 | help msg a long help msg |
|---|
| 227 | a long help msg a long |
|---|
| 228 | help msg a long help msg |
|---|
| 229 | a long help msg a long |
|---|
| 230 | help msg |
|---|
| 231 | |
|---|
| 232 | </pre> |
|---|
| 233 | <p> |
|---|
| 234 | </p> |
|---|
| 235 | <p>For the case where line is wrapped, you can want an additional |
|---|
| 236 | indent for wrapped text. This can be done by |
|---|
| 237 | inserting a tabulator character ('\t') at the desired position. For |
|---|
| 238 | example: |
|---|
| 239 | </p> |
|---|
| 240 | <pre class="programlisting"> |
|---|
| 241 | options.add_options() |
|---|
| 242 | ("well_formated", "As you can see this is a very well formatted |
|---|
| 243 | option description.\n" |
|---|
| 244 | "You can do this for example:\n\n" |
|---|
| 245 | "Values:\n" |
|---|
| 246 | " Value1: \tdoes this and that, bla bla bla bla |
|---|
| 247 | bla bla bla bla bla bla bla bla bla bla bla\n" |
|---|
| 248 | " Value2: \tdoes something else, bla bla bla bla |
|---|
| 249 | bla bla bla bla bla bla bla bla bla bla bla\n\n" |
|---|
| 250 | " This paragraph has a first line indent only, |
|---|
| 251 | bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla"); |
|---|
| 252 | </pre> |
|---|
| 253 | <p> |
|---|
| 254 | will produce: |
|---|
| 255 | </p> |
|---|
| 256 | <pre class="screen"> |
|---|
| 257 | --well_formated As you can see this is a |
|---|
| 258 | very well formatted |
|---|
| 259 | option description. |
|---|
| 260 | You can do this for |
|---|
| 261 | example: |
|---|
| 262 | |
|---|
| 263 | Values: |
|---|
| 264 | Value1: does this and |
|---|
| 265 | that, bla bla |
|---|
| 266 | bla bla bla bla |
|---|
| 267 | bla bla bla bla |
|---|
| 268 | bla bla bla bla |
|---|
| 269 | bla |
|---|
| 270 | Value2: does something |
|---|
| 271 | else, bla bla |
|---|
| 272 | bla bla bla bla |
|---|
| 273 | bla bla bla bla |
|---|
| 274 | bla bla bla bla |
|---|
| 275 | bla |
|---|
| 276 | |
|---|
| 277 | This paragraph has a |
|---|
| 278 | first line indent only, |
|---|
| 279 | bla bla bla bla bla bla |
|---|
| 280 | bla bla bla bla bla bla |
|---|
| 281 | bla bla bla |
|---|
| 282 | </pre> |
|---|
| 283 | <p> |
|---|
| 284 | The tab character is removed before output. Only one tabulator per |
|---|
| 285 | paragraph is allowed, otherwisee an exception of type |
|---|
| 286 | program_options::error is thrown. Finally, the tabulator is ignored if |
|---|
| 287 | it's is not on the first line of the paragraph or is on the last |
|---|
| 288 | possible position of the first line. |
|---|
| 289 | </p> |
|---|
| 290 | </div> |
|---|
| 291 | </div> |
|---|
| 292 | <div class="section" lang="en"> |
|---|
| 293 | <div class="titlepage"><div><div><h4 class="title"> |
|---|
| 294 | <a name="id1591838"></a>Semantic Information</h4></div></div></div> |
|---|
| 295 | <p>The semantic information is completely provided by the |
|---|
| 296 | <code class="computeroutput"><a href="../boost/program_options/value_semantic.html" title="Class value_semantic">boost::program_options::value_semantic</a></code> class. For |
|---|
| 297 | example: |
|---|
| 298 | </p> |
|---|
| 299 | <pre class="programlisting"> |
|---|
| 300 | options_description desc; |
|---|
| 301 | desc.add_options() |
|---|
| 302 | ("compression", value<int>()->default_value(10), "compression level") |
|---|
| 303 | ("email", value< vector<string> >() |
|---|
| 304 | ->composing()->notifier(&your_function), "email") |
|---|
| 305 | ; |
|---|
| 306 | </pre> |
|---|
| 307 | <p> |
|---|
| 308 | These declarations specify that default value of the first option is 10, |
|---|
| 309 | that the second option can appear several times and all instances should |
|---|
| 310 | be merged, and that after parsing is done, the library will call |
|---|
| 311 | function <code class="computeroutput">&your_function</code>, passing the value of the |
|---|
| 312 | "email" option as argument. |
|---|
| 313 | </p> |
|---|
| 314 | </div> |
|---|
| 315 | <div class="section" lang="en"> |
|---|
| 316 | <div class="titlepage"><div><div><h4 class="title"> |
|---|
| 317 | <a name="id1591885"></a>Positional Options</h4></div></div></div> |
|---|
| 318 | <p>Our definition of option as (name, value) pairs is simple and |
|---|
| 319 | useful, but in one special case of the command line, there's a |
|---|
| 320 | problem. A command line can include a <em class="firstterm">positional option</em>, |
|---|
| 321 | which does not specify any name at all, for example: |
|---|
| 322 | </p> |
|---|
| 323 | <pre class="screen"> |
|---|
| 324 | archiver --compression=9 /etc/passwd |
|---|
| 325 | </pre> |
|---|
| 326 | <p> |
|---|
| 327 | Here, the "/etc/passwd" element does not have any option name. |
|---|
| 328 | </p> |
|---|
| 329 | <p>One solution is to ask the user to extract positional options |
|---|
| 330 | himself and process them as he likes. However, there's a nicer approach |
|---|
| 331 | -- provide a method to automatically assign the names for positional |
|---|
| 332 | options, so that the above command line can be interpreted the same way |
|---|
| 333 | as: |
|---|
| 334 | </p> |
|---|
| 335 | <pre class="screen"> |
|---|
| 336 | archiver --compression=9 --input-file=/etc/passwd |
|---|
| 337 | </pre> |
|---|
| 338 | <p> |
|---|
| 339 | </p> |
|---|
| 340 | <p>The <code class="computeroutput"><a href="../boost/program_options/positional_options_description.html" title="Class positional_options_description">positional_options_description</a></code> class allows the command line |
|---|
| 341 | parser to assign the names. The class specifies how many positional options |
|---|
| 342 | are allowed, and for each allowed option, specifies the name. For example: |
|---|
| 343 | </p> |
|---|
| 344 | <pre class="programlisting"> |
|---|
| 345 | positional_options_description pd; pd.add("input-file", 1); |
|---|
| 346 | </pre> |
|---|
| 347 | <p> specifies that for exactly one, first, positional |
|---|
| 348 | option the name will be "input-file". |
|---|
| 349 | </p> |
|---|
| 350 | <p>It's possible to specify that a number, or even all positional options, be |
|---|
| 351 | given the same name. |
|---|
| 352 | </p> |
|---|
| 353 | <pre class="programlisting"> |
|---|
| 354 | positional_options_description pd; |
|---|
| 355 | pd.add("output-file", 2).add("input-file", -1); |
|---|
| 356 | </pre> |
|---|
| 357 | <p> |
|---|
| 358 | In the above example, the first two positional options will be associated |
|---|
| 359 | with name "output-file", and any others with the name "input-file". |
|---|
| 360 | </p> |
|---|
| 361 | <div class="warning"><table border="0" summary="Warning"> |
|---|
| 362 | <tr> |
|---|
| 363 | <td rowspan="2" align="center" valign="top" width="25"><img alt="[Warning]" src="../images/warning.png"></td> |
|---|
| 364 | <th align="left">Warning</th> |
|---|
| 365 | </tr> |
|---|
| 366 | <tr><td align="left" valign="top"><p>The <code class="computeroutput"><a href="../boost/program_options/positional_options_description.html" title="Class positional_options_description">positional_options_description</a></code> class only specifies translation from |
|---|
| 367 | position to name, and the option name should still be registered with |
|---|
| 368 | an instance of the <code class="computeroutput"><a href="../boost/program_options/options_description.html" title="Class options_description">options_description</a></code> class.</p></td></tr> |
|---|
| 369 | </table></div> |
|---|
| 370 | </div> |
|---|
| 371 | </div> |
|---|
| 372 | <div class="section" lang="en"> |
|---|
| 373 | <div class="titlepage"><div><div><h3 class="title"> |
|---|
| 374 | <a name="id1592003"></a>Parsers Component</h3></div></div></div> |
|---|
| 375 | <p>The parsers component splits input sources into (name, value) pairs. |
|---|
| 376 | Each parser looks for possible options and consults the options |
|---|
| 377 | description component to determine if the option is known and how its value |
|---|
| 378 | is specified. In the simplest case, the name is explicitly specified, |
|---|
| 379 | which allows the library to decide if such option is known. If it is known, the |
|---|
| 380 | <code class="computeroutput"><a href="../boost/program_options/value_semantic.html" title="Class value_semantic">value_semantic</a></code> instance determines how the value is specified. (If |
|---|
| 381 | it is not known, an exception is thrown.) Common |
|---|
| 382 | cases are when the value is explicitly specified by the user, and when |
|---|
| 383 | the value cannot be specified by the user, but the presence of the |
|---|
| 384 | option implies some value (for example, <code class="computeroutput">true</code>). So, the |
|---|
| 385 | parser checks that the value is specified when needed and not specified |
|---|
| 386 | when not needed, and returns new (name, value) pair. |
|---|
| 387 | </p> |
|---|
| 388 | <p> |
|---|
| 389 | To invoke a parser you typically call a function, passing the options |
|---|
| 390 | description and command line or config file or something else. |
|---|
| 391 | The results of parsing are returned as an instance of the <code class="computeroutput">parsed_options</code> |
|---|
| 392 | class. Typically, that object is passed directly to the storage |
|---|
| 393 | component. However, it also can be used directly, or undergo some additional |
|---|
| 394 | processing. |
|---|
| 395 | </p> |
|---|
| 396 | <p> |
|---|
| 397 | There are three exceptions to the above model -- all related to |
|---|
| 398 | traditional usage of the command line. While they require some support |
|---|
| 399 | from the options description component, the additional complexity is |
|---|
| 400 | tolerable. |
|---|
| 401 | </p> |
|---|
| 402 | <div class="itemizedlist"><ul type="disc"> |
|---|
| 403 | <li><p>The name specified on the command line may be |
|---|
| 404 | different from the option name -- it's common to provide a "short option |
|---|
| 405 | name" alias to a longer name. It's also common to allow an abbreviated name |
|---|
| 406 | to be specified on the command line. |
|---|
| 407 | </p></li> |
|---|
| 408 | <li><p>Sometimes it's desirable to specify value as several |
|---|
| 409 | tokens. For example, an option "--email-recipient" may be followed |
|---|
| 410 | by several emails, each as a separate command line token. This |
|---|
| 411 | behaviour is supported, though it can lead to parsing ambiguities |
|---|
| 412 | and is not enabled by default. |
|---|
| 413 | </p></li> |
|---|
| 414 | <li><p>The command line may contain positional options -- elements |
|---|
| 415 | which don't have any name. The command line parser provides a |
|---|
| 416 | mechanism to guess names for such options, as we've seen in the |
|---|
| 417 | tutorial. |
|---|
| 418 | </p></li> |
|---|
| 419 | </ul></div> |
|---|
| 420 | <p> |
|---|
| 421 | </p> |
|---|
| 422 | </div> |
|---|
| 423 | <div class="section" lang="en"> |
|---|
| 424 | <div class="titlepage"><div><div><h3 class="title"> |
|---|
| 425 | <a name="id1592104"></a>Storage Component</h3></div></div></div> |
|---|
| 426 | <p>The storage component is responsible for: |
|---|
| 427 | </p> |
|---|
| 428 | <div class="itemizedlist"><ul type="disc"> |
|---|
| 429 | <li><p>Storing the final values of an option into a special class and in |
|---|
| 430 | regular variables</p></li> |
|---|
| 431 | <li><p>Handling priorities among different sources.</p></li> |
|---|
| 432 | <li><p>Calling user-specified <code class="computeroutput">notify</code> functions with the final |
|---|
| 433 | values of options.</p></li> |
|---|
| 434 | </ul></div> |
|---|
| 435 | <p> |
|---|
| 436 | </p> |
|---|
| 437 | <p>Let's consider an example: |
|---|
| 438 | </p> |
|---|
| 439 | <pre class="programlisting"> |
|---|
| 440 | variables_map vm; |
|---|
| 441 | store(parse_command_line(argc, argv, desc), vm); |
|---|
| 442 | store(parse_config_file("example.cfg", desc), vm); |
|---|
| 443 | notify(vm); |
|---|
| 444 | </pre> |
|---|
| 445 | <p> |
|---|
| 446 | The <code class="computeroutput">variables_map</code> class is used to store the option |
|---|
| 447 | values. The two calls to the <code class="computeroutput">store</code> function add values |
|---|
| 448 | found on the command line and in the config file. Finally the call to |
|---|
| 449 | the <code class="computeroutput">notify</code> function runs the user-specified notify |
|---|
| 450 | functions and stores the values into regular variables, if needed. |
|---|
| 451 | </p> |
|---|
| 452 | <p>The priority is handled in a simple way: the <code class="computeroutput">store</code> |
|---|
| 453 | function will not change the value of an option if it's already |
|---|
| 454 | assigned. In this case, if the command line specifies the value for an |
|---|
| 455 | option, any value in the config file is ignored. |
|---|
| 456 | </p> |
|---|
| 457 | <div class="warning"><table border="0" summary="Warning"> |
|---|
| 458 | <tr> |
|---|
| 459 | <td rowspan="2" align="center" valign="top" width="25"><img alt="[Warning]" src="../images/warning.png"></td> |
|---|
| 460 | <th align="left">Warning</th> |
|---|
| 461 | </tr> |
|---|
| 462 | <tr><td align="left" valign="top"><p>Don't forget to call the <code class="computeroutput">notify</code> function after you've |
|---|
| 463 | stored all parsed values.</p></td></tr> |
|---|
| 464 | </table></div> |
|---|
| 465 | </div> |
|---|
| 466 | <div class="section" lang="en"> |
|---|
| 467 | <div class="titlepage"><div><div><h3 class="title"> |
|---|
| 468 | <a name="id1592208"></a>Specific parsers</h3></div></div></div> |
|---|
| 469 | <div class="toc"><dl><dt><span class="section"><a href="overview.html#id1592214">Environment variables</a></span></dt></dl></div> |
|---|
| 470 | <div class="section" lang="en"> |
|---|
| 471 | <div class="titlepage"><div><div><h4 class="title"> |
|---|
| 472 | <a name="id1592214"></a>Environment variables</h4></div></div></div> |
|---|
| 473 | <p><em class="firstterm">Environment variables</em> are string variables |
|---|
| 474 | which are available to all programs via the <code class="computeroutput">getenv</code> function |
|---|
| 475 | of C runtime library. The operating system allows to set initial values |
|---|
| 476 | for a given user, and the values can be further changed on the command |
|---|
| 477 | line. For example, on Windows one can use the |
|---|
| 478 | <code class="filename">autoexec.bat</code> file or (on recent versions) the |
|---|
| 479 | <code class="filename">Control Panel/System/Advanced/Environment Variables</code> |
|---|
| 480 | dialog, and on Unix —, the <code class="filename">/etc/profile</code>, |
|---|
| 481 | <code class="filename">~/.profile</code> and <code class="filename">~/.bash_profile</code> |
|---|
| 482 | files. Because environment variables can be set for the entire system, |
|---|
| 483 | they are particularly suitable for options which apply to all programs. |
|---|
| 484 | </p> |
|---|
| 485 | <p>The environment variables can be parsed with the |
|---|
| 486 | <code class="computeroutput"><a href="../id1004920-bb.html" title="Function parse_environment">parse_environment</a></code> function. The function have several overloaded |
|---|
| 487 | versions. The first parameter is always an <code class="computeroutput"><a href="../boost/program_options/options_description.html" title="Class options_description">options_description</a></code> |
|---|
| 488 | instance, and the second specifies what variables must be processed, and |
|---|
| 489 | what option names must correspond to it. To describe the second |
|---|
| 490 | parameter we need to consider naming conventions for environment |
|---|
| 491 | variables.</p> |
|---|
| 492 | <p>If you have an option that should be specified via environment |
|---|
| 493 | variable, you need make up the variable's name. To avoid name clashes, |
|---|
| 494 | we suggest that you use a sufficiently unique prefix for environment |
|---|
| 495 | variables. Also, while option names are most likely in lower case, |
|---|
| 496 | environment variables conventionally use upper case. So, for an option |
|---|
| 497 | name <code class="literal">proxy</code> the environment variable might be called |
|---|
| 498 | <code class="envar">BOOST_PROXY</code>. During parsing, we need to perform reverse |
|---|
| 499 | conversion of the names. This is accomplished by passing the choosen |
|---|
| 500 | prefix as the second parameter of the <code class="computeroutput"><a href="../id1004920-bb.html" title="Function parse_environment">parse_environment</a></code> function. |
|---|
| 501 | Say, if you pass <code class="literal">BOOST_</code> as the prefix, and there are |
|---|
| 502 | two variables, <code class="envar">CVSROOT</code> and <code class="envar">BOOST_PROXY</code>, the |
|---|
| 503 | first variable will be ignored, and the second one will be converted to |
|---|
| 504 | option <code class="literal">proxy</code>. |
|---|
| 505 | </p> |
|---|
| 506 | <p>The above logic is sufficient in many cases, but it is also |
|---|
| 507 | possible to pass, as the second parameter of the <code class="computeroutput"><a href="../id1004920-bb.html" title="Function parse_environment">parse_environment</a></code> |
|---|
| 508 | function, any function taking a <code class="computeroutput">std::string</code> and returning |
|---|
| 509 | <code class="computeroutput">std::string</code>. That function will be called for each |
|---|
| 510 | environment variable and should return either the name of the option, or |
|---|
| 511 | empty string if the variable should be ignored. |
|---|
| 512 | </p> |
|---|
| 513 | </div> |
|---|
| 514 | </div> |
|---|
| 515 | <div class="section" lang="en"> |
|---|
| 516 | <div class="titlepage"><div><div><h3 class="title"> |
|---|
| 517 | <a name="id1592393"></a>Annotated List of Symbols</h3></div></div></div> |
|---|
| 518 | <p>The following table describes all the important symbols in the |
|---|
| 519 | library, for quick access.</p> |
|---|
| 520 | <div class="informaltable"><table class="table" width="100%"> |
|---|
| 521 | <colgroup> |
|---|
| 522 | <col> |
|---|
| 523 | <col> |
|---|
| 524 | </colgroup> |
|---|
| 525 | <thead><tr> |
|---|
| 526 | <th>Symbol</th> |
|---|
| 527 | <th>Description</th> |
|---|
| 528 | </tr></thead> |
|---|
| 529 | <tbody> |
|---|
| 530 | <tr><td colspan="2">Options description component</td></tr> |
|---|
| 531 | <tr> |
|---|
| 532 | <td><code class="computeroutput"><a href="../boost/program_options/options_description.html" title="Class options_description">options_description</a></code></td> |
|---|
| 533 | <td>describes a number of options</td> |
|---|
| 534 | </tr> |
|---|
| 535 | <tr> |
|---|
| 536 | <td><code class="computeroutput"><a href="../boost/program_options/value.html" title="Function value">value</a></code></td> |
|---|
| 537 | <td>defines the option's value</td> |
|---|
| 538 | </tr> |
|---|
| 539 | <tr><td colspan="2">Parsers component</td></tr> |
|---|
| 540 | <tr> |
|---|
| 541 | <td><code class="computeroutput"><a href="../boost/program_options/parse_command_line.html" title="Function template parse_command_line">parse_command_line</a></code></td> |
|---|
| 542 | <td>parses command line (simpified interface)</td> |
|---|
| 543 | </tr> |
|---|
| 544 | <tr> |
|---|
| 545 | <td><code class="computeroutput"><a href="../boost/program_options/basic_command_line_parser.html" title="Class template basic_command_line_parser">basic_command_line_parser</a></code></td> |
|---|
| 546 | <td>parses command line (extended interface)</td> |
|---|
| 547 | </tr> |
|---|
| 548 | <tr> |
|---|
| 549 | <td><code class="computeroutput"><a href="../boost/program_options/parse_config_file.html" title="Function template parse_config_file">parse_config_file</a></code></td> |
|---|
| 550 | <td>parses config file</td> |
|---|
| 551 | </tr> |
|---|
| 552 | <tr> |
|---|
| 553 | <td><code class="computeroutput"><a href="../id1004920-bb.html" title="Function parse_environment">parse_environment</a></code></td> |
|---|
| 554 | <td>parses environment</td> |
|---|
| 555 | </tr> |
|---|
| 556 | <tr><td colspan="2">Storage component</td></tr> |
|---|
| 557 | <tr> |
|---|
| 558 | <td><code class="computeroutput"><a href="../boost/program_options/variables_map.html" title="Class variables_map">variables_map</a></code></td> |
|---|
| 559 | <td>storage for option values</td> |
|---|
| 560 | </tr> |
|---|
| 561 | </tbody> |
|---|
| 562 | </table></div> |
|---|
| 563 | </div> |
|---|
| 564 | </div> |
|---|
| 565 | <table width="100%"><tr> |
|---|
| 566 | <td align="left"></td> |
|---|
| 567 | <td align="right"><small>Copyright © 2002-2004 Vladimir Prus</small></td> |
|---|
| 568 | </tr></table> |
|---|
| 569 | <hr> |
|---|
| 570 | <div class="spirit-nav"> |
|---|
| 571 | <a accesskey="p" href="tutorial.html"><img src="../images/prev.png" alt="Prev"></a><a accesskey="u" href="../program_options.html"><img src="../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../images/home.png" alt="Home"></a><a accesskey="n" href="howto.html"><img src="../images/next.png" alt="Next"></a> |
|---|
| 572 | </div> |
|---|
| 573 | </body> |
|---|
| 574 | </html> |
|---|