Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/doc/html/program_options/overview.html @ 12

Last change on this file since 12 was 12, checked in by landauf, 18 years ago

added boost

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