blob: 531c0f899c954d29391a2d42be97f5edb97cb3ff [file] [log] [blame]
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
2 "http://www.w3.org/TR/html4/strict.dtd">
3<html>
4<head>
Misha Brukmanc86e8b12003-10-24 19:59:21 +00005 <title>CommandLine 2.0 Library Manual</title>
Misha Brukman7120c832003-11-07 18:11:14 +00006 <link rel="stylesheet" href="llvm.css" type="text/css">
Misha Brukmanc86e8b12003-10-24 19:59:21 +00007</head>
8<body>
Chris Lattnerba025252001-07-23 23:03:12 +00009
Misha Brukmanc86e8b12003-10-24 19:59:21 +000010<div class="doc_title">
11 CommandLine 2.0 Library Manual
12</div>
Chris Lattnerba025252001-07-23 23:03:12 +000013
14<ol>
Misha Brukmanc86e8b12003-10-24 19:59:21 +000015 <li><a href="#introduction">Introduction</a></li>
16
Chris Lattnerba025252001-07-23 23:03:12 +000017 <li><a href="#quickstart">Quick Start Guide</a>
18 <ol>
Misha Brukmanc86e8b12003-10-24 19:59:21 +000019 <li><a href="#bool">Boolean Arguments</a></li>
20 <li><a href="#alias">Argument Aliases</a></li>
Chris Lattner50761292002-08-06 19:36:06 +000021 <li><a href="#onealternative">Selecting an alternative from a
Misha Brukmanc86e8b12003-10-24 19:59:21 +000022 set of possibilities</a></li>
23 <li><a href="#namedalternatives">Named alternatives</a></li>
24 <li><a href="#list">Parsing a list of options</a></li>
25 <li><a href="#description">Adding freeform text to help output</a></li>
26 </ol></li>
27
Chris Lattnerba025252001-07-23 23:03:12 +000028 <li><a href="#referenceguide">Reference Guide</a>
Chris Lattner2e19f3e2002-07-25 19:27:01 +000029 <ol>
Chris Lattner50761292002-08-06 19:36:06 +000030 <li><a href="#positional">Positional Arguments</a>
Chris Lattner2e19f3e2002-07-25 19:27:01 +000031 <ul>
Misha Brukmanc86e8b12003-10-24 19:59:21 +000032 <li><a href="#--">Specifying positional options with hyphens</a></li>
Reid Spencerc0f1b212004-08-13 20:19:14 +000033 <li><a href="#getPosition">Determining absolute position with
34 getPosition</a></li>
Chris Lattner50761292002-08-06 19:36:06 +000035 <li><a href="#cl::ConsumeAfter">The <tt>cl::ConsumeAfter</tt>
Misha Brukmanc86e8b12003-10-24 19:59:21 +000036 modifier</a></li>
37 </ul></li>
38
39 <li><a href="#storage">Internal vs External Storage</a></li>
40
41 <li><a href="#attributes">Option Attributes</a></li>
42
Chris Lattner50761292002-08-06 19:36:06 +000043 <li><a href="#modifiers">Option Modifiers</a>
Chris Lattner2e19f3e2002-07-25 19:27:01 +000044 <ul>
Misha Brukmanc86e8b12003-10-24 19:59:21 +000045 <li><a href="#hiding">Hiding an option from <tt>--help</tt>
46 output</a></li>
47 <li><a href="#numoccurrences">Controlling the number of occurrences
48 required and allowed</a></li>
Chris Lattner50761292002-08-06 19:36:06 +000049 <li><a href="#valrequired">Controlling whether or not a value must be
Misha Brukmanc86e8b12003-10-24 19:59:21 +000050 specified</a></li>
51 <li><a href="#formatting">Controlling other formatting options</a></li>
52 <li><a href="#misc">Miscellaneous option modifiers</a></li>
53 </ul></li>
54
Chris Lattner74ea5a62002-08-07 18:27:04 +000055 <li><a href="#toplevel">Top-Level Classes and Functions</a>
Chris Lattner50761292002-08-06 19:36:06 +000056 <ul>
Misha Brukmanc86e8b12003-10-24 19:59:21 +000057 <li><a href="#cl::ParseCommandLineOptions">The
58 <tt>cl::ParseCommandLineOptions</tt> function</a></li>
59 <li><a href="#cl::ParseEnvironmentOptions">The
60 <tt>cl::ParseEnvironmentOptions</tt> function</a></li>
61 <li><a href="#cl::opt">The <tt>cl::opt</tt> class</a></li>
62 <li><a href="#cl::list">The <tt>cl::list</tt> class</a></li>
63 <li><a href="#cl::alias">The <tt>cl::alias</tt> class</a></li>
64 </ul></li>
65
Chris Lattner50761292002-08-06 19:36:06 +000066 <li><a href="#builtinparsers">Builtin parsers</a>
67 <ul>
68 <li><a href="#genericparser">The Generic <tt>parser&lt;t&gt;</tt>
Misha Brukmanc86e8b12003-10-24 19:59:21 +000069 parser</a></li>
Chris Lattner50761292002-08-06 19:36:06 +000070 <li><a href="#boolparser">The <tt>parser&lt;bool&gt;</tt>
Misha Brukmanc86e8b12003-10-24 19:59:21 +000071 specialization</a></li>
Chris Lattner50761292002-08-06 19:36:06 +000072 <li><a href="#stringparser">The <tt>parser&lt;string&gt;</tt>
Misha Brukmanc86e8b12003-10-24 19:59:21 +000073 specialization</a></li>
Chris Lattner50761292002-08-06 19:36:06 +000074 <li><a href="#intparser">The <tt>parser&lt;int&gt;</tt>
Misha Brukmanc86e8b12003-10-24 19:59:21 +000075 specialization</a></li>
Chris Lattner50761292002-08-06 19:36:06 +000076 <li><a href="#doubleparser">The <tt>parser&lt;double&gt;</tt> and
Misha Brukmanc86e8b12003-10-24 19:59:21 +000077 <tt>parser&lt;float&gt;</tt> specializations</a></li>
78 </ul></li>
79 </ol></li>
Chris Lattnerba025252001-07-23 23:03:12 +000080 <li><a href="#extensionguide">Extension Guide</a>
Chris Lattner2e19f3e2002-07-25 19:27:01 +000081 <ol>
Misha Brukmanc86e8b12003-10-24 19:59:21 +000082 <li><a href="#customparser">Writing a custom parser</a></li>
83 <li><a href="#explotingexternal">Exploiting external storage</a></li>
84 <li><a href="#dynamicopts">Dynamically adding command line
85 options</a></li>
86 </ol></li>
87</ol>
Chris Lattner50761292002-08-06 19:36:06 +000088
Chris Lattner020e1fc2004-05-23 21:07:27 +000089<div class="doc_author">
90 <p>Written by <a href="mailto:sabre@nondot.org">Chris Lattner</a></p>
Misha Brukmanc86e8b12003-10-24 19:59:21 +000091</div>
Chris Lattnerba025252001-07-23 23:03:12 +000092
93<!-- *********************************************************************** -->
Misha Brukmanc86e8b12003-10-24 19:59:21 +000094<div class="doc_section">
Misha Brukman403ff612003-11-07 19:42:44 +000095 <a name="introduction">Introduction</a>
Misha Brukmanc86e8b12003-10-24 19:59:21 +000096</div>
Chris Lattnerba025252001-07-23 23:03:12 +000097<!-- *********************************************************************** -->
98
Misha Brukmanc86e8b12003-10-24 19:59:21 +000099<div class="doc_text">
100
101<p>This document describes the CommandLine argument processing library. It will
Chris Lattner50761292002-08-06 19:36:06 +0000102show you how to use it, and what it can do. The CommandLine library uses a
103declarative approach to specifying the command line options that your program
104takes. By default, these options declarations implicitly hold the value parsed
105for the option declared (of course this <a href="#storage">can be
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000106changed</a>).</p>
Chris Lattnerba025252001-07-23 23:03:12 +0000107
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000108<p>Although there are a <b>lot</b> of command line argument parsing libraries
109out there in many different languages, none of them fit well with what I needed.
110By looking at the features and problems of other libraries, I designed the
111CommandLine library to have the following features:</p>
Chris Lattnerba025252001-07-23 23:03:12 +0000112
113<ol>
Chris Lattner2e19f3e2002-07-25 19:27:01 +0000114<li>Speed: The CommandLine library is very quick and uses little resources. The
115parsing time of the library is directly proportional to the number of arguments
116parsed, not the the number of options recognized. Additionally, command line
Chris Lattner364e6312003-06-21 21:45:56 +0000117argument values are captured transparently into user defined global variables,
118which can be accessed like any other variable (and with the same
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000119performance).</li>
Chris Lattnerba025252001-07-23 23:03:12 +0000120
Chris Lattner2e19f3e2002-07-25 19:27:01 +0000121<li>Type Safe: As a user of CommandLine, you don't have to worry about
122remembering the type of arguments that you want (is it an int? a string? a
123bool? an enum?) and keep casting it around. Not only does this help prevent
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000124error prone constructs, it also leads to dramatically cleaner source code.</li>
Chris Lattnerba025252001-07-23 23:03:12 +0000125
Chris Lattner2e19f3e2002-07-25 19:27:01 +0000126<li>No subclasses required: To use CommandLine, you instantiate variables that
127correspond to the arguments that you would like to capture, you don't subclass a
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000128parser. This means that you don't have to write <b>any</b> boilerplate
129code.</li>
Chris Lattnerba025252001-07-23 23:03:12 +0000130
Chris Lattner2e19f3e2002-07-25 19:27:01 +0000131<li>Globally accessible: Libraries can specify command line arguments that are
132automatically enabled in any tool that links to the library. This is possible
133because the application doesn't have to keep a "list" of arguments to pass to
Chris Lattner50761292002-08-06 19:36:06 +0000134the parser. This also makes supporting <a href="#dynamicopts">dynamically
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000135loaded options</a> trivial.</li>
Chris Lattnerba025252001-07-23 23:03:12 +0000136
Chris Lattner74ea5a62002-08-07 18:27:04 +0000137<li>Cleaner: CommandLine supports enum and other types directly, meaning that
Chris Lattner50761292002-08-06 19:36:06 +0000138there is less error and more security built into the library. You don't have to
139worry about whether your integral command line argument accidentally got
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000140assigned a value that is not valid for your enum type.</li>
Chris Lattnerba025252001-07-23 23:03:12 +0000141
Chris Lattner2e19f3e2002-07-25 19:27:01 +0000142<li>Powerful: The CommandLine library supports many different types of
Chris Lattner50761292002-08-06 19:36:06 +0000143arguments, from simple <a href="#boolparser">boolean flags</a> to <a
144href="#cl::opt">scalars arguments</a> (<a href="#stringparser">strings</a>, <a
145href="#intparser">integers</a>, <a href="#genericparser">enums</a>, <a
146href="#doubleparser">doubles</a>), to <a href="#cl::list">lists of
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000147arguments</a>. This is possible because CommandLine is...</li>
Chris Lattnerba025252001-07-23 23:03:12 +0000148
Chris Lattner2e19f3e2002-07-25 19:27:01 +0000149<li>Extensible: It is very simple to add a new argument type to CommandLine.
150Simply specify the parser that you want to use with the command line option when
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000151you declare it. <a href="#customparser">Custom parsers</a> are no problem.</li>
Chris Lattnerba025252001-07-23 23:03:12 +0000152
Chris Lattner2e19f3e2002-07-25 19:27:01 +0000153<li>Labor Saving: The CommandLine library cuts down on the amount of grunt work
Chris Lattner50761292002-08-06 19:36:06 +0000154that you, the user, have to do. For example, it automatically provides a
155<tt>--help</tt> option that shows the available command line options for your
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000156tool. Additionally, it does most of the basic correctness checking for
157you.</li>
Chris Lattner50761292002-08-06 19:36:06 +0000158
159<li>Capable: The CommandLine library can handle lots of different forms of
160options often found in real programs. For example, <a
161href="#positional">positional</a> arguments, <tt>ls</tt> style <a
162href="#cl::Grouping">grouping</a> options (to allow processing '<tt>ls
163-lad</tt>' naturally), <tt>ld</tt> style <a href="#cl::Prefix">prefix</a>
164options (to parse '<tt>-lmalloc -L/usr/lib</tt>'), and <a
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000165href="#cl::ConsumeAfter">interpreter style options</a>.</li>
Chris Lattner50761292002-08-06 19:36:06 +0000166
Chris Lattnerba025252001-07-23 23:03:12 +0000167</ol>
168
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000169<p>This document will hopefully let you jump in and start using CommandLine in
170your utility quickly and painlessly. Additionally it should be a simple
171reference manual to figure out how stuff works. If it is failing in some area
172(or you want an extension to the library), nag the author, <a
173href="mailto:sabre@nondot.org">Chris Lattner</a>.</p>
Chris Lattner50761292002-08-06 19:36:06 +0000174
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000175</div>
Chris Lattnerba025252001-07-23 23:03:12 +0000176
177<!-- *********************************************************************** -->
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000178<div class="doc_section">
179 <a name="quickstart">Quick Start Guide</a>
180</div>
Chris Lattnerba025252001-07-23 23:03:12 +0000181<!-- *********************************************************************** -->
182
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000183<div class="doc_text">
184
185<p>This section of the manual runs through a simple CommandLine'ification of a
Chris Lattner2e19f3e2002-07-25 19:27:01 +0000186basic compiler tool. This is intended to show you how to jump into using the
187CommandLine library in your own program, and show you some of the cool things it
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000188can do.</p>
Chris Lattnerba025252001-07-23 23:03:12 +0000189
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000190<p>To start out, you need to include the CommandLine header file into your
191program:</p>
Chris Lattnerba025252001-07-23 23:03:12 +0000192
193<pre>
Chris Lattner2e19f3e2002-07-25 19:27:01 +0000194 #include "Support/CommandLine.h"
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000195</pre>
Chris Lattnerba025252001-07-23 23:03:12 +0000196
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000197<p>Additionally, you need to add this as the first line of your main
198program:</p>
Chris Lattnerba025252001-07-23 23:03:12 +0000199
200<pre>
201int main(int argc, char **argv) {
Chris Lattner74ea5a62002-08-07 18:27:04 +0000202 <a href="#cl::ParseCommandLineOptions">cl::ParseCommandLineOptions</a>(argc, argv);
Chris Lattnerba025252001-07-23 23:03:12 +0000203 ...
204}
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000205</pre>
Chris Lattnerba025252001-07-23 23:03:12 +0000206
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000207<p>... which actually parses the arguments and fills in the variable
208declarations.</p>
Chris Lattnerba025252001-07-23 23:03:12 +0000209
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000210<p>Now that you are ready to support command line arguments, we need to tell the
Chris Lattner2e19f3e2002-07-25 19:27:01 +0000211system which ones we want, and what type of argument they are. The CommandLine
Chris Lattner364e6312003-06-21 21:45:56 +0000212library uses a declarative syntax to model command line arguments with the
213global variable declarations that capture the parsed values. This means that
214for every command line option that you would like to support, there should be a
215global variable declaration to capture the result. For example, in a compiler,
216we would like to support the unix standard '<tt>-o &lt;filename&gt;</tt>' option
217to specify where to put the output. With the CommandLine library, this is
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000218represented like this:</p>
Chris Lattnerba025252001-07-23 23:03:12 +0000219
Misha Brukman374258e2004-05-12 18:42:35 +0000220<a name="value_desc_example"></a>
221<pre>
222<a href="#cl::opt">cl::opt</a>&lt;string&gt; OutputFilename("<i>o</i>", <a href="#cl::desc">cl::desc</a>("<i>Specify output filename</i>"), <a href="#cl::value_desc">cl::value_desc</a>("<i>filename</i>"));
223</pre>
Chris Lattnerba025252001-07-23 23:03:12 +0000224
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000225<p>This declares a global variable "<tt>OutputFilename</tt>" that is used to
Chris Lattner364e6312003-06-21 21:45:56 +0000226capture the result of the "<tt>o</tt>" argument (first parameter). We specify
227that this is a simple scalar option by using the "<tt><a
228href="#cl::opt">cl::opt</a></tt>" template (as opposed to the <a
229href="#list">"<tt>cl::list</tt> template</a>), and tell the CommandLine library
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000230that the data type that we are parsing is a string.</p>
Chris Lattner2e19f3e2002-07-25 19:27:01 +0000231
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000232<p>The second and third parameters (which are optional) are used to specify what
233to output for the "<tt>--help</tt>" option. In this case, we get a line that
234looks like this:</p>
Chris Lattnerba025252001-07-23 23:03:12 +0000235
236<pre>
Chris Lattner2e19f3e2002-07-25 19:27:01 +0000237USAGE: compiler [options]
Chris Lattnerba025252001-07-23 23:03:12 +0000238
Chris Lattner2e19f3e2002-07-25 19:27:01 +0000239OPTIONS:
240 -help - display available options (--help-hidden for more)
Chris Lattner74ea5a62002-08-07 18:27:04 +0000241 <b>-o &lt;filename&gt; - Specify output filename</b>
Chris Lattner2e19f3e2002-07-25 19:27:01 +0000242</pre>
243
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000244<p>Because we specified that the command line option should parse using the
Chris Lattner2e19f3e2002-07-25 19:27:01 +0000245<tt>string</tt> data type, the variable declared is automatically usable as a
246real string in all contexts that a normal C++ string object may be used. For
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000247example:</p>
Chris Lattnerba025252001-07-23 23:03:12 +0000248
249<pre>
250 ...
251 ofstream Output(OutputFilename.c_str());
252 if (Out.good()) ...
253 ...
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000254</pre>
Chris Lattnerba025252001-07-23 23:03:12 +0000255
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000256<p>There are many different options that you can use to customize the command
257line option handling library, but the above example shows the general interface
258to these options. The options can be specified in any order, and are specified
Chris Lattner50761292002-08-06 19:36:06 +0000259with helper functions like <a href="#cl::desc"><tt>cl::desc(...)</tt></a>, so
Chris Lattner74ea5a62002-08-07 18:27:04 +0000260there are no positional dependencies to remember. The available options are
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000261discussed in detail in the <a href="#referenceguide">Reference Guide</a>.</p>
Chris Lattner50761292002-08-06 19:36:06 +0000262
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000263<p>Continuing the example, we would like to have our compiler take an input
Chris Lattner2e19f3e2002-07-25 19:27:01 +0000264filename as well as an output filename, but we do not want the input filename to
265be specified with a hyphen (ie, not <tt>-filename.c</tt>). To support this
Chris Lattner50761292002-08-06 19:36:06 +0000266style of argument, the CommandLine library allows for <a
267href="#positional">positional</a> arguments to be specified for the program.
268These positional arguments are filled with command line parameters that are not
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000269in option form. We use this feature like this:</p>
Chris Lattnerba025252001-07-23 23:03:12 +0000270
271<pre>
Chris Lattner50761292002-08-06 19:36:06 +0000272<a href="#cl::opt">cl::opt</a>&lt;string&gt; InputFilename(<a href="#cl::Positional">cl::Positional</a>, <a href="#cl::desc">cl::desc</a>("<i>&lt;input file&gt;</i>"), <a href="#cl::init">cl::init</a>("<i>-</i>"));
Chris Lattnerba025252001-07-23 23:03:12 +0000273</pre>
274
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000275<p>This declaration indicates that the first positional argument should be
276treated as the input filename. Here we use the <tt><a
Chris Lattner50761292002-08-06 19:36:06 +0000277href="#cl::init">cl::init</a></tt> option to specify an initial value for the
278command line option, which is used if the option is not specified (if you do not
279specify a <tt><a href="#cl::init">cl::init</a></tt> modifier for an option, then
280the default constructor for the data type is used to initialize the value).
Chris Lattner2e19f3e2002-07-25 19:27:01 +0000281Command line options default to being optional, so if we would like to require
Chris Lattner50761292002-08-06 19:36:06 +0000282that the user always specify an input filename, we would add the <tt><a
283href="#cl::Required">cl::Required</a></tt> flag, and we could eliminate the
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000284<tt><a href="#cl::init">cl::init</a></tt> modifier, like this:</p>
Chris Lattnerba025252001-07-23 23:03:12 +0000285
286<pre>
Chris Lattner50761292002-08-06 19:36:06 +0000287<a href="#cl::opt">cl::opt</a>&lt;string&gt; InputFilename(<a href="#cl::Positional">cl::Positional</a>, <a href="#cl::desc">cl::desc</a>("<i>&lt;input file&gt;</i>"), <b><a href="#cl::Required">cl::Required</a></b>);
Chris Lattnerba025252001-07-23 23:03:12 +0000288</pre>
289
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000290<p>Again, the CommandLine library does not require the options to be specified
291in any particular order, so the above declaration is equivalent to:</p>
Chris Lattner2e19f3e2002-07-25 19:27:01 +0000292
293<pre>
Chris Lattner50761292002-08-06 19:36:06 +0000294<a href="#cl::opt">cl::opt</a>&lt;string&gt; InputFilename(<a href="#cl::Positional">cl::Positional</a>, <a href="#cl::Required">cl::Required</a>, <a href="#cl::desc">cl::desc</a>("<i>&lt;input file&gt;</i>"));
Chris Lattner2e19f3e2002-07-25 19:27:01 +0000295</pre>
296
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000297<p>By simply adding the <tt><a href="#cl::Required">cl::Required</a></tt> flag,
298the CommandLine library will automatically issue an error if the argument is not
Chris Lattner50761292002-08-06 19:36:06 +0000299specified, which shifts all of the command line option verification code out of
300your application into the library. This is just one example of how using flags
301can alter the default behaviour of the library, on a per-option basis. By
302adding one of the declarations above, the <tt>--help</tt> option synopsis is now
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000303extended to:</p>
Chris Lattner2e19f3e2002-07-25 19:27:01 +0000304
305<pre>
Chris Lattner74ea5a62002-08-07 18:27:04 +0000306USAGE: compiler [options] <b>&lt;input file&gt;</b>
Chris Lattner2e19f3e2002-07-25 19:27:01 +0000307
308OPTIONS:
309 -help - display available options (--help-hidden for more)
310 -o &lt;filename&gt; - Specify output filename
311</pre>
312
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000313<p>... indicating that an input filename is expected.</p>
Chris Lattner2e19f3e2002-07-25 19:27:01 +0000314
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000315</div>
Chris Lattner74ea5a62002-08-07 18:27:04 +0000316
Chris Lattnerba025252001-07-23 23:03:12 +0000317<!-- ======================================================================= -->
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000318<div class="doc_subsection">
319 <a name="bool">Boolean Arguments</a>
320</div>
Chris Lattnerba025252001-07-23 23:03:12 +0000321
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000322<div class="doc_text">
323
324<p>In addition to input and output filenames, we would like the compiler example
325to support three boolean flags: "<tt>-f</tt>" to force overwriting of the output
Chris Lattner2e19f3e2002-07-25 19:27:01 +0000326file, "<tt>--quiet</tt>" to enable quiet mode, and "<tt>-q</tt>" for backwards
327compatibility with some of our users. We can support these by declaring options
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000328of boolean type like this:</p>
Chris Lattnerba025252001-07-23 23:03:12 +0000329
330<pre>
Chris Lattner50761292002-08-06 19:36:06 +0000331<a href="#cl::opt">cl::opt</a>&lt;bool&gt; Force ("<i>f</i>", <a href="#cl::desc">cl::desc</a>("<i>Overwrite output files</i>"));
332<a href="#cl::opt">cl::opt</a>&lt;bool&gt; Quiet ("<i>quiet</i>", <a href="#cl::desc">cl::desc</a>("<i>Don't print informational messages</i>"));
333<a href="#cl::opt">cl::opt</a>&lt;bool&gt; Quiet2("<i>q</i>", <a href="#cl::desc">cl::desc</a>("<i>Don't print informational messages</i>"), <a href="#cl::Hidden">cl::Hidden</a>);
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000334</pre>
Chris Lattnerba025252001-07-23 23:03:12 +0000335
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000336<p>This does what you would expect: it declares three boolean variables
Chris Lattner2e19f3e2002-07-25 19:27:01 +0000337("<tt>Force</tt>", "<tt>Quiet</tt>", and "<tt>Quiet2</tt>") to recognize these
Chris Lattner50761292002-08-06 19:36:06 +0000338options. Note that the "<tt>-q</tt>" option is specified with the "<a
339href="#cl::Hidden"><tt>cl::Hidden</tt></a>" flag. This modifier prevents it
340from being shown by the standard "<tt>--help</tt>" output (note that it is still
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000341shown in the "<tt>--help-hidden</tt>" output).</p>
Chris Lattner2e19f3e2002-07-25 19:27:01 +0000342
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000343<p>The CommandLine library uses a <a href="#builtinparsers">different parser</a>
Chris Lattner74ea5a62002-08-07 18:27:04 +0000344for different data types. For example, in the string case, the argument passed
345to the option is copied literally into the content of the string variable... we
346obviously cannot do that in the boolean case, however, so we must use a smarter
347parser. In the case of the boolean parser, it allows no options (in which case
348it assigns the value of true to the variable), or it allows the values
349"<tt>true</tt>" or "<tt>false</tt>" to be specified, allowing any of the
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000350following inputs:</p>
Chris Lattnerba025252001-07-23 23:03:12 +0000351
352<pre>
Chris Lattner2e19f3e2002-07-25 19:27:01 +0000353 compiler -f # No value, 'Force' == true
354 compiler -f=true # Value specified, 'Force' == true
355 compiler -f=TRUE # Value specified, 'Force' == true
356 compiler -f=FALSE # Value specified, 'Force' == false
357</pre>
358
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000359<p>... you get the idea. The <a href="#boolparser">bool parser</a> just turns
360the string values into boolean values, and rejects things like '<tt>compiler
Chris Lattner74ea5a62002-08-07 18:27:04 +0000361-f=foo</tt>'. Similarly, the <a href="#doubleparser">float</a>, <a
362href="#doubleparser">double</a>, and <a href="#intparser">int</a> parsers work
363like you would expect, using the '<tt>strtol</tt>' and '<tt>strtod</tt>' C
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000364library calls to parse the string value into the specified data type.</p>
Chris Lattner2e19f3e2002-07-25 19:27:01 +0000365
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000366<p>With the declarations above, "<tt>compiler --help</tt>" emits this:</p>
Chris Lattner2e19f3e2002-07-25 19:27:01 +0000367
368<pre>
369USAGE: compiler [options] &lt;input file&gt;
Chris Lattnerba025252001-07-23 23:03:12 +0000370
371OPTIONS:
Chris Lattner74ea5a62002-08-07 18:27:04 +0000372 <b>-f - Overwrite output files</b>
Chris Lattnerba025252001-07-23 23:03:12 +0000373 -o - Override output filename
Chris Lattner74ea5a62002-08-07 18:27:04 +0000374 <b>-quiet - Don't print informational messages</b>
Chris Lattnerba025252001-07-23 23:03:12 +0000375 -help - display available options (--help-hidden for more)
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000376</pre>
Chris Lattnerba025252001-07-23 23:03:12 +0000377
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000378<p>and "<tt>opt --help-hidden</tt>" prints this:</p>
Chris Lattnerba025252001-07-23 23:03:12 +0000379
380<pre>
Chris Lattner74ea5a62002-08-07 18:27:04 +0000381USAGE: compiler [options] &lt;input file&gt;
Chris Lattnerba025252001-07-23 23:03:12 +0000382
383OPTIONS:
384 -f - Overwrite output files
385 -o - Override output filename
Chris Lattner74ea5a62002-08-07 18:27:04 +0000386 <b>-q - Don't print informational messages</b>
Chris Lattnerba025252001-07-23 23:03:12 +0000387 -quiet - Don't print informational messages
388 -help - display available options (--help-hidden for more)
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000389</pre>
Chris Lattnerba025252001-07-23 23:03:12 +0000390
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000391<p>This brief example has shown you how to use the '<tt><a
Chris Lattner50761292002-08-06 19:36:06 +0000392href="#cl::opt">cl::opt</a></tt>' class to parse simple scalar command line
393arguments. In addition to simple scalar arguments, the CommandLine library also
394provides primitives to support CommandLine option <a href="#alias">aliases</a>,
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000395and <a href="#list">lists</a> of options.</p>
Chris Lattnerba025252001-07-23 23:03:12 +0000396
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000397</div>
Chris Lattnerba025252001-07-23 23:03:12 +0000398
399<!-- ======================================================================= -->
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000400<div class="doc_subsection">
401 <a name="alias">Argument Aliases</a>
402</div>
Chris Lattnerba025252001-07-23 23:03:12 +0000403
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000404<div class="doc_text">
405
406<p>So far, the example works well, except for the fact that we need to check the
407quiet condition like this now:</p>
Chris Lattnerba025252001-07-23 23:03:12 +0000408
409<pre>
410...
411 if (!Quiet &amp;&amp; !Quiet2) printInformationalMessage(...);
412...
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000413</pre>
Chris Lattnerba025252001-07-23 23:03:12 +0000414
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000415<p>... which is a real pain! Instead of defining two values for the same
Chris Lattner50761292002-08-06 19:36:06 +0000416condition, we can use the "<tt><a href="#cl::alias">cl::alias</a></tt>" class to make the "<tt>-q</tt>"
Chris Lattner2e19f3e2002-07-25 19:27:01 +0000417option an <b>alias</b> for the "<tt>-quiet</tt>" option, instead of providing
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000418a value itself:</p>
Chris Lattnerba025252001-07-23 23:03:12 +0000419
420<pre>
Chris Lattner50761292002-08-06 19:36:06 +0000421<a href="#cl::opt">cl::opt</a>&lt;bool&gt; Force ("<i>f</i>", <a href="#cl::desc">cl::desc</a>("<i>Overwrite output files</i>"));
422<a href="#cl::opt">cl::opt</a>&lt;bool&gt; Quiet ("<i>quiet</i>", <a href="#cl::desc">cl::desc</a>("<i>Don't print informational messages</i>"));
423<a href="#cl::alias">cl::alias</a> QuietA("<i>q</i>", <a href="#cl::desc">cl::desc</a>("<i>Alias for -quiet</i>"), <a href="#cl::aliasopt">cl::aliasopt</a>(Quiet));
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000424</pre>
Chris Lattnerba025252001-07-23 23:03:12 +0000425
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000426<p>The third line (which is the only one we modified from above) defines a
Chris Lattner2e19f3e2002-07-25 19:27:01 +0000427"<tt>-q</tt> alias that updates the "<tt>Quiet</tt>" variable (as specified by
Chris Lattner50761292002-08-06 19:36:06 +0000428the <tt><a href="#cl::aliasopt">cl::aliasopt</a></tt> modifier) whenever it is
429specified. Because aliases do not hold state, the only thing the program has to
430query is the <tt>Quiet</tt> variable now. Another nice feature of aliases is
431that they automatically hide themselves from the <tt>-help</tt> output
432(although, again, they are still visible in the <tt>--help-hidden
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000433output</tt>).</p>
Chris Lattner2e19f3e2002-07-25 19:27:01 +0000434
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000435<p>Now the application code can simply use:</p>
Chris Lattnerba025252001-07-23 23:03:12 +0000436
437<pre>
438...
439 if (!Quiet) printInformationalMessage(...);
440...
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000441</pre>
Chris Lattnerba025252001-07-23 23:03:12 +0000442
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000443<p>... which is much nicer! The "<tt><a href="#cl::alias">cl::alias</a></tt>"
444can be used to specify an alternative name for any variable type, and has many
445uses.</p>
Chris Lattner2e19f3e2002-07-25 19:27:01 +0000446
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000447</div>
Chris Lattnerba025252001-07-23 23:03:12 +0000448
449<!-- ======================================================================= -->
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000450<div class="doc_subsection">
451 <a name="onealternative">Selecting an alternative from a set of
452 possibilities</a>
453</div>
Chris Lattnerba025252001-07-23 23:03:12 +0000454
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000455<div class="doc_text">
456
457<p>So far, we have seen how the CommandLine library handles builtin types like
Chris Lattner2e19f3e2002-07-25 19:27:01 +0000458<tt>std::string</tt>, <tt>bool</tt> and <tt>int</tt>, but how does it handle
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000459things it doesn't know about, like enums or '<tt>int*</tt>'s?</p>
Chris Lattnerba025252001-07-23 23:03:12 +0000460
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000461<p>The answer is that it uses a table driven generic parser (unless you specify
Chris Lattner2e19f3e2002-07-25 19:27:01 +0000462your own parser, as described in the <a href="#extensionguide">Extension
Reid Spencera39bf3d2004-08-10 16:38:18 +0000463Guide</a>). This parser maps literal strings to whatever type is required, and
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000464requires you to tell it what this mapping should be.</p>
Chris Lattner2e19f3e2002-07-25 19:27:01 +0000465
Reid Spencera39bf3d2004-08-10 16:38:18 +0000466<p>Lets say that we would like to add four optimization levels to our
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000467optimizer, using the standard flags "<tt>-g</tt>", "<tt>-O0</tt>",
468"<tt>-O1</tt>", and "<tt>-O2</tt>". We could easily implement this with boolean
469options like above, but there are several problems with this strategy:</p>
Chris Lattnerba025252001-07-23 23:03:12 +0000470
471<ol>
Chris Lattner2e19f3e2002-07-25 19:27:01 +0000472<li>A user could specify more than one of the options at a time, for example,
473"<tt>opt -O3 -O2</tt>". The CommandLine library would not be able to catch this
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000474erroneous input for us.</li>
Chris Lattner2e19f3e2002-07-25 19:27:01 +0000475
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000476<li>We would have to test 4 different variables to see which ones are set.</li>
Chris Lattner2e19f3e2002-07-25 19:27:01 +0000477
478<li>This doesn't map to the numeric levels that we want... so we cannot easily
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000479see if some level &gt;= "<tt>-O1</tt>" is enabled.</li>
Chris Lattner2e19f3e2002-07-25 19:27:01 +0000480
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000481</ol>
Chris Lattnerba025252001-07-23 23:03:12 +0000482
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000483<p>To cope with these problems, we can use an enum value, and have the
484CommandLine library fill it in with the appropriate level directly, which is
485used like this:</p>
Chris Lattnerba025252001-07-23 23:03:12 +0000486
487<pre>
488enum OptLevel {
489 g, O1, O2, O3
490};
491
Chris Lattner50761292002-08-06 19:36:06 +0000492<a href="#cl::opt">cl::opt</a>&lt;OptLevel&gt; OptimizationLevel(<a href="#cl::desc">cl::desc</a>("<i>Choose optimization level:</i>"),
493 <a href="#cl::values">cl::values</a>(
Chris Lattner2e19f3e2002-07-25 19:27:01 +0000494 clEnumVal(g , "<i>No optimizations, enable debugging</i>"),
495 clEnumVal(O1, "<i>Enable trivial optimizations</i>"),
496 clEnumVal(O2, "<i>Enable default optimizations</i>"),
497 clEnumVal(O3, "<i>Enable expensive optimizations</i>"),
Chris Lattner1fbd76e2004-07-16 00:10:54 +0000498 clEnumValEnd));
Chris Lattnerba025252001-07-23 23:03:12 +0000499
500...
Chris Lattner2e19f3e2002-07-25 19:27:01 +0000501 if (OptimizationLevel &gt;= O2) doPartialRedundancyElimination(...);
Chris Lattnerba025252001-07-23 23:03:12 +0000502...
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000503</pre>
Chris Lattnerba025252001-07-23 23:03:12 +0000504
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000505<p>This declaration defines a variable "<tt>OptimizationLevel</tt>" of the
Chris Lattner2e19f3e2002-07-25 19:27:01 +0000506"<tt>OptLevel</tt>" enum type. This variable can be assigned any of the values
507that are listed in the declaration (Note that the declaration list must be
Chris Lattner1fbd76e2004-07-16 00:10:54 +0000508terminated with the "<tt>clEnumValEnd</tt>" argument!). The CommandLine
509library enforces
Chris Lattner2e19f3e2002-07-25 19:27:01 +0000510that the user can only specify one of the options, and it ensure that only valid
511enum values can be specified. The "<tt>clEnumVal</tt>" macros ensure that the
Chris Lattner50761292002-08-06 19:36:06 +0000512command line arguments matched the enum values. With this option added, our
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000513help output now is:</p>
Chris Lattnerba025252001-07-23 23:03:12 +0000514
Chris Lattner2e19f3e2002-07-25 19:27:01 +0000515<pre>
516USAGE: compiler [options] &lt;input file&gt;
Chris Lattnerba025252001-07-23 23:03:12 +0000517
Chris Lattner2e19f3e2002-07-25 19:27:01 +0000518OPTIONS:
Chris Lattner74ea5a62002-08-07 18:27:04 +0000519 <b>Choose optimization level:
Chris Lattner2e19f3e2002-07-25 19:27:01 +0000520 -g - No optimizations, enable debugging
521 -O1 - Enable trivial optimizations
522 -O2 - Enable default optimizations
Chris Lattner74ea5a62002-08-07 18:27:04 +0000523 -O3 - Enable expensive optimizations</b>
Chris Lattner2e19f3e2002-07-25 19:27:01 +0000524 -f - Overwrite output files
525 -help - display available options (--help-hidden for more)
526 -o &lt;filename&gt; - Specify output filename
527 -quiet - Don't print informational messages
528</pre>
Chris Lattnerba025252001-07-23 23:03:12 +0000529
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000530<p>In this case, it is sort of awkward that flag names correspond directly to
531enum names, because we probably don't want a enum definition named "<tt>g</tt>"
532in our program. Because of this, we can alternatively write this example like
533this:</p>
Chris Lattnerba025252001-07-23 23:03:12 +0000534
535<pre>
536enum OptLevel {
537 Debug, O1, O2, O3
538};
539
Chris Lattner50761292002-08-06 19:36:06 +0000540<a href="#cl::opt">cl::opt</a>&lt;OptLevel&gt; OptimizationLevel(<a href="#cl::desc">cl::desc</a>("<i>Choose optimization level:</i>"),
541 <a href="#cl::values">cl::values</a>(
Chris Lattner2e19f3e2002-07-25 19:27:01 +0000542 clEnumValN(Debug, "g", "<i>No optimizations, enable debugging</i>"),
543 clEnumVal(O1 , "<i>Enable trivial optimizations</i>"),
544 clEnumVal(O2 , "<i>Enable default optimizations</i>"),
545 clEnumVal(O3 , "<i>Enable expensive optimizations</i>"),
Chris Lattner1fbd76e2004-07-16 00:10:54 +0000546 clEnumValEnd));
Chris Lattnerba025252001-07-23 23:03:12 +0000547
548...
549 if (OptimizationLevel == Debug) outputDebugInfo(...);
550...
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000551</pre>
Chris Lattnerba025252001-07-23 23:03:12 +0000552
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000553<p>By using the "<tt>clEnumValN</tt>" macro instead of "<tt>clEnumVal</tt>", we
554can directly specify the name that the flag should get. In general a direct
555mapping is nice, but sometimes you can't or don't want to preserve the mapping,
556which is when you would use it.</p>
Chris Lattner2e19f3e2002-07-25 19:27:01 +0000557
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000558</div>
Chris Lattnerba025252001-07-23 23:03:12 +0000559
560<!-- ======================================================================= -->
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000561<div class="doc_subsection">
562 <a name="namedalternatives">Named Alternatives</a>
563</div>
Chris Lattnerba025252001-07-23 23:03:12 +0000564
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000565<div class="doc_text">
566
567<p>Another useful argument form is a named alternative style. We shall use this
Chris Lattner2e19f3e2002-07-25 19:27:01 +0000568style in our compiler to specify different debug levels that can be used.
569Instead of each debug level being its own switch, we want to support the
570following options, of which only one can be specified at a time:
571"<tt>--debug-level=none</tt>", "<tt>--debug-level=quick</tt>",
572"<tt>--debug-level=detailed</tt>". To do this, we use the exact same format as
573our optimization level flags, but we also specify an option name. For this
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000574case, the code looks like this:</p>
Chris Lattnerba025252001-07-23 23:03:12 +0000575
576<pre>
577enum DebugLev {
578 nodebuginfo, quick, detailed
579};
580
581// Enable Debug Options to be specified on the command line
Chris Lattner3b8f3da2003-06-03 04:40:06 +0000582<a href="#cl::opt">cl::opt</a>&lt;DebugLev&gt; DebugLevel("<i>debug_level</i>", <a href="#cl::desc">cl::desc</a>("<i>Set the debugging level:</i>"),
Chris Lattner50761292002-08-06 19:36:06 +0000583 <a href="#cl::values">cl::values</a>(
Chris Lattner2e19f3e2002-07-25 19:27:01 +0000584 clEnumValN(nodebuginfo, "none", "<i>disable debug information</i>"),
585 clEnumVal(quick, "<i>enable quick debug information</i>"),
586 clEnumVal(detailed, "<i>enable detailed debug information</i>"),
Chris Lattner1fbd76e2004-07-16 00:10:54 +0000587 clEnumValEnd));
Chris Lattnerba025252001-07-23 23:03:12 +0000588</pre>
589
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000590<p>This definition defines an enumerated command line variable of type "<tt>enum
Chris Lattner2e19f3e2002-07-25 19:27:01 +0000591DebugLev</tt>", which works exactly the same way as before. The difference here
592is just the interface exposed to the user of your program and the help output by
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000593the "<tt>--help</tt>" option:</p>
Chris Lattnerba025252001-07-23 23:03:12 +0000594
595<pre>
Chris Lattner2e19f3e2002-07-25 19:27:01 +0000596USAGE: compiler [options] &lt;input file&gt;
597
Chris Lattnerba025252001-07-23 23:03:12 +0000598OPTIONS:
Chris Lattner2e19f3e2002-07-25 19:27:01 +0000599 Choose optimization level:
600 -g - No optimizations, enable debugging
601 -O1 - Enable trivial optimizations
602 -O2 - Enable default optimizations
603 -O3 - Enable expensive optimizations
Chris Lattner74ea5a62002-08-07 18:27:04 +0000604 <b>-debug_level - Set the debugging level:
Chris Lattner2e19f3e2002-07-25 19:27:01 +0000605 =none - disable debug information
606 =quick - enable quick debug information
Chris Lattner74ea5a62002-08-07 18:27:04 +0000607 =detailed - enable detailed debug information</b>
Chris Lattner2e19f3e2002-07-25 19:27:01 +0000608 -f - Overwrite output files
609 -help - display available options (--help-hidden for more)
610 -o &lt;filename&gt; - Specify output filename
611 -quiet - Don't print informational messages
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000612</pre>
Chris Lattnerba025252001-07-23 23:03:12 +0000613
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000614<p>Again, the only structural difference between the debug level declaration and
Chris Lattner2e19f3e2002-07-25 19:27:01 +0000615the optimiation level declaration is that the debug level declaration includes
616an option name (<tt>"debug_level"</tt>), which automatically changes how the
617library processes the argument. The CommandLine library supports both forms so
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000618that you can choose the form most appropriate for your application.</p>
Chris Lattner2e19f3e2002-07-25 19:27:01 +0000619
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000620</div>
Chris Lattnerba025252001-07-23 23:03:12 +0000621
622<!-- ======================================================================= -->
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000623<div class="doc_subsection">
624 <a name="list">Parsing a list of options</a>
625</div>
Chris Lattnerba025252001-07-23 23:03:12 +0000626
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000627<div class="doc_text">
628
629<p>Now that we have the standard run of the mill argument types out of the way,
Chris Lattner2e19f3e2002-07-25 19:27:01 +0000630lets get a little wild and crazy. Lets say that we want our optimizer to accept
631a <b>list</b> of optimizations to perform, allowing duplicates. For example, we
632might want to run: "<tt>compiler -dce -constprop -inline -dce -strip</tt>". In
633this case, the order of the arguments and the number of appearances is very
Chris Lattner50761292002-08-06 19:36:06 +0000634important. This is what the "<tt><a href="#cl::list">cl::list</a></tt>"
635template is for. First, start by defining an enum of the optimizations that you
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000636would like to perform:</p>
Chris Lattnerba025252001-07-23 23:03:12 +0000637
638<pre>
639enum Opts {
Chris Lattner2e19f3e2002-07-25 19:27:01 +0000640 // 'inline' is a C++ keyword, so name it 'inlining'
Chris Lattnerba025252001-07-23 23:03:12 +0000641 dce, constprop, inlining, strip
Chris Lattner2e19f3e2002-07-25 19:27:01 +0000642};
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000643</pre>
Chris Lattnerba025252001-07-23 23:03:12 +0000644
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000645<p>Then define your "<tt><a href="#cl::list">cl::list</a></tt>" variable:</p>
Chris Lattnerba025252001-07-23 23:03:12 +0000646
647<pre>
Chris Lattner50761292002-08-06 19:36:06 +0000648<a href="#cl::list">cl::list</a>&lt;Opts&gt; OptimizationList(<a href="#cl::desc">cl::desc</a>("<i>Available Optimizations:</i>"),
649 <a href="#cl::values">cl::values</a>(
Chris Lattner2e19f3e2002-07-25 19:27:01 +0000650 clEnumVal(dce , "<i>Dead Code Elimination</i>"),
Misha Brukman373086d2003-05-20 21:01:22 +0000651 clEnumVal(constprop , "<i>Constant Propagation</i>"),
Chris Lattner2e19f3e2002-07-25 19:27:01 +0000652 clEnumValN(inlining, "<i>inline</i>", "<i>Procedure Integration</i>"),
653 clEnumVal(strip , "<i>Strip Symbols</i>"),
Chris Lattner1fbd76e2004-07-16 00:10:54 +0000654 clEnumValEnd));
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000655</pre>
Chris Lattnerba025252001-07-23 23:03:12 +0000656
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000657<p>This defines a variable that is conceptually of the type
Chris Lattner2e19f3e2002-07-25 19:27:01 +0000658"<tt>std::vector&lt;enum Opts&gt;</tt>". Thus, you can access it with standard
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000659vector methods:</p>
Chris Lattnerba025252001-07-23 23:03:12 +0000660
661<pre>
Chris Lattner2e19f3e2002-07-25 19:27:01 +0000662 for (unsigned i = 0; i != OptimizationList.size(); ++i)
Chris Lattnerba025252001-07-23 23:03:12 +0000663 switch (OptimizationList[i])
664 ...
665</pre>
666
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000667<p>... to iterate through the list of options specified.</p>
Chris Lattnerba025252001-07-23 23:03:12 +0000668
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000669<p>Note that the "<tt><a href="#cl::list">cl::list</a></tt>" template is
670completely general and may be used with any data types or other arguments that
671you can use with the "<tt><a href="#cl::opt">cl::opt</a></tt>" template. One
672especially useful way to use a list is to capture all of the positional
673arguments together if there may be more than one specified. In the case of a
674linker, for example, the linker takes several '<tt>.o</tt>' files, and needs to
675capture them into a list. This is naturally specified as:</p>
Chris Lattner03133252002-04-13 18:35:59 +0000676
677<pre>
678...
Chris Lattner50761292002-08-06 19:36:06 +0000679<a href="#cl::list">cl::list</a>&lt;std::string&gt; InputFilenames(<a href="#cl::Positional">cl::Positional</a>, <a href="#cl::desc">cl::desc</a>("&lt;Input files&gt;"), <a href="#cl::OneOrMore">cl::OneOrMore</a>);
Chris Lattner03133252002-04-13 18:35:59 +0000680...
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000681</pre>
Chris Lattner03133252002-04-13 18:35:59 +0000682
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000683<p>This variable works just like a "<tt>vector&lt;string&gt;</tt>" object. As
Chris Lattner2e19f3e2002-07-25 19:27:01 +0000684such, accessing the list is simple, just like above. In this example, we used
Chris Lattner50761292002-08-06 19:36:06 +0000685the <tt><a href="#cl::OneOrMore">cl::OneOrMore</a></tt> modifier to inform the
686CommandLine library that it is an error if the user does not specify any
687<tt>.o</tt> files on our command line. Again, this just reduces the amount of
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000688checking we have to do.</p>
Chris Lattner03133252002-04-13 18:35:59 +0000689
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000690</div>
Chris Lattner03133252002-04-13 18:35:59 +0000691
Chris Lattner74ea5a62002-08-07 18:27:04 +0000692<!-- ======================================================================= -->
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000693<div class="doc_subsection">
694 <a name="description">Adding freeform text to help output</a>
695</div>
Chris Lattner74ea5a62002-08-07 18:27:04 +0000696
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000697<div class="doc_text">
698
699<p>As our program grows and becomes more mature, we may decide to put summary
Chris Lattner74ea5a62002-08-07 18:27:04 +0000700information about what it does into the help output. The help output is styled
701to look similar to a Unix <tt>man</tt> page, providing concise information about
702a program. Unix <tt>man</tt> pages, however often have a description about what
703the program does. To add this to your CommandLine program, simply pass a third
704argument to the <a
705href="#cl::ParseCommandLineOptions"><tt>cl::ParseCommandLineOptions</tt></a>
706call in main. This additional argument is then printed as the overview
707information for your program, allowing you to include any additional information
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000708that you want. For example:</p>
Chris Lattner74ea5a62002-08-07 18:27:04 +0000709
710<pre>
711int main(int argc, char **argv) {
712 <a href="#cl::ParseCommandLineOptions">cl::ParseCommandLineOptions</a>(argc, argv, " CommandLine compiler example\n\n"
713 " This program blah blah blah...\n");
714 ...
715}
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000716</pre>
Chris Lattner74ea5a62002-08-07 18:27:04 +0000717
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000718<p>Would yield the help output:</p>
Chris Lattner74ea5a62002-08-07 18:27:04 +0000719
720<pre>
721<b>OVERVIEW: CommandLine compiler example
722
723 This program blah blah blah...</b>
724
725USAGE: compiler [options] &lt;input file&gt;
726
727OPTIONS:
728 ...
729 -help - display available options (--help-hidden for more)
730 -o &lt;filename&gt; - Specify output filename
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000731</pre>
Chris Lattner74ea5a62002-08-07 18:27:04 +0000732
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000733</div>
Chris Lattner74ea5a62002-08-07 18:27:04 +0000734
735
Chris Lattnerba025252001-07-23 23:03:12 +0000736<!-- *********************************************************************** -->
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000737<div class="doc_section">
738 <a name="referenceguide">Reference Guide</a>
739</div>
Chris Lattnerba025252001-07-23 23:03:12 +0000740<!-- *********************************************************************** -->
741
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000742<div class="doc_text">
Chris Lattner50761292002-08-06 19:36:06 +0000743
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000744<p>Now that you know the basics of how to use the CommandLine library, this
745section will give you the detailed information you need to tune how command line
746options work, as well as information on more "advanced" command line option
747processing capabilities.</p>
748
749</div>
Chris Lattner50761292002-08-06 19:36:06 +0000750
751<!-- ======================================================================= -->
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000752<div class="doc_subsection">
753 <a name="positional">Positional Arguments</a>
754</div>
Chris Lattner50761292002-08-06 19:36:06 +0000755
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000756<div class="doc_text">
757
758<p>Positional arguments are those arguments that are not named, and are not
Chris Lattner50761292002-08-06 19:36:06 +0000759specified with a hyphen. Positional arguments should be used when an option is
760specified by its position alone. For example, the standard Unix <tt>grep</tt>
761tool takes a regular expression argument, and an optional filename to search
762through (which defaults to standard input if a filename is not specified).
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000763Using the CommandLine library, this would be specified as:</p>
Chris Lattner50761292002-08-06 19:36:06 +0000764
765<pre>
766<a href="#cl::opt">cl::opt</a>&lt;string&gt; Regex (<a href="#cl::Positional">cl::Positional</a>, <a href="#cl::desc">cl::desc</a>("<i>&lt;regular expression&gt;</i>"), <a href="#cl::Required">cl::Required</a>);
767<a href="#cl::opt">cl::opt</a>&lt;string&gt; Filename(<a href="#cl::Positional">cl::Positional</a>, <a href="#cl::desc">cl::desc</a>("<i>&lt;input file&gt;</i>"), <a href="#cl::init">cl::init</a>("<i>-</i>"));
768</pre>
769
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000770<p>Given these two option declarations, the <tt>--help</tt> output for our grep
771replacement would look like this:</p>
Chris Lattner50761292002-08-06 19:36:06 +0000772
773<pre>
Chris Lattner74ea5a62002-08-07 18:27:04 +0000774USAGE: spiffygrep [options] <b>&lt;regular expression&gt; &lt;input file&gt;</b>
Chris Lattner50761292002-08-06 19:36:06 +0000775
776OPTIONS:
777 -help - display available options (--help-hidden for more)
778</pre>
779
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000780<p>... and the resultant program could be used just like the standard
781<tt>grep</tt> tool.</p>
Chris Lattner50761292002-08-06 19:36:06 +0000782
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000783<p>Positional arguments are sorted by their order of construction. This means
784that command line options will be ordered according to how they are listed in a
Reid Spencerc0f1b212004-08-13 20:19:14 +0000785.cpp file, but will not have an ordering defined if the positional arguments
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000786are defined in multiple .cpp files. The fix for this problem is simply to
787define all of your positional arguments in one .cpp file.</p>
Chris Lattner50761292002-08-06 19:36:06 +0000788
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000789</div>
Chris Lattner50761292002-08-06 19:36:06 +0000790
791
792<!-- _______________________________________________________________________ -->
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000793<div class="doc_subsubsection">
794 <a name="--">Specifying positional options with hyphens</a>
795</div>
Chris Lattner50761292002-08-06 19:36:06 +0000796
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000797<div class="doc_text">
798
799<p>Sometimes you may want to specify a value to your positional argument that
Chris Lattner50761292002-08-06 19:36:06 +0000800starts with a hyphen (for example, searching for '<tt>-foo</tt>' in a file). At
801first, you will have trouble doing this, because it will try to find an argument
802named '<tt>-foo</tt>', and will fail (and single quotes will not save you).
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000803Note that the system <tt>grep</tt> has the same problem:</p>
Chris Lattner50761292002-08-06 19:36:06 +0000804
805<pre>
806 $ spiffygrep '-foo' test.txt
807 Unknown command line argument '-foo'. Try: spiffygrep --help'
808
809 $ grep '-foo' test.txt
810 grep: illegal option -- f
811 grep: illegal option -- o
812 grep: illegal option -- o
813 Usage: grep -hblcnsviw pattern file . . .
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000814</pre>
Chris Lattner50761292002-08-06 19:36:06 +0000815
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000816<p>The solution for this problem is the same for both your tool and the system
Chris Lattner50761292002-08-06 19:36:06 +0000817version: use the '<tt>--</tt>' marker. When the user specifies '<tt>--</tt>' on
818the command line, it is telling the program that all options after the
819'<tt>--</tt>' should be treated as positional arguments, not options. Thus, we
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000820can use it like this:</p>
Chris Lattner50761292002-08-06 19:36:06 +0000821
822<pre>
823 $ spiffygrep -- -foo test.txt
824 ...output...
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000825</pre>
Chris Lattner50761292002-08-06 19:36:06 +0000826
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000827</div>
Chris Lattner50761292002-08-06 19:36:06 +0000828
829<!-- _______________________________________________________________________ -->
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000830<div class="doc_subsubsection">
Reid Spencerc0f1b212004-08-13 20:19:14 +0000831 <a name="getPosition">Determining absolute position with getPosition()</a>
832</div>
833<div class="doc_text">
834 <p>Sometimes an option can affect or modify the meaning of another option. For
835 example, consider <tt>gcc</tt>'s <tt>-x LANG</tt> option. This tells
836 <tt>gcc</tt> to ignore the suffix of subsequent positional arguments and force
837 the file to be interpreted as if it contained source code in language
838 <tt>LANG</tt>. In order to handle this properly , you need to know the
839 absolute position of each argument, especially those in lists, so their
840 interaction(s) can be applied correctly. This is also useful for options like
841 <tt>-llibname</tt> which is actually a positional argument that starts with
842 a dash.</p>
843 <p>So, generally, the problem is that you have two <tt>cl::list</tt> variables
844 that interact in some way. To ensure the correct interaction, you can use the
845 <tt>cl::list::getPosition(optnum)</tt> method. This method returns the
846 absolute position (as found on the command line) of the <tt>optnum</tt>
847 item in the <tt>cl::list</tt>.</p>
848 <p>The idiom for usage is like this:<pre><tt>
849 static cl::list&lt;std::string&gt; Files(cl::Positional, cl::OneOrMore);
850 static cl::listlt;std::string&gt; Libraries("l", cl::ZeroOrMore);
851
852 int main(int argc, char**argv) {
853 // ...
854 std::vector&lt;std::string&gt;::iterator fileIt = Files.begin();
855 std::vector&lt;std::string&gt;::iterator libIt = Libraries.begin();
856 unsigned libPos = 0, filePos = 0;
857 while ( 1 ) {
858 if ( libIt != Libraries.end() )
859 libPos = Libraries.getPosition( libIt - Libraries.begin() );
860 else
861 libPos = 0;
862 if ( fileIt != Files.end() )
863 filePos = Files.getPosition( fileIt - Files.begin() );
864 else
865 filePos = 0;
866
867 if ( filePos != 0 &amp;&amp; (libPos == 0 || filePos &lt; libPos) ) {
868 // Source File Is next
869 ++fileIt;
870 }
871 else if ( libPos != 0 &amp;&amp; (filePos == 0 || libPos &lt; filePos) ) {
872 // Library is next
873 ++libIt;
874 }
875 else
876 break; // we're done with the list
877 }
Reid Spencer07773212004-11-01 09:16:30 +0000878 }
879 </tt></pre></p>
Reid Spencerc0f1b212004-08-13 20:19:14 +0000880 <p>Note that, for compatibility reasons, the <tt>cl::opt</tt> also supports an
881 <tt>unsigned getPosition()</tt> option that will provide the absolute position
882 of that option. You can apply the same approach as above with a
883 <tt>cl::opt</tt> and a <tt>cl::list</tt> option as you can with two lists.</p>
884</div>
885
886<!-- _______________________________________________________________________ -->
887<div class="doc_subsubsection">
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000888 <a name="cl::ConsumeAfter">The <tt>cl::ConsumeAfter</tt> modifier</a>
889</div>
Chris Lattner50761292002-08-06 19:36:06 +0000890
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000891<div class="doc_text">
892
893<p>The <tt>cl::ConsumeAfter</tt> <a href="#formatting">formatting option</a> is
Chris Lattner50761292002-08-06 19:36:06 +0000894used to construct programs that use "interpreter style" option processing. With
895this style of option processing, all arguments specified after the last
896positional argument are treated as special interpreter arguments that are not
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000897interpreted by the command line argument.</p>
Chris Lattner50761292002-08-06 19:36:06 +0000898
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000899<p>As a concrete example, lets say we are developing a replacement for the
900standard Unix Bourne shell (<tt>/bin/sh</tt>). To run <tt>/bin/sh</tt>, first
901you specify options to the shell itself (like <tt>-x</tt> which turns on trace
Chris Lattner50761292002-08-06 19:36:06 +0000902output), then you specify the name of the script to run, then you specify
903arguments to the script. These arguments to the script are parsed by the bourne
904shell command line option processor, but are not interpreted as options to the
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000905shell itself. Using the CommandLine library, we would specify this as:</p>
Chris Lattner50761292002-08-06 19:36:06 +0000906
907<pre>
908<a href="#cl::opt">cl::opt</a>&lt;string&gt; Script(<a href="#cl::Positional">cl::Positional</a>, <a href="#cl::desc">cl::desc</a>("<i>&lt;input script&gt;</i>"), <a href="#cl::init">cl::init</a>("-"));
909<a href="#cl::list">cl::list</a>&lt;string&gt; Argv(<a href="#cl::ConsumeAfter">cl::ConsumeAfter</a>, <a href="#cl::desc">cl::desc</a>("<i>&lt;program arguments&gt;...</i>"));
910<a href="#cl::opt">cl::opt</a>&lt;bool&gt; Trace("<i>x</i>", <a href="#cl::desc">cl::desc</a>("<i>Enable trace output</i>"));
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000911</pre>
Chris Lattner50761292002-08-06 19:36:06 +0000912
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000913<p>which automatically provides the help output:</p>
Chris Lattner50761292002-08-06 19:36:06 +0000914
915<pre>
Chris Lattner74ea5a62002-08-07 18:27:04 +0000916USAGE: spiffysh [options] <b>&lt;input script&gt; &lt;program arguments&gt;...</b>
Chris Lattner50761292002-08-06 19:36:06 +0000917
918OPTIONS:
919 -help - display available options (--help-hidden for more)
Chris Lattner74ea5a62002-08-07 18:27:04 +0000920 <b>-x - Enable trace output</b>
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000921</pre>
Chris Lattner50761292002-08-06 19:36:06 +0000922
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000923<p>At runtime, if we run our new shell replacement as '<tt>spiffysh -x test.sh
924-a -x -y bar</tt>', the <tt>Trace</tt> variable will be set to true, the
Chris Lattner50761292002-08-06 19:36:06 +0000925<tt>Script</tt> variable will be set to "<tt>test.sh</tt>", and the
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000926<tt>Argv</tt> list will contain <tt>["-a", "-x", "-y", "bar"]</tt>, because they
927were specified after the last positional argument (which is the script
928name).</p>
Chris Lattner50761292002-08-06 19:36:06 +0000929
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000930<p>There are several limitations to when <tt>cl::ConsumeAfter</tt> options can
931be specified. For example, only one <tt>cl::ConsumeAfter</tt> can be specified
932per program, there must be at least one <a href="#positional">positional
Chris Lattnerb68bd762004-05-06 22:03:59 +0000933argument</a> specified, there must not be any <a href="#cl::list">cl::list</a>
934positional arguments, and the <tt>cl::ConsumeAfter</tt> option should be a <a
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000935href="#cl::list">cl::list</a> option.</p>
Chris Lattner50761292002-08-06 19:36:06 +0000936
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000937</div>
Chris Lattner50761292002-08-06 19:36:06 +0000938
939<!-- ======================================================================= -->
Chris Lattnerb68bd762004-05-06 22:03:59 +0000940<div class="doc_subsection">
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000941 <a name="storage">Internal vs External Storage</a>
942</div>
Chris Lattner50761292002-08-06 19:36:06 +0000943
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000944<div class="doc_text">
945
946<p>By default, all command line options automatically hold the value that they
Chris Lattner50761292002-08-06 19:36:06 +0000947parse from the command line. This is very convenient in the common case,
948especially when combined with the ability to define command line options in the
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000949files that use them. This is called the internal storage model.</p>
Chris Lattner50761292002-08-06 19:36:06 +0000950
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000951<p>Sometimes, however, it is nice to separate the command line option processing
Chris Lattner50761292002-08-06 19:36:06 +0000952code from the storage of the value parsed. For example, lets say that we have a
953'<tt>-debug</tt>' option that we would like to use to enable debug information
954across the entire body of our program. In this case, the boolean value
955controlling the debug code should be globally accessable (in a header file, for
956example) yet the command line option processing code should not be exposed to
957all of these clients (requiring lots of .cpp files to #include
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000958<tt>CommandLine.h</tt>).</p>
Chris Lattner50761292002-08-06 19:36:06 +0000959
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000960<p>To do this, set up your .h file with your option, like this for example:</p>
Chris Lattner50761292002-08-06 19:36:06 +0000961
962<pre>
963<i>// DebugFlag.h - Get access to the '-debug' command line option
964//
965
966// DebugFlag - This boolean is set to true if the '-debug' command line option
967// is specified. This should probably not be referenced directly, instead, use
968// the DEBUG macro below.
969//</i>
970extern bool DebugFlag;
971
972<i>// DEBUG macro - This macro should be used by code to emit debug information.
973// In the '-debug' option is specified on the command line, and if this is a
974// debug build, then the code specified as the option to the macro will be
975// executed. Otherwise it will not be. Example:
976//
977// DEBUG(cerr << "Bitset contains: " << Bitset << "\n");
978//</i>
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000979<span class="doc_red">#ifdef NDEBUG
Chris Lattner50761292002-08-06 19:36:06 +0000980#define DEBUG(X)
981#else
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000982#define DEBUG(X)</span> \
Chris Lattner50761292002-08-06 19:36:06 +0000983 do { if (DebugFlag) { X; } } while (0)
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000984<span class="doc_red">#endif</span>
Chris Lattner50761292002-08-06 19:36:06 +0000985</pre>
986
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000987<p>This allows clients to blissfully use the <tt>DEBUG()</tt> macro, or the
Chris Lattner50761292002-08-06 19:36:06 +0000988<tt>DebugFlag</tt> explicitly if they want to. Now we just need to be able to
989set the <tt>DebugFlag</tt> boolean when the option is set. To do this, we pass
990an additial argument to our command line argument processor, and we specify
Misha Brukmanc86e8b12003-10-24 19:59:21 +0000991where to fill in with the <a href="#cl::location">cl::location</a>
992attribute:</p>
Chris Lattner50761292002-08-06 19:36:06 +0000993
994<pre>
995bool DebugFlag; <i>// the actual value</i>
Chris Lattner76884682003-08-01 21:30:37 +0000996static <a href="#cl::opt">cl::opt</a>&lt;bool, true&gt; <i>// The parser</i>
Misha Brukman374258e2004-05-12 18:42:35 +0000997Debug("<i>debug</i>", <a href="#cl::desc">cl::desc</a>("<i>Enable debug output</i>"), <a href="#cl::Hidden">cl::Hidden</a>,
Chris Lattner50761292002-08-06 19:36:06 +0000998 <a href="#cl::location">cl::location</a>(DebugFlag));
999</pre>
1000
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001001<p>In the above example, we specify "<tt>true</tt>" as the second argument to
1002the <a href="#cl::opt">cl::opt</a> template, indicating that the template should
1003not maintain a copy of the value itself. In addition to this, we specify the <a
Chris Lattner50761292002-08-06 19:36:06 +00001004href="#cl::location">cl::location</a> attribute, so that <tt>DebugFlag</tt> is
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001005automatically set.</p>
Chris Lattner50761292002-08-06 19:36:06 +00001006
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001007</div>
Chris Lattner50761292002-08-06 19:36:06 +00001008
1009<!-- ======================================================================= -->
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001010<div class="doc_subsection">
1011 <a name="attributes">Option Attributes</a>
1012</div>
Chris Lattner50761292002-08-06 19:36:06 +00001013
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001014<div class="doc_text">
1015
1016<p>This section describes the basic attributes that you can specify on
1017options.</p>
Chris Lattner50761292002-08-06 19:36:06 +00001018
1019<ul>
1020
1021<li>The option name attribute (which is required for all options, except <a
1022href="#positional">positional options</a>) specifies what the option name is.
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001023This option is specified in simple double quotes:
Chris Lattner50761292002-08-06 19:36:06 +00001024
Chris Lattner74ea5a62002-08-07 18:27:04 +00001025<pre>
1026<a href="#cl::opt">cl::opt</a>&lt;<b>bool</b>&gt; Quiet("<i>quiet</i>");
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001027</pre>
Chris Lattner50761292002-08-06 19:36:06 +00001028
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001029</li>
1030
1031<li><a name="cl::desc">The <b><tt>cl::desc</tt></b></a> attribute specifies a
Chris Lattner50761292002-08-06 19:36:06 +00001032description for the option to be shown in the <tt>--help</tt> output for the
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001033program.</li>
Chris Lattner50761292002-08-06 19:36:06 +00001034
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001035<li><a name="cl::value_desc">The <b><tt>cl::value_desc</tt></b></a> attribute
Chris Lattner50761292002-08-06 19:36:06 +00001036specifies a string that can be used to fine tune the <tt>--help</tt> output for
1037a command line option. Look <a href="#value_desc_example">here</a> for an
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001038example.</li>
Chris Lattner50761292002-08-06 19:36:06 +00001039
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001040<li><a name="cl::init">The <b><tt>cl::init</tt></b></a> attribute specifies an
Chris Lattner50761292002-08-06 19:36:06 +00001041inital value for a <a href="#cl::opt">scalar</a> option. If this attribute is
1042not specified then the command line option value defaults to the value created
Brian Gaeke8b89b2b2003-08-19 22:56:22 +00001043by the default constructor for the type. <b>Warning</b>: If you specify both
1044<b><tt>cl::init</tt></b> and <b><tt>cl::location</tt></b> for an option,
1045you must specify <b><tt>cl::location</tt></b> first, so that when the
1046command-line parser sees <b><tt>cl::init</tt></b>, it knows where to put the
1047initial value. (You will get an error at runtime if you don't put them in
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001048the right order.)</li>
Chris Lattner50761292002-08-06 19:36:06 +00001049
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001050<li><a name="cl::location">The <b><tt>cl::location</tt></b></a> attribute where to
Chris Lattner50761292002-08-06 19:36:06 +00001051store the value for a parsed command line option if using external storage. See
1052the section on <a href="#storage">Internal vs External Storage</a> for more
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001053information.</li>
Chris Lattner50761292002-08-06 19:36:06 +00001054
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001055<li><a name="cl::aliasopt">The <b><tt>cl::aliasopt</tt></b></a> attribute
1056specifies which option a <a href="#cl::alias">cl::alias</a> option is an alias
1057for.</li>
Chris Lattner50761292002-08-06 19:36:06 +00001058
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001059<li><a name="cl::values">The <b><tt>cl::values</tt></b></a> attribute specifies
1060the string-to-value mapping to be used by the generic parser. It takes a
Chris Lattner1fbd76e2004-07-16 00:10:54 +00001061<b>clEnumValEnd terminated</b> list of (option, value, description) triplets
1062that
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001063specify the option name, the value mapped to, and the description shown in the
1064<tt>--help</tt> for the tool. Because the generic parser is used most
1065frequently with enum values, two macros are often useful:
1066
Chris Lattner50761292002-08-06 19:36:06 +00001067<ol>
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001068
1069<li><a name="clEnumVal">The <b><tt>clEnumVal</tt></b></a> macro is used as a
1070nice simple way to specify a triplet for an enum. This macro automatically
1071makes the option name be the same as the enum name. The first option to the
1072macro is the enum, the second is the description for the command line
1073option.</li>
1074
1075<li><a name="clEnumValN">The <b><tt>clEnumValN</tt></b></a> macro is used to
1076specify macro options where the option name doesn't equal the enum name. For
1077this macro, the first argument is the enum value, the second is the flag name,
1078and the second is the description.</li>
1079
Chris Lattner50761292002-08-06 19:36:06 +00001080</ol>
1081
1082You will get a compile time error if you try to use cl::values with a parser
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001083that does not support it.</li>
Chris Lattner50761292002-08-06 19:36:06 +00001084
1085</ul>
1086
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001087</div>
Chris Lattner50761292002-08-06 19:36:06 +00001088
1089<!-- ======================================================================= -->
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001090<div class="doc_subsection">
1091 <a name="modifiers">Option Modifiers</a>
1092</div>
Chris Lattner50761292002-08-06 19:36:06 +00001093
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001094<div class="doc_text">
1095
1096<p>Option modifiers are the flags and expressions that you pass into the
Chris Lattner50761292002-08-06 19:36:06 +00001097constructors for <tt><a href="#cl::opt">cl::opt</a></tt> and <tt><a
1098href="#cl::list">cl::list</a></tt>. These modifiers give you the ability to
1099tweak how options are parsed and how <tt>--help</tt> output is generated to fit
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001100your application well.</p>
Chris Lattner50761292002-08-06 19:36:06 +00001101
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001102<p>These options fall into five main catagories:</p>
Chris Lattner50761292002-08-06 19:36:06 +00001103
1104<ol>
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001105<li><a href="#hiding">Hiding an option from <tt>--help</tt> output</a></li>
1106<li><a href="#numoccurrences">Controlling the number of occurrences
1107 required and allowed</a></li>
Chris Lattner50761292002-08-06 19:36:06 +00001108<li><a href="#valrequired">Controlling whether or not a value must be
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001109 specified</a></li>
1110<li><a href="#formatting">Controlling other formatting options</a></li>
1111<li><a href="#misc">Miscellaneous option modifiers</a></li>
1112</ol>
Chris Lattner50761292002-08-06 19:36:06 +00001113
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001114<p>It is not possible to specify two options from the same catagory (you'll get
1115a runtime error) to a single option, except for options in the miscellaneous
Chris Lattner7333cc12003-05-22 20:36:06 +00001116catagory. The CommandLine library specifies defaults for all of these settings
1117that are the most useful in practice and the most common, which mean that you
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001118usually shouldn't have to worry about these.</p>
Chris Lattner50761292002-08-06 19:36:06 +00001119
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001120</div>
Chris Lattner50761292002-08-06 19:36:06 +00001121
1122<!-- _______________________________________________________________________ -->
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001123<div class="doc_subsubsection">
1124 <a name="hiding">Hiding an option from <tt>--help</tt> output</a>
1125</div>
Chris Lattner50761292002-08-06 19:36:06 +00001126
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001127<div class="doc_text">
1128
1129<p>The <tt>cl::NotHidden</tt>, <tt>cl::Hidden</tt>, and
1130<tt>cl::ReallyHidden</tt> modifiers are used to control whether or not an option
1131appears in the <tt>--help</tt> and <tt>--help-hidden</tt> output for the
1132compiled program:</p>
Chris Lattner50761292002-08-06 19:36:06 +00001133
1134<ul>
1135
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001136<li><a name="cl::NotHidden">The <b><tt>cl::NotHidden</tt></b></a> modifier
1137(which is the default for <tt><a href="#cl::opt">cl::opt</a></tt> and <tt><a
Chris Lattner50761292002-08-06 19:36:06 +00001138href="#cl::list">cl::list</a></tt> options), indicates the option is to appear
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001139in both help listings.</li>
Chris Lattner50761292002-08-06 19:36:06 +00001140
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001141<li><a name="cl::Hidden">The <b><tt>cl::Hidden</tt></b></a> modifier (which is the
Chris Lattner50761292002-08-06 19:36:06 +00001142default for <tt><a href="#cl::alias">cl::alias</a></tt> options), indicates that
1143the option should not appear in the <tt>--help</tt> output, but should appear in
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001144the <tt>--help-hidden</tt> output.</li>
Chris Lattner50761292002-08-06 19:36:06 +00001145
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001146<li><a name="cl::ReallyHidden">The <b><tt>cl::ReallyHidden</tt></b></a> modifier,
1147indicates that the option should not appear in any help output.</li>
1148
Chris Lattner50761292002-08-06 19:36:06 +00001149</ul>
1150
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001151</div>
1152
Chris Lattner50761292002-08-06 19:36:06 +00001153<!-- _______________________________________________________________________ -->
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001154<div class="doc_subsubsection">
1155 <a name="numoccurrences">Controlling the number of occurrences required and
1156 allowed</a>
1157</div>
Chris Lattner50761292002-08-06 19:36:06 +00001158
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001159<div class="doc_text">
1160
1161<p>This group of options is used to control how many time an option is allowed
1162(or required) to be specified on the command line of your program. Specifying a
Chris Lattner50761292002-08-06 19:36:06 +00001163value for this setting allows the CommandLine library to do error checking for
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001164you.</p>
Chris Lattner50761292002-08-06 19:36:06 +00001165
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001166<p>The allowed values for this option group are:</p>
Chris Lattner50761292002-08-06 19:36:06 +00001167
1168<ul>
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001169
1170<li><a name="cl::Optional">The <b><tt>cl::Optional</tt></b></a> modifier (which
1171is the default for the <tt><a href="#cl::opt">cl::opt</a></tt> and <tt><a
Chris Lattner50761292002-08-06 19:36:06 +00001172href="#cl::alias">cl::alias</a></tt> classes) indicates that your program will
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001173allow either zero or one occurrence of the option to be specified.</li>
Chris Lattner50761292002-08-06 19:36:06 +00001174
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001175<li><a name="cl::ZeroOrMore">The <b><tt>cl::ZeroOrMore</tt></b></a> modifier
1176(which is the default for the <tt><a href="#cl::list">cl::list</a></tt> class)
1177indicates that your program will allow the option to be specified zero or more
1178times.</li>
Chris Lattner50761292002-08-06 19:36:06 +00001179
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001180<li><a name="cl::Required">The <b><tt>cl::Required</tt></b></a> modifier
1181indicates that the specified option must be specified exactly one time.</li>
Chris Lattner50761292002-08-06 19:36:06 +00001182
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001183<li><a name="cl::OneOrMore">The <b><tt>cl::OneOrMore</tt></b></a> modifier
1184indicates that the option must be specified at least one time.</li>
Chris Lattner50761292002-08-06 19:36:06 +00001185
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001186<li>The <b><tt>cl::ConsumeAfter</tt></b> modifier is described in the <a
1187href="#positional">Positional arguments section</a></li>
Chris Lattner50761292002-08-06 19:36:06 +00001188
1189</ul>
1190
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001191<p>If an option is not specified, then the value of the option is equal to the
Chris Lattner50761292002-08-06 19:36:06 +00001192value specified by the <tt><a href="#cl::init">cl::init</a></tt> attribute. If
1193the <tt><a href="#cl::init">cl::init</a></tt> attribute is not specified, the
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001194option value is initialized with the default constructor for the data type.</p>
Chris Lattner50761292002-08-06 19:36:06 +00001195
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001196<p>If an option is specified multiple times for an option of the <tt><a
1197href="#cl::opt">cl::opt</a></tt> class, only the last value will be
1198retained.</p>
Chris Lattner50761292002-08-06 19:36:06 +00001199
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001200</div>
Chris Lattner50761292002-08-06 19:36:06 +00001201
1202<!-- _______________________________________________________________________ -->
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001203<div class="doc_subsubsection">
1204 <a name="valrequired">Controlling whether or not a value must be specified</a>
1205</div>
Chris Lattner50761292002-08-06 19:36:06 +00001206
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001207<div class="doc_text">
1208
1209<p>This group of options is used to control whether or not the option allows a
Chris Lattner50761292002-08-06 19:36:06 +00001210value to be present. In the case of the CommandLine library, a value is either
1211specified with an equal sign (e.g. '<tt>-index-depth=17</tt>') or as a trailing
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001212string (e.g. '<tt>-o a.out</tt>').</p>
Chris Lattner50761292002-08-06 19:36:06 +00001213
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001214<p>The allowed values for this option group are:</p>
Chris Lattner50761292002-08-06 19:36:06 +00001215
1216<ul>
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001217
1218<li><a name="cl::ValueOptional">The <b><tt>cl::ValueOptional</tt></b></a> modifier
Chris Lattner50761292002-08-06 19:36:06 +00001219(which is the default for <tt>bool</tt> typed options) specifies that it is
1220acceptable to have a value, or not. A boolean argument can be enabled just by
1221appearing on the command line, or it can have an explicit '<tt>-foo=true</tt>'.
1222If an option is specified with this mode, it is illegal for the value to be
1223provided without the equal sign. Therefore '<tt>-foo true</tt>' is illegal. To
1224get this behavior, you must use the <a
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001225href="#cl::ValueRequired">cl::ValueRequired</a> modifier.</li>
Chris Lattner50761292002-08-06 19:36:06 +00001226
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001227<li><a name="cl::ValueRequired">The <b><tt>cl::ValueRequired</tt></b></a> modifier
Chris Lattner50761292002-08-06 19:36:06 +00001228(which is the default for all other types except for <a
1229href="#onealternative">unnamed alternatives using the generic parser</a>)
1230specifies that a value must be provided. This mode informs the command line
1231library that if an option is not provides with an equal sign, that the next
1232argument provided must be the value. This allows things like '<tt>-o
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001233a.out</tt>' to work.</li>
Chris Lattner50761292002-08-06 19:36:06 +00001234
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001235<li><a name="cl::ValueDisallowed">The <b><tt>cl::ValueDisallowed</tt></b></a>
1236modifier (which is the default for <a href="#onealternative">unnamed
1237alternatives using the generic parser</a>) indicates that it is a runtime error
1238for the user to specify a value. This can be provided to disallow users from
1239providing options to boolean options (like '<tt>-foo=true</tt>').</li>
Chris Lattner50761292002-08-06 19:36:06 +00001240
1241</ul>
1242
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001243<p>In general, the default values for this option group work just like you would
Chris Lattner50761292002-08-06 19:36:06 +00001244want them to. As mentioned above, you can specify the <a
1245href="#cl::ValueDisallowed">cl::ValueDisallowed</a> modifier to a boolean
1246argument to restrict your command line parser. These options are mostly useful
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001247when <a href="#extensionguide">extending the library</a>.</p>
Chris Lattner50761292002-08-06 19:36:06 +00001248
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001249</div>
Chris Lattner50761292002-08-06 19:36:06 +00001250
1251<!-- _______________________________________________________________________ -->
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001252<div class="doc_subsubsection">
1253 <a name="formatting">Controlling other formatting options</a>
1254</div>
Chris Lattner50761292002-08-06 19:36:06 +00001255
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001256<div class="doc_text">
1257
1258<p>The formatting option group is used to specify that the command line option
1259has special abilities and is otherwise different from other command line
1260arguments. As usual, you can only specify at most one of these arguments.</p>
Chris Lattner50761292002-08-06 19:36:06 +00001261
1262<ul>
Chris Lattner50761292002-08-06 19:36:06 +00001263
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001264<li><a name="cl::NormalFormatting">The <b><tt>cl::NormalFormatting</tt></b></a>
1265modifier (which is the default all options) specifies that this option is
1266"normal".</li>
Chris Lattner50761292002-08-06 19:36:06 +00001267
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001268<li><a name="cl::Positional">The <b><tt>cl::Positional</tt></b></a> modifier
1269specifies that this is a positional argument, that does not have a command line
1270option associated with it. See the <a href="#positional">Positional
1271Arguments</a> section for more information.</li>
Chris Lattner50761292002-08-06 19:36:06 +00001272
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001273<li>The <b><a href="#cl::ConsumeAfter"><tt>cl::ConsumeAfter</tt></a></b> modifier
1274specifies that this option is used to capture "interpreter style" arguments. See <a href="#cl::ConsumeAfter">this section for more information</a>.</li>
Chris Lattner50761292002-08-06 19:36:06 +00001275
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001276<li><a name="cl::Prefix">The <b><tt>cl::Prefix</tt></b></a> modifier specifies
1277that this option prefixes its value. With 'Prefix' options, there is no equal
1278sign that separates the value from the option name specified. This is useful
1279for processing odd arguments like '<tt>-lmalloc -L/usr/lib'</tt> in a linker
1280tool. Here, the '<tt>l</tt>' and '<tt>L</tt>' options are normal string (list)
Chris Lattner50761292002-08-06 19:36:06 +00001281options, that have the <a href="#cl::Prefix">cl::Prefix</a> modifier added to
1282allow the CommandLine library to recognize them. Note that <a
1283href="#cl::Prefix">cl::Prefix</a> options must not have the <a
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001284href="#cl::ValueDisallowed">cl::ValueDisallowed</a> modifier specified.</li>
Chris Lattner50761292002-08-06 19:36:06 +00001285
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001286<li><a name="cl::Grouping">The <b><tt>cl::Grouping</tt></b></a> modifier is used
1287to implement unix style tools (like <tt>ls</tt>) that have lots of single letter
Chris Lattner50761292002-08-06 19:36:06 +00001288arguments, but only require a single dash. For example, the '<tt>ls -labF</tt>'
1289command actually enables four different options, all of which are single
1290letters. Note that <a href="#cl::Grouping">cl::Grouping</a> options cannot have
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001291values.</li>
Chris Lattner50761292002-08-06 19:36:06 +00001292
1293</ul>
1294
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001295<p>The CommandLine library does not restrict how you use the <a
Chris Lattner50761292002-08-06 19:36:06 +00001296href="#cl::Prefix">cl::Prefix</a> or <a href="#cl::Grouping">cl::Grouping</a>
1297modifiers, but it is possible to specify ambiguous argument settings. Thus, it
1298is possible to have multiple letter options that are prefix or grouping options,
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001299and they will still work as designed.</p>
Chris Lattner50761292002-08-06 19:36:06 +00001300
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001301<p>To do this, the CommandLine library uses a greedy algorithm to parse the
1302input option into (potentially multiple) prefix and grouping options. The
1303strategy basically looks like this:</p>
Chris Lattner50761292002-08-06 19:36:06 +00001304
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001305<p><tt>parse(string OrigInput) {</tt>
Misha Brukman374258e2004-05-12 18:42:35 +00001306
Chris Lattner50761292002-08-06 19:36:06 +00001307<ol>
1308<li><tt>string input = OrigInput;</tt>
1309<li><tt>if (isOption(input)) return getOption(input).parse();</tt>&nbsp;&nbsp;&nbsp;&nbsp;<i>// Normal option</i>
1310<li><tt>while (!isOption(input) &amp;&amp; !input.empty()) input.pop_back();</tt>&nbsp;&nbsp;&nbsp;&nbsp;<i>// Remove the last letter</i>
1311<li><tt>if (input.empty()) return error();</tt>&nbsp;&nbsp;&nbsp;&nbsp;<i>// No matching option</i>
1312<li><tt>if (getOption(input).isPrefix())<br>
1313&nbsp;&nbsp;return getOption(input).parse(input);</tt>
1314<li><tt>while (!input.empty()) {&nbsp;&nbsp;&nbsp;&nbsp;<i>// Must be grouping options</i><br>
1315&nbsp;&nbsp;getOption(input).parse();<br>
1316&nbsp;&nbsp;OrigInput.erase(OrigInput.begin(), OrigInput.begin()+input.length());<br>
1317&nbsp;&nbsp;input = OrigInput;<br>
1318&nbsp;&nbsp;while (!isOption(input) &amp;&amp; !input.empty()) input.pop_back();<br>
1319}</tt>
Misha Brukman374258e2004-05-12 18:42:35 +00001320<li><tt>if (!OrigInput.empty()) error();</tt></li>
Chris Lattner50761292002-08-06 19:36:06 +00001321
1322</ol>
Misha Brukman374258e2004-05-12 18:42:35 +00001323
1324<p><tt>}</tt></p>
Chris Lattner50761292002-08-06 19:36:06 +00001325
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001326</div>
Chris Lattner7333cc12003-05-22 20:36:06 +00001327
1328<!-- _______________________________________________________________________ -->
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001329<div class="doc_subsubsection">
1330 <a name="misc">Miscellaneous option modifiers</a>
1331</div>
Chris Lattner7333cc12003-05-22 20:36:06 +00001332
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001333<div class="doc_text">
1334
1335<p>The miscellaneous option modifiers are the only flags where you can specify
1336more than one flag from the set: they are not mutually exclusive. These flags
1337specify boolean properties that modify the option.</p>
Chris Lattner7333cc12003-05-22 20:36:06 +00001338
1339<ul>
1340
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001341<li><a name="cl::CommaSeparated">The <b><tt>cl::CommaSeparated</tt></b></a> modifier
Chris Lattner7333cc12003-05-22 20:36:06 +00001342indicates that any commas specified for an option's value should be used to
1343split the value up into multiple values for the option. For example, these two
1344options are equivalent when <tt>cl::CommaSeparated</tt> is specified:
1345"<tt>-foo=a -foo=b -foo=c</tt>" and "<tt>-foo=a,b,c</tt>". This option only
1346makes sense to be used in a case where the option is allowed to accept one or
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001347more values (i.e. it is a <a href="#cl::list">cl::list</a> option).</li>
1348
Chris Lattnerb68bd762004-05-06 22:03:59 +00001349<li><a name="cl::PositionalEatsArgs">The
1350<b><tt>cl::PositionalEatsArgs</tt></b></a> modifier (which only applies to
1351positional arguments, and only makes sense for lists) indicates that positional
1352argument should consume any strings after it (including strings that start with
1353a "-") up until another recognized positional argument. For example, if you
1354have two "eating" positional arguments "<tt>pos1</tt>" and "<tt>pos2</tt>" the
1355string "<tt>-pos1 -foo -bar baz -pos2 -bork</tt>" would cause the "<tt>-foo -bar
1356-baz</tt>" strings to be applied to the "<tt>-pos1</tt>" option and the
1357"<tt>-bork</tt>" string to be applied to the "<tt>-pos2</tt>" option.</li>
1358
Chris Lattner7333cc12003-05-22 20:36:06 +00001359</ul>
1360
Chris Lattnerb68bd762004-05-06 22:03:59 +00001361<p>So far, these are the only two miscellaneous option modifiers.</p>
Chris Lattner7333cc12003-05-22 20:36:06 +00001362
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001363</div>
Chris Lattner7333cc12003-05-22 20:36:06 +00001364
Chris Lattner50761292002-08-06 19:36:06 +00001365<!-- ======================================================================= -->
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001366<div class="doc_subsection">
1367 <a name="toplevel">Top-Level Classes and Functions</a>
1368</div>
Chris Lattner50761292002-08-06 19:36:06 +00001369
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001370<div class="doc_text">
1371
1372<p>Despite all of the built-in flexibility, the CommandLine option library
1373really only consists of one function (<a
Chris Lattner74ea5a62002-08-07 18:27:04 +00001374href="#cl::ParseCommandLineOptions"><tt>cl::ParseCommandLineOptions</tt></a>)
1375and three main classes: <a href="#cl::opt"><tt>cl::opt</tt></a>, <a
1376href="#cl::list"><tt>cl::list</tt></a>, and <a
1377href="#cl::alias"><tt>cl::alias</tt></a>. This section describes these three
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001378classes in detail.</p>
1379
1380</div>
Chris Lattner74ea5a62002-08-07 18:27:04 +00001381
1382<!-- _______________________________________________________________________ -->
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001383<div class="doc_subsubsection">
1384 <a name="cl::ParseCommandLineOptions">The <tt>cl::ParseCommandLineOptions</tt>
1385 function</a>
1386</div>
Chris Lattner74ea5a62002-08-07 18:27:04 +00001387
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001388<div class="doc_text">
1389
1390<p>The <tt>cl::ParseCommandLineOptions</tt> function is designed to be called
Chris Lattner74ea5a62002-08-07 18:27:04 +00001391directly from <tt>main</tt>, and is used to fill in the values of all of the
1392command line option variables once <tt>argc</tt> and <tt>argv</tt> are
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001393available.</p>
Chris Lattner74ea5a62002-08-07 18:27:04 +00001394
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001395<p>The <tt>cl::ParseCommandLineOptions</tt> function requires two parameters
Chris Lattner74ea5a62002-08-07 18:27:04 +00001396(<tt>argc</tt> and <tt>argv</tt>), but may also take an optional third parameter
1397which holds <a href="#description">additional extra text</a> to emit when the
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001398<tt>--help</tt> option is invoked.</p>
Chris Lattner74ea5a62002-08-07 18:27:04 +00001399
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001400</div>
Chris Lattner50761292002-08-06 19:36:06 +00001401
1402<!-- _______________________________________________________________________ -->
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001403<div class="doc_subsubsection">
1404 <a name="cl::ParseEnvironmentOptions">The <tt>cl::ParseEnvironmentOptions</tt>
1405 function</a>
1406</div>
Brian Gaekef2ea9e02003-08-19 23:11:43 +00001407
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001408<div class="doc_text">
Brian Gaekef2ea9e02003-08-19 23:11:43 +00001409
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001410<p>The <tt>cl::ParseEnvironmentOptions</tt> function has mostly the same effects
1411as <a
1412href="#cl::ParseCommandLineOptions"><tt>cl::ParseCommandLineOptions</tt></a>,
1413except that it is designed to take values for options from an environment
1414variable, for those cases in which reading the command line is not convenient or
1415not desired. It fills in the values of all the command line option variables
1416just like <a
1417href="#cl::ParseCommandLineOptions"><tt>cl::ParseCommandLineOptions</tt></a>
1418does.</p>
1419
1420<p>It takes three parameters: first, the name of the program (since
1421<tt>argv</tt> may not be available, it can't just look in <tt>argv[0]</tt>),
1422second, the name of the environment variable to examine, and third, the optional
Brian Gaekef2ea9e02003-08-19 23:11:43 +00001423<a href="#description">additional extra text</a> to emit when the
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001424<tt>--help</tt> option is invoked.</p>
Brian Gaekef2ea9e02003-08-19 23:11:43 +00001425
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001426<p><tt>cl::ParseEnvironmentOptions</tt> will break the environment
Brian Gaekef2ea9e02003-08-19 23:11:43 +00001427variable's value up into words and then process them using
1428<a href="#cl::ParseCommandLineOptions"><tt>cl::ParseCommandLineOptions</tt></a>.
1429<b>Note:</b> Currently <tt>cl::ParseEnvironmentOptions</tt> does not support
1430quoting, so an environment variable containing <tt>-option "foo bar"</tt> will
1431be parsed as three words, <tt>-option</tt>, <tt>"foo</tt>, and <tt>bar"</tt>,
1432which is different from what you would get from the shell with the same
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001433input.</p>
1434
1435</div>
Brian Gaekef2ea9e02003-08-19 23:11:43 +00001436
1437<!-- _______________________________________________________________________ -->
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001438<div class="doc_subsubsection">
1439 <a name="cl::opt">The <tt>cl::opt</tt> class</a>
1440</div>
Chris Lattner50761292002-08-06 19:36:06 +00001441
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001442<div class="doc_text">
1443
1444<p>The <tt>cl::opt</tt> class is the class used to represent scalar command line
Chris Lattner50761292002-08-06 19:36:06 +00001445options, and is the one used most of the time. It is a templated class which
1446can take up to three arguments (all except for the first have default values
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001447though):</p>
Chris Lattner50761292002-08-06 19:36:06 +00001448
1449<pre>
Chris Lattner74ea5a62002-08-07 18:27:04 +00001450<b>namespace</b> cl {
1451 <b>template</b> &lt;<b>class</b> DataType, <b>bool</b> ExternalStorage = <b>false</b>,
1452 <b>class</b> ParserClass = parser&lt;DataType&gt; &gt;
1453 <b>class</b> opt;
Chris Lattner50761292002-08-06 19:36:06 +00001454}
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001455</pre>
Chris Lattner50761292002-08-06 19:36:06 +00001456
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001457<p>The first template argument specifies what underlying data type the command
1458line argument is, and is used to select a default parser implementation. The
1459second template argument is used to specify whether the option should contain
1460the storage for the option (the default) or whether external storage should be
1461used to contain the value parsed for the option (see <a href="#storage">Internal
1462vs External Storage</a> for more information).</p>
Chris Lattner50761292002-08-06 19:36:06 +00001463
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001464<p>The third template argument specifies which parser to use. The default value
Chris Lattner50761292002-08-06 19:36:06 +00001465selects an instantiation of the <tt>parser</tt> class based on the underlying
1466data type of the option. In general, this default works well for most
1467applications, so this option is only used when using a <a
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001468href="#customparser">custom parser</a>.</p>
Chris Lattner50761292002-08-06 19:36:06 +00001469
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001470</div>
Chris Lattner50761292002-08-06 19:36:06 +00001471
1472<!-- _______________________________________________________________________ -->
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001473<div class="doc_subsubsection">
1474 <a name="cl::list">The <tt>cl::list</tt> class</a>
1475</div>
Chris Lattner50761292002-08-06 19:36:06 +00001476
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001477<div class="doc_text">
1478
1479<p>The <tt>cl::list</tt> class is the class used to represent a list of command
Chris Lattner50761292002-08-06 19:36:06 +00001480line options. It too is a templated class which can take up to three
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001481arguments:</p>
Chris Lattner50761292002-08-06 19:36:06 +00001482
1483<pre>
Chris Lattner74ea5a62002-08-07 18:27:04 +00001484<b>namespace</b> cl {
1485 <b>template</b> &lt;<b>class</b> DataType, <b>class</b> Storage = <b>bool</b>,
1486 <b>class</b> ParserClass = parser&lt;DataType&gt; &gt;
1487 <b>class</b> list;
Chris Lattner50761292002-08-06 19:36:06 +00001488}
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001489</pre>
Chris Lattner50761292002-08-06 19:36:06 +00001490
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001491<p>This class works the exact same as the <a
1492href="#cl::opt"><tt>cl::opt</tt></a> class, except that the second argument is
1493the <b>type</b> of the external storage, not a boolean value. For this class,
1494the marker type '<tt>bool</tt>' is used to indicate that internal storage should
1495be used.</p>
Chris Lattner50761292002-08-06 19:36:06 +00001496
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001497</div>
Chris Lattner50761292002-08-06 19:36:06 +00001498
1499<!-- _______________________________________________________________________ -->
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001500<div class="doc_subsubsection">
1501 <a name="cl::alias">The <tt>cl::alias</tt> class</a>
1502</div>
Chris Lattner50761292002-08-06 19:36:06 +00001503
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001504<div class="doc_text">
1505
1506<p>The <tt>cl::alias</tt> class is a nontemplated class that is used to form
1507aliases for other arguments.</p>
Chris Lattner50761292002-08-06 19:36:06 +00001508
1509<pre>
Chris Lattner74ea5a62002-08-07 18:27:04 +00001510<b>namespace</b> cl {
1511 <b>class</b> alias;
Chris Lattner50761292002-08-06 19:36:06 +00001512}
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001513</pre>
Chris Lattner50761292002-08-06 19:36:06 +00001514
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001515<p>The <a href="#cl::aliasopt"><tt>cl::aliasopt</tt></a> attribute should be
1516used to specify which option this is an alias for. Alias arguments default to
1517being <a href="#cl::Hidden">Hidden</a>, and use the aliased options parser to do
1518the conversion from string to data.</p>
Chris Lattner50761292002-08-06 19:36:06 +00001519
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001520</div>
Chris Lattner50761292002-08-06 19:36:06 +00001521
1522<!-- ======================================================================= -->
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001523<div class="doc_subsection">
1524 <a name="builtinparsers">Builtin parsers</a>
1525</div>
Chris Lattner50761292002-08-06 19:36:06 +00001526
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001527<div class="doc_text">
1528
1529<p>Parsers control how the string value taken from the command line is
1530translated into a typed value, suitable for use in a C++ program. By default,
1531the CommandLine library uses an instance of <tt>parser&lt;type&gt;</tt> if the
Chris Lattner50761292002-08-06 19:36:06 +00001532command line option specifies that it uses values of type '<tt>type</tt>'.
1533Because of this, custom option processing is specified with specializations of
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001534the '<tt>parser</tt>' class.</p>
Chris Lattner50761292002-08-06 19:36:06 +00001535
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001536<p>The CommandLine library provides the following builtin parser
1537specializations, which are sufficient for most applications. It can, however,
1538also be extended to work with new data types and new ways of interpreting the
1539same data. See the <a href="#customparser">Writing a Custom Parser</a> for more
1540details on this type of library extension.</p>
Chris Lattner50761292002-08-06 19:36:06 +00001541
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001542<ul>
1543
1544<li><a name="genericparser">The <b>generic <tt>parser&lt;t&gt;</tt> parser</b></a>
Chris Lattner50761292002-08-06 19:36:06 +00001545can be used to map strings values to any data type, through the use of the <a
1546href="#cl::values">cl::values</a> property, which specifies the mapping
1547information. The most common use of this parser is for parsing enum values,
1548which allows you to use the CommandLine library for all of the error checking to
1549make sure that only valid enum values are specified (as opposed to accepting
1550arbitrary strings). Despite this, however, the generic parser class can be used
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001551for any data type.</li>
Chris Lattner50761292002-08-06 19:36:06 +00001552
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001553<li><a name="boolparser">The <b><tt>parser&lt;bool&gt;</tt> specialization</b></a>
Chris Lattner50761292002-08-06 19:36:06 +00001554is used to convert boolean strings to a boolean value. Currently accepted
1555strings are "<tt>true</tt>", "<tt>TRUE</tt>", "<tt>True</tt>", "<tt>1</tt>",
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001556"<tt>false</tt>", "<tt>FALSE</tt>", "<tt>False</tt>", and "<tt>0</tt>".</li>
Chris Lattner50761292002-08-06 19:36:06 +00001557
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001558<li><a name="stringparser">The <b><tt>parser&lt;string&gt;</tt>
1559specialization</b></a> simply stores the parsed string into the string value
1560specified. No conversion or modification of the data is performed.</li>
Chris Lattner50761292002-08-06 19:36:06 +00001561
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001562<li><a name="intparser">The <b><tt>parser&lt;int&gt;</tt> specialization</b></a>
Chris Lattner50761292002-08-06 19:36:06 +00001563uses the C <tt>strtol</tt> function to parse the string input. As such, it will
1564accept a decimal number (with an optional '+' or '-' prefix) which must start
1565with a non-zero digit. It accepts octal numbers, which are identified with a
1566'<tt>0</tt>' prefix digit, and hexadecimal numbers with a prefix of
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001567'<tt>0x</tt>' or '<tt>0X</tt>'.</li>
Chris Lattner50761292002-08-06 19:36:06 +00001568
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001569<li><a name="doubleparser">The <b><tt>parser&lt;double&gt;</tt></b></a> and
Chris Lattner50761292002-08-06 19:36:06 +00001570<b><tt>parser&lt;float&gt;</tt> specializations</b> use the standard C
1571<tt>strtod</tt> function to convert floating point strings into floating point
1572values. As such, a broad range of string formats is supported, including
1573exponential notation (ex: <tt>1.7e15</tt>) and properly supports locales.
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001574</li>
Chris Lattner50761292002-08-06 19:36:06 +00001575
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001576</ul>
Chris Lattnerba025252001-07-23 23:03:12 +00001577
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001578</div>
Chris Lattnerba025252001-07-23 23:03:12 +00001579
1580<!-- *********************************************************************** -->
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001581<div class="doc_section">
1582 <a name="extensionguide">Extension Guide</a>
1583</div>
Chris Lattnerba025252001-07-23 23:03:12 +00001584<!-- *********************************************************************** -->
1585
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001586<div class="doc_text">
1587
1588<p>Although the CommandLine library has a lot of functionality built into it
Chris Lattner74ea5a62002-08-07 18:27:04 +00001589already (as discussed previously), one of its true strengths lie in its
1590extensibility. This section discusses how the CommandLine library works under
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001591the covers and illustrates how to do some simple, common, extensions.</p>
Chris Lattner74ea5a62002-08-07 18:27:04 +00001592
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001593</div>
Chris Lattnerba025252001-07-23 23:03:12 +00001594
Chris Lattner50761292002-08-06 19:36:06 +00001595<!-- ======================================================================= -->
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001596<div class="doc_subsection">
1597 <a name="customparser">Writing a custom parser</a>
1598</div>
Chris Lattner50761292002-08-06 19:36:06 +00001599
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001600<div class="doc_text">
1601
1602<p>One of the simplest and most common extensions is the use of a custom parser.
Chris Lattner74ea5a62002-08-07 18:27:04 +00001603As <a href="#builtinparsers">discussed previously</a>, parsers are the portion
1604of the CommandLine library that turns string input from the user into a
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001605particular parsed data type, validating the input in the process.</p>
Chris Lattner74ea5a62002-08-07 18:27:04 +00001606
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001607<p>There are two ways to use a new parser:</p>
Chris Lattner74ea5a62002-08-07 18:27:04 +00001608
1609<ol>
Chris Lattner74ea5a62002-08-07 18:27:04 +00001610
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001611<li>
Chris Lattner74ea5a62002-08-07 18:27:04 +00001612
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001613<p>Specialize the <a href="#genericparser"><tt>cl::parser</tt></a> template for
1614your custom data type.<p>
Chris Lattner74ea5a62002-08-07 18:27:04 +00001615
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001616<p>This approach has the advantage that users of your custom data type will
1617automatically use your custom parser whenever they define an option with a value
1618type of your data type. The disadvantage of this approach is that it doesn't
1619work if your fundemental data type is something that is already supported.</p>
Chris Lattner74ea5a62002-08-07 18:27:04 +00001620
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001621</li>
Chris Lattner74ea5a62002-08-07 18:27:04 +00001622
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001623<li>
1624
1625<p>Write an independent class, using it explicitly from options that need
1626it.</p>
1627
1628<p>This approach works well in situations where you would line to parse an
1629option using special syntax for a not-very-special data-type. The drawback of
1630this approach is that users of your parser have to be aware that they are using
1631your parser, instead of the builtin ones.</p>
1632
1633</li>
1634
1635</ol>
1636
1637<p>To guide the discussion, we will discuss a custom parser that accepts file
Chris Lattner74ea5a62002-08-07 18:27:04 +00001638sizes, specified with an optional unit after the numeric size. For example, we
1639would like to parse "102kb", "41M", "1G" into the appropriate integer value. In
1640this case, the underlying data type we want to parse into is
1641'<tt>unsigned</tt>'. We choose approach #2 above because we don't want to make
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001642this the default for all <tt>unsigned</tt> options.</p>
Chris Lattner74ea5a62002-08-07 18:27:04 +00001643
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001644<p>To start out, we declare our new <tt>FileSizeParser</tt> class:</p>
Chris Lattner74ea5a62002-08-07 18:27:04 +00001645
1646<pre>
1647<b>struct</b> FileSizeParser : <b>public</b> cl::basic_parser&lt;<b>unsigned</b>&gt; {
1648 <i>// parse - Return true on error.</i>
1649 <b>bool</b> parse(cl::Option &amp;O, <b>const char</b> *ArgName, <b>const</b> std::string &amp;ArgValue,
1650 <b>unsigned</b> &amp;Val);
1651};
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001652</pre>
Chris Lattner74ea5a62002-08-07 18:27:04 +00001653
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001654<p>Our new class inherits from the <tt>cl::basic_parser</tt> template class to
1655fill in the default, boiler plate, code for us. We give it the data type that
1656we parse into (the last argument to the <tt>parse</tt> method so that clients of
Chris Lattner74ea5a62002-08-07 18:27:04 +00001657our custom parser know what object type to pass in to the parse method (here we
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001658declare that we parse into '<tt>unsigned</tt>' variables.</p>
Chris Lattner74ea5a62002-08-07 18:27:04 +00001659
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001660<p>For most purposes, the only method that must be implemented in a custom
1661parser is the <tt>parse</tt> method. The <tt>parse</tt> method is called
1662whenever the option is invoked, passing in the option itself, the option name,
1663the string to parse, and a reference to a return value. If the string to parse
1664is not well formed, the parser should output an error message and return true.
1665Otherwise it should return false and set '<tt>Val</tt>' to the parsed value. In
1666our example, we implement <tt>parse</tt> as:</p>
Chris Lattner74ea5a62002-08-07 18:27:04 +00001667
1668<pre>
1669<b>bool</b> FileSizeParser::parse(cl::Option &amp;O, <b>const char</b> *ArgName,
1670 <b>const</b> std::string &amp;Arg, <b>unsigned</b> &amp;Val) {
1671 <b>const char</b> *ArgStart = Arg.c_str();
1672 <b>char</b> *End;
1673
1674 <i>// Parse integer part, leaving 'End' pointing to the first non-integer char</i>
1675 Val = (unsigned)strtol(ArgStart, &amp;End, 0);
1676
1677 <b>while</b> (1) {
1678 <b>switch</b> (*End++) {
1679 <b>case</b> 0: <b>return</b> false; <i>// No error</i>
1680 <b>case</b> 'i': <i>// Ignore the 'i' in KiB if people use that</i>
1681 <b>case</b> 'b': <b>case</b> 'B': <i>// Ignore B suffix</i>
1682 <b>break</b>;
1683
1684 <b>case</b> 'g': <b>case</b> 'G': Val *= 1024*1024*1024; <b>break</b>;
1685 <b>case</b> 'm': <b>case</b> 'M': Val *= 1024*1024; <b>break</b>;
1686 <b>case</b> 'k': <b>case</b> 'K': Val *= 1024; <b>break</b>;
1687
1688 default:
1689 <i>// Print an error message if unrecognized character!</i>
1690 <b>return</b> O.error(": '" + Arg + "' value invalid for file size argument!");
1691 }
1692 }
1693}
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001694</pre>
Chris Lattner74ea5a62002-08-07 18:27:04 +00001695
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001696<p>This function implements a very simple parser for the kinds of strings we are
Chris Lattner74ea5a62002-08-07 18:27:04 +00001697interested in. Although it has some holes (it allows "<tt>123KKK</tt>" for
1698example), it is good enough for this example. Note that we use the option
1699itself to print out the error message (the <tt>error</tt> method always returns
1700true) in order to get a nice error message (shown below). Now that we have our
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001701parser class, we can use it like this:</p>
Chris Lattner74ea5a62002-08-07 18:27:04 +00001702
1703<pre>
1704<b>static</b> <a href="#cl::opt">cl::opt</a>&lt;<b>unsigned</b>, <b>false</b>, FileSizeParser&gt;
1705MFS(<i>"max-file-size"</i>, <a href="#cl::desc">cl::desc</a>(<i>"Maximum file size to accept"</i>),
1706 <a href="#cl::value_desc">cl::value_desc</a>("<i>size</i>"));
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001707</pre>
Chris Lattner74ea5a62002-08-07 18:27:04 +00001708
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001709<p>Which adds this to the output of our program:</p>
Chris Lattner74ea5a62002-08-07 18:27:04 +00001710
1711<pre>
1712OPTIONS:
1713 -help - display available options (--help-hidden for more)
1714 ...
1715 <b>-max-file-size=&lt;size&gt; - Maximum file size to accept</b>
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001716</pre>
Chris Lattner74ea5a62002-08-07 18:27:04 +00001717
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001718<p>And we can test that our parse works correctly now (the test program just
1719prints out the max-file-size argument value):</p>
Chris Lattner74ea5a62002-08-07 18:27:04 +00001720
1721<pre>
1722$ ./test
1723MFS: 0
1724$ ./test -max-file-size=123MB
1725MFS: 128974848
1726$ ./test -max-file-size=3G
1727MFS: 3221225472
1728$ ./test -max-file-size=dog
1729-max-file-size option: 'dog' value invalid for file size argument!
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001730</pre>
Chris Lattner74ea5a62002-08-07 18:27:04 +00001731
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001732<p>It looks like it works. The error message that we get is nice and helpful,
1733and we seem to accept reasonable file sizes. This wraps up the "custom parser"
1734tutorial.</p>
Chris Lattner74ea5a62002-08-07 18:27:04 +00001735
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001736</div>
Chris Lattner74ea5a62002-08-07 18:27:04 +00001737
1738<!-- ======================================================================= -->
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001739<div class="doc_subsection">
1740 <a name="explotingexternal">Exploiting external storage</a>
1741</div>
Chris Lattner74ea5a62002-08-07 18:27:04 +00001742
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001743<div class="doc_text">
Reid Spencercf2bc3d2004-08-30 05:56:51 +00001744 <p>Several of the LLVM libraries define static <tt>cl::opt</tt> instances that
1745 will automatically be included in any program that links with that library.
1746 This is a feature. However, sometimes it is necessary to know the value of the
1747 command line option outside of the library. In these cases the library does or
1748 should provide an external storage location that is accessible to users of the
1749 library. Examples of this include the <tt>llvm::DebugFlag</tt> exported by the
1750 <tt>lib/Support/Debug.cpp</tt> file and the <tt>llvm::TimePassesIsEnabled</tt>
1751 flag exported by the <tt>lib/VMCore/Pass.cpp</tt> file.</p>
Chris Lattner50761292002-08-06 19:36:06 +00001752
Reid Spencercf2bc3d2004-08-30 05:56:51 +00001753<p>TODO: complete this section</p>
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001754
1755</div>
Chris Lattner50761292002-08-06 19:36:06 +00001756
1757<!-- ======================================================================= -->
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001758<div class="doc_subsection">
1759 <a name="dynamicopts">Dynamically adding command line options</a>
1760</div>
Chris Lattner50761292002-08-06 19:36:06 +00001761
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001762<div class="doc_text">
Chris Lattner50761292002-08-06 19:36:06 +00001763
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001764<p>TODO: fill in this section</p>
Chris Lattnerba025252001-07-23 23:03:12 +00001765
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001766</div>
Chris Lattnerba025252001-07-23 23:03:12 +00001767
Chris Lattnerba025252001-07-23 23:03:12 +00001768<!-- *********************************************************************** -->
1769
1770<hr>
Misha Brukman374258e2004-05-12 18:42:35 +00001771<address>
1772 <a href="http://jigsaw.w3.org/css-validator/check/referer"><img
1773 src="http://jigsaw.w3.org/css-validator/images/vcss" alt="Valid CSS!"></a>
1774 <a href="http://validator.w3.org/check/referer"><img
1775 src="http://www.w3.org/Icons/valid-html401" alt="Valid HTML 4.01!"></a>
1776
1777 <a href="mailto:sabre@nondot.org">Chris Lattner</a><br>
1778 <a href="http://llvm.cs.uiuc.edu">LLVM Compiler Infrastructure</a><br>
Misha Brukman7120c832003-11-07 18:11:14 +00001779 Last modified: $Date$
Misha Brukman374258e2004-05-12 18:42:35 +00001780</address>
Misha Brukmanc86e8b12003-10-24 19:59:21 +00001781
1782</body>
1783</html>