blob: 0eda9c2c4d210bf6b9e5186483c87d4c78f66a8f [file] [log] [blame]
Misha Brukman9718e962003-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 Brukman9718e962003-10-24 19:59:21 +00005 <title>CommandLine 2.0 Library Manual</title>
Misha Brukman3c299112003-11-07 18:11:14 +00006 <link rel="stylesheet" href="llvm.css" type="text/css">
Misha Brukman9718e962003-10-24 19:59:21 +00007</head>
8<body>
Chris Lattner209c7f42001-07-23 23:03:12 +00009
Misha Brukman9718e962003-10-24 19:59:21 +000010<div class="doc_title">
11 CommandLine 2.0 Library Manual
12</div>
Chris Lattner209c7f42001-07-23 23:03:12 +000013
14<ol>
Misha Brukman9718e962003-10-24 19:59:21 +000015 <li><a href="#introduction">Introduction</a></li>
16
Chris Lattner209c7f42001-07-23 23:03:12 +000017 <li><a href="#quickstart">Quick Start Guide</a>
18 <ol>
Misha Brukman9718e962003-10-24 19:59:21 +000019 <li><a href="#bool">Boolean Arguments</a></li>
20 <li><a href="#alias">Argument Aliases</a></li>
Chris Lattnere76d4ab2002-08-06 19:36:06 +000021 <li><a href="#onealternative">Selecting an alternative from a
Misha Brukman9718e962003-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 Lattner209c7f42001-07-23 23:03:12 +000028 <li><a href="#referenceguide">Reference Guide</a>
Chris Lattnerae853632002-07-25 19:27:01 +000029 <ol>
Chris Lattnere76d4ab2002-08-06 19:36:06 +000030 <li><a href="#positional">Positional Arguments</a>
Chris Lattnerae853632002-07-25 19:27:01 +000031 <ul>
Misha Brukman9718e962003-10-24 19:59:21 +000032 <li><a href="#--">Specifying positional options with hyphens</a></li>
Reid Spencer2c8ab582004-08-13 20:19:14 +000033 <li><a href="#getPosition">Determining absolute position with
34 getPosition</a></li>
Chris Lattnere76d4ab2002-08-06 19:36:06 +000035 <li><a href="#cl::ConsumeAfter">The <tt>cl::ConsumeAfter</tt>
Misha Brukman9718e962003-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 Lattnere76d4ab2002-08-06 19:36:06 +000043 <li><a href="#modifiers">Option Modifiers</a>
Chris Lattnerae853632002-07-25 19:27:01 +000044 <ul>
Misha Brukman9718e962003-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 Lattnere76d4ab2002-08-06 19:36:06 +000049 <li><a href="#valrequired">Controlling whether or not a value must be
Misha Brukman9718e962003-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 Lattnerc1ae40c2002-08-07 18:27:04 +000055 <li><a href="#toplevel">Top-Level Classes and Functions</a>
Chris Lattnere76d4ab2002-08-06 19:36:06 +000056 <ul>
Misha Brukman9718e962003-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 Lattnere76d4ab2002-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 Brukman9718e962003-10-24 19:59:21 +000069 parser</a></li>
Chris Lattnere76d4ab2002-08-06 19:36:06 +000070 <li><a href="#boolparser">The <tt>parser&lt;bool&gt;</tt>
Misha Brukman9718e962003-10-24 19:59:21 +000071 specialization</a></li>
Chris Lattnere76d4ab2002-08-06 19:36:06 +000072 <li><a href="#stringparser">The <tt>parser&lt;string&gt;</tt>
Misha Brukman9718e962003-10-24 19:59:21 +000073 specialization</a></li>
Chris Lattnere76d4ab2002-08-06 19:36:06 +000074 <li><a href="#intparser">The <tt>parser&lt;int&gt;</tt>
Misha Brukman9718e962003-10-24 19:59:21 +000075 specialization</a></li>
Chris Lattnere76d4ab2002-08-06 19:36:06 +000076 <li><a href="#doubleparser">The <tt>parser&lt;double&gt;</tt> and
Misha Brukman9718e962003-10-24 19:59:21 +000077 <tt>parser&lt;float&gt;</tt> specializations</a></li>
78 </ul></li>
79 </ol></li>
Chris Lattner209c7f42001-07-23 23:03:12 +000080 <li><a href="#extensionguide">Extension Guide</a>
Chris Lattnerae853632002-07-25 19:27:01 +000081 <ol>
Misha Brukman9718e962003-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 Lattnere76d4ab2002-08-06 19:36:06 +000088
Chris Lattner7911ce22004-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 Brukman9718e962003-10-24 19:59:21 +000091</div>
Chris Lattner209c7f42001-07-23 23:03:12 +000092
93<!-- *********************************************************************** -->
Misha Brukman9718e962003-10-24 19:59:21 +000094<div class="doc_section">
Misha Brukman8becd712003-11-07 19:42:44 +000095 <a name="introduction">Introduction</a>
Misha Brukman9718e962003-10-24 19:59:21 +000096</div>
Chris Lattner209c7f42001-07-23 23:03:12 +000097<!-- *********************************************************************** -->
98
Misha Brukman9718e962003-10-24 19:59:21 +000099<div class="doc_text">
100
101<p>This document describes the CommandLine argument processing library. It will
Chris Lattnere76d4ab2002-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 Brukman9718e962003-10-24 19:59:21 +0000106changed</a>).</p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000107
Misha Brukman9718e962003-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 Lattner209c7f42001-07-23 23:03:12 +0000112
113<ol>
Chris Lattnerae853632002-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 Lattneredf351f2003-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 Brukman9718e962003-10-24 19:59:21 +0000119performance).</li>
Chris Lattner209c7f42001-07-23 23:03:12 +0000120
Chris Lattnerae853632002-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 Brukman9718e962003-10-24 19:59:21 +0000124error prone constructs, it also leads to dramatically cleaner source code.</li>
Chris Lattner209c7f42001-07-23 23:03:12 +0000125
Chris Lattnerae853632002-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 Brukman9718e962003-10-24 19:59:21 +0000128parser. This means that you don't have to write <b>any</b> boilerplate
129code.</li>
Chris Lattner209c7f42001-07-23 23:03:12 +0000130
Chris Lattnerae853632002-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 Lattnere76d4ab2002-08-06 19:36:06 +0000134the parser. This also makes supporting <a href="#dynamicopts">dynamically
Misha Brukman9718e962003-10-24 19:59:21 +0000135loaded options</a> trivial.</li>
Chris Lattner209c7f42001-07-23 23:03:12 +0000136
Chris Lattnerc1ae40c2002-08-07 18:27:04 +0000137<li>Cleaner: CommandLine supports enum and other types directly, meaning that
Chris Lattnere76d4ab2002-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 Brukman9718e962003-10-24 19:59:21 +0000140assigned a value that is not valid for your enum type.</li>
Chris Lattner209c7f42001-07-23 23:03:12 +0000141
Chris Lattnerae853632002-07-25 19:27:01 +0000142<li>Powerful: The CommandLine library supports many different types of
Chris Lattnere76d4ab2002-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 Brukman9718e962003-10-24 19:59:21 +0000147arguments</a>. This is possible because CommandLine is...</li>
Chris Lattner209c7f42001-07-23 23:03:12 +0000148
Chris Lattnerae853632002-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 Brukman9718e962003-10-24 19:59:21 +0000151you declare it. <a href="#customparser">Custom parsers</a> are no problem.</li>
Chris Lattner209c7f42001-07-23 23:03:12 +0000152
Chris Lattnerae853632002-07-25 19:27:01 +0000153<li>Labor Saving: The CommandLine library cuts down on the amount of grunt work
Chris Lattnere76d4ab2002-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 Brukman9718e962003-10-24 19:59:21 +0000156tool. Additionally, it does most of the basic correctness checking for
157you.</li>
Chris Lattnere76d4ab2002-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 Brukman9718e962003-10-24 19:59:21 +0000165href="#cl::ConsumeAfter">interpreter style options</a>.</li>
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000166
Chris Lattner209c7f42001-07-23 23:03:12 +0000167</ol>
168
Misha Brukman9718e962003-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 Lattnere76d4ab2002-08-06 19:36:06 +0000174
Misha Brukman9718e962003-10-24 19:59:21 +0000175</div>
Chris Lattner209c7f42001-07-23 23:03:12 +0000176
177<!-- *********************************************************************** -->
Misha Brukman9718e962003-10-24 19:59:21 +0000178<div class="doc_section">
179 <a name="quickstart">Quick Start Guide</a>
180</div>
Chris Lattner209c7f42001-07-23 23:03:12 +0000181<!-- *********************************************************************** -->
182
Misha Brukman9718e962003-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 Lattnerae853632002-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 Brukman9718e962003-10-24 19:59:21 +0000188can do.</p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000189
Misha Brukman9718e962003-10-24 19:59:21 +0000190<p>To start out, you need to include the CommandLine header file into your
191program:</p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000192
193<pre>
Chris Lattnerae853632002-07-25 19:27:01 +0000194 #include "Support/CommandLine.h"
Misha Brukman9718e962003-10-24 19:59:21 +0000195</pre>
Chris Lattner209c7f42001-07-23 23:03:12 +0000196
Misha Brukman9718e962003-10-24 19:59:21 +0000197<p>Additionally, you need to add this as the first line of your main
198program:</p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000199
200<pre>
201int main(int argc, char **argv) {
Chris Lattnerc1ae40c2002-08-07 18:27:04 +0000202 <a href="#cl::ParseCommandLineOptions">cl::ParseCommandLineOptions</a>(argc, argv);
Chris Lattner209c7f42001-07-23 23:03:12 +0000203 ...
204}
Misha Brukman9718e962003-10-24 19:59:21 +0000205</pre>
Chris Lattner209c7f42001-07-23 23:03:12 +0000206
Misha Brukman9718e962003-10-24 19:59:21 +0000207<p>... which actually parses the arguments and fills in the variable
208declarations.</p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000209
Misha Brukman9718e962003-10-24 19:59:21 +0000210<p>Now that you are ready to support command line arguments, we need to tell the
Chris Lattnerae853632002-07-25 19:27:01 +0000211system which ones we want, and what type of argument they are. The CommandLine
Chris Lattneredf351f2003-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 Brukman9718e962003-10-24 19:59:21 +0000218represented like this:</p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000219
Misha Brukmanc53aefb2004-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 Lattner209c7f42001-07-23 23:03:12 +0000224
Misha Brukman9718e962003-10-24 19:59:21 +0000225<p>This declares a global variable "<tt>OutputFilename</tt>" that is used to
Chris Lattneredf351f2003-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 Brukman9718e962003-10-24 19:59:21 +0000230that the data type that we are parsing is a string.</p>
Chris Lattnerae853632002-07-25 19:27:01 +0000231
Misha Brukman9718e962003-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 Lattner209c7f42001-07-23 23:03:12 +0000235
236<pre>
Chris Lattnerae853632002-07-25 19:27:01 +0000237USAGE: compiler [options]
Chris Lattner209c7f42001-07-23 23:03:12 +0000238
Chris Lattnerae853632002-07-25 19:27:01 +0000239OPTIONS:
240 -help - display available options (--help-hidden for more)
Chris Lattnerc1ae40c2002-08-07 18:27:04 +0000241 <b>-o &lt;filename&gt; - Specify output filename</b>
Chris Lattnerae853632002-07-25 19:27:01 +0000242</pre>
243
Misha Brukman9718e962003-10-24 19:59:21 +0000244<p>Because we specified that the command line option should parse using the
Chris Lattnerae853632002-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 Brukman9718e962003-10-24 19:59:21 +0000247example:</p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000248
249<pre>
250 ...
251 ofstream Output(OutputFilename.c_str());
252 if (Out.good()) ...
253 ...
Misha Brukman9718e962003-10-24 19:59:21 +0000254</pre>
Chris Lattner209c7f42001-07-23 23:03:12 +0000255
Misha Brukman9718e962003-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 Lattnere76d4ab2002-08-06 19:36:06 +0000259with helper functions like <a href="#cl::desc"><tt>cl::desc(...)</tt></a>, so
Chris Lattnerc1ae40c2002-08-07 18:27:04 +0000260there are no positional dependencies to remember. The available options are
Misha Brukman9718e962003-10-24 19:59:21 +0000261discussed in detail in the <a href="#referenceguide">Reference Guide</a>.</p>
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000262
Misha Brukman9718e962003-10-24 19:59:21 +0000263<p>Continuing the example, we would like to have our compiler take an input
Chris Lattnerae853632002-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 Lattnere76d4ab2002-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 Brukman9718e962003-10-24 19:59:21 +0000269in option form. We use this feature like this:</p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000270
271<pre>
Chris Lattnere76d4ab2002-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 Lattner209c7f42001-07-23 23:03:12 +0000273</pre>
274
Misha Brukman9718e962003-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 Lattnere76d4ab2002-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 Lattnerae853632002-07-25 19:27:01 +0000281Command line options default to being optional, so if we would like to require
Chris Lattnere76d4ab2002-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 Brukman9718e962003-10-24 19:59:21 +0000284<tt><a href="#cl::init">cl::init</a></tt> modifier, like this:</p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000285
286<pre>
Chris Lattnere76d4ab2002-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 Lattner209c7f42001-07-23 23:03:12 +0000288</pre>
289
Misha Brukman9718e962003-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 Lattnerae853632002-07-25 19:27:01 +0000292
293<pre>
Chris Lattnere76d4ab2002-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 Lattnerae853632002-07-25 19:27:01 +0000295</pre>
296
Misha Brukman9718e962003-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 Lattnere76d4ab2002-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 Brukman9718e962003-10-24 19:59:21 +0000303extended to:</p>
Chris Lattnerae853632002-07-25 19:27:01 +0000304
305<pre>
Chris Lattnerc1ae40c2002-08-07 18:27:04 +0000306USAGE: compiler [options] <b>&lt;input file&gt;</b>
Chris Lattnerae853632002-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 Brukman9718e962003-10-24 19:59:21 +0000313<p>... indicating that an input filename is expected.</p>
Chris Lattnerae853632002-07-25 19:27:01 +0000314
Misha Brukman9718e962003-10-24 19:59:21 +0000315</div>
Chris Lattnerc1ae40c2002-08-07 18:27:04 +0000316
Chris Lattner209c7f42001-07-23 23:03:12 +0000317<!-- ======================================================================= -->
Misha Brukman9718e962003-10-24 19:59:21 +0000318<div class="doc_subsection">
319 <a name="bool">Boolean Arguments</a>
320</div>
Chris Lattner209c7f42001-07-23 23:03:12 +0000321
Misha Brukman9718e962003-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 Lattnerae853632002-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 Brukman9718e962003-10-24 19:59:21 +0000328of boolean type like this:</p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000329
330<pre>
Chris Lattnere76d4ab2002-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 Brukman9718e962003-10-24 19:59:21 +0000334</pre>
Chris Lattner209c7f42001-07-23 23:03:12 +0000335
Misha Brukman9718e962003-10-24 19:59:21 +0000336<p>This does what you would expect: it declares three boolean variables
Chris Lattnerae853632002-07-25 19:27:01 +0000337("<tt>Force</tt>", "<tt>Quiet</tt>", and "<tt>Quiet2</tt>") to recognize these
Chris Lattnere76d4ab2002-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 Brukman9718e962003-10-24 19:59:21 +0000341shown in the "<tt>--help-hidden</tt>" output).</p>
Chris Lattnerae853632002-07-25 19:27:01 +0000342
Misha Brukman9718e962003-10-24 19:59:21 +0000343<p>The CommandLine library uses a <a href="#builtinparsers">different parser</a>
Chris Lattnerc1ae40c2002-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 Brukman9718e962003-10-24 19:59:21 +0000350following inputs:</p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000351
352<pre>
Chris Lattnerae853632002-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 Brukman9718e962003-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 Lattnerc1ae40c2002-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 Brukman9718e962003-10-24 19:59:21 +0000364library calls to parse the string value into the specified data type.</p>
Chris Lattnerae853632002-07-25 19:27:01 +0000365
Misha Brukman9718e962003-10-24 19:59:21 +0000366<p>With the declarations above, "<tt>compiler --help</tt>" emits this:</p>
Chris Lattnerae853632002-07-25 19:27:01 +0000367
368<pre>
369USAGE: compiler [options] &lt;input file&gt;
Chris Lattner209c7f42001-07-23 23:03:12 +0000370
371OPTIONS:
Chris Lattnerc1ae40c2002-08-07 18:27:04 +0000372 <b>-f - Overwrite output files</b>
Chris Lattner209c7f42001-07-23 23:03:12 +0000373 -o - Override output filename
Chris Lattnerc1ae40c2002-08-07 18:27:04 +0000374 <b>-quiet - Don't print informational messages</b>
Chris Lattner209c7f42001-07-23 23:03:12 +0000375 -help - display available options (--help-hidden for more)
Misha Brukman9718e962003-10-24 19:59:21 +0000376</pre>
Chris Lattner209c7f42001-07-23 23:03:12 +0000377
Misha Brukman9718e962003-10-24 19:59:21 +0000378<p>and "<tt>opt --help-hidden</tt>" prints this:</p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000379
380<pre>
Chris Lattnerc1ae40c2002-08-07 18:27:04 +0000381USAGE: compiler [options] &lt;input file&gt;
Chris Lattner209c7f42001-07-23 23:03:12 +0000382
383OPTIONS:
384 -f - Overwrite output files
385 -o - Override output filename
Chris Lattnerc1ae40c2002-08-07 18:27:04 +0000386 <b>-q - Don't print informational messages</b>
Chris Lattner209c7f42001-07-23 23:03:12 +0000387 -quiet - Don't print informational messages
388 -help - display available options (--help-hidden for more)
Misha Brukman9718e962003-10-24 19:59:21 +0000389</pre>
Chris Lattner209c7f42001-07-23 23:03:12 +0000390
Misha Brukman9718e962003-10-24 19:59:21 +0000391<p>This brief example has shown you how to use the '<tt><a
Chris Lattnere76d4ab2002-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 Brukman9718e962003-10-24 19:59:21 +0000395and <a href="#list">lists</a> of options.</p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000396
Misha Brukman9718e962003-10-24 19:59:21 +0000397</div>
Chris Lattner209c7f42001-07-23 23:03:12 +0000398
399<!-- ======================================================================= -->
Misha Brukman9718e962003-10-24 19:59:21 +0000400<div class="doc_subsection">
401 <a name="alias">Argument Aliases</a>
402</div>
Chris Lattner209c7f42001-07-23 23:03:12 +0000403
Misha Brukman9718e962003-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 Lattner209c7f42001-07-23 23:03:12 +0000408
409<pre>
410...
411 if (!Quiet &amp;&amp; !Quiet2) printInformationalMessage(...);
412...
Misha Brukman9718e962003-10-24 19:59:21 +0000413</pre>
Chris Lattner209c7f42001-07-23 23:03:12 +0000414
Misha Brukman9718e962003-10-24 19:59:21 +0000415<p>... which is a real pain! Instead of defining two values for the same
Chris Lattnere76d4ab2002-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 Lattnerae853632002-07-25 19:27:01 +0000417option an <b>alias</b> for the "<tt>-quiet</tt>" option, instead of providing
Misha Brukman9718e962003-10-24 19:59:21 +0000418a value itself:</p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000419
420<pre>
Chris Lattnere76d4ab2002-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 Brukman9718e962003-10-24 19:59:21 +0000424</pre>
Chris Lattner209c7f42001-07-23 23:03:12 +0000425
Misha Brukman9718e962003-10-24 19:59:21 +0000426<p>The third line (which is the only one we modified from above) defines a
Chris Lattnerae853632002-07-25 19:27:01 +0000427"<tt>-q</tt> alias that updates the "<tt>Quiet</tt>" variable (as specified by
Chris Lattnere76d4ab2002-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 Brukman9718e962003-10-24 19:59:21 +0000433output</tt>).</p>
Chris Lattnerae853632002-07-25 19:27:01 +0000434
Misha Brukman9718e962003-10-24 19:59:21 +0000435<p>Now the application code can simply use:</p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000436
437<pre>
438...
439 if (!Quiet) printInformationalMessage(...);
440...
Misha Brukman9718e962003-10-24 19:59:21 +0000441</pre>
Chris Lattner209c7f42001-07-23 23:03:12 +0000442
Misha Brukman9718e962003-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 Lattnerae853632002-07-25 19:27:01 +0000446
Misha Brukman9718e962003-10-24 19:59:21 +0000447</div>
Chris Lattner209c7f42001-07-23 23:03:12 +0000448
449<!-- ======================================================================= -->
Misha Brukman9718e962003-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 Lattner209c7f42001-07-23 23:03:12 +0000454
Misha Brukman9718e962003-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 Lattnerae853632002-07-25 19:27:01 +0000458<tt>std::string</tt>, <tt>bool</tt> and <tt>int</tt>, but how does it handle
Misha Brukman9718e962003-10-24 19:59:21 +0000459things it doesn't know about, like enums or '<tt>int*</tt>'s?</p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000460
Misha Brukman9718e962003-10-24 19:59:21 +0000461<p>The answer is that it uses a table driven generic parser (unless you specify
Chris Lattnerae853632002-07-25 19:27:01 +0000462your own parser, as described in the <a href="#extensionguide">Extension
Reid Spencerd8473372004-08-10 16:38:18 +0000463Guide</a>). This parser maps literal strings to whatever type is required, and
Misha Brukman9718e962003-10-24 19:59:21 +0000464requires you to tell it what this mapping should be.</p>
Chris Lattnerae853632002-07-25 19:27:01 +0000465
Reid Spencerd8473372004-08-10 16:38:18 +0000466<p>Lets say that we would like to add four optimization levels to our
Misha Brukman9718e962003-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 Lattner209c7f42001-07-23 23:03:12 +0000470
471<ol>
Chris Lattnerae853632002-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 Brukman9718e962003-10-24 19:59:21 +0000474erroneous input for us.</li>
Chris Lattnerae853632002-07-25 19:27:01 +0000475
Misha Brukman9718e962003-10-24 19:59:21 +0000476<li>We would have to test 4 different variables to see which ones are set.</li>
Chris Lattnerae853632002-07-25 19:27:01 +0000477
478<li>This doesn't map to the numeric levels that we want... so we cannot easily
Misha Brukman9718e962003-10-24 19:59:21 +0000479see if some level &gt;= "<tt>-O1</tt>" is enabled.</li>
Chris Lattnerae853632002-07-25 19:27:01 +0000480
Misha Brukman9718e962003-10-24 19:59:21 +0000481</ol>
Chris Lattner209c7f42001-07-23 23:03:12 +0000482
Misha Brukman9718e962003-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 Lattner209c7f42001-07-23 23:03:12 +0000486
487<pre>
488enum OptLevel {
489 g, O1, O2, O3
490};
491
Chris Lattnere76d4ab2002-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 Lattnerae853632002-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 Lattnerb406ead2004-07-16 00:10:54 +0000498 clEnumValEnd));
Chris Lattner209c7f42001-07-23 23:03:12 +0000499
500...
Chris Lattnerae853632002-07-25 19:27:01 +0000501 if (OptimizationLevel &gt;= O2) doPartialRedundancyElimination(...);
Chris Lattner209c7f42001-07-23 23:03:12 +0000502...
Misha Brukman9718e962003-10-24 19:59:21 +0000503</pre>
Chris Lattner209c7f42001-07-23 23:03:12 +0000504
Misha Brukman9718e962003-10-24 19:59:21 +0000505<p>This declaration defines a variable "<tt>OptimizationLevel</tt>" of the
Chris Lattnerae853632002-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 Lattnerb406ead2004-07-16 00:10:54 +0000508terminated with the "<tt>clEnumValEnd</tt>" argument!). The CommandLine
509library enforces
Chris Lattnerae853632002-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 Lattnere76d4ab2002-08-06 19:36:06 +0000512command line arguments matched the enum values. With this option added, our
Misha Brukman9718e962003-10-24 19:59:21 +0000513help output now is:</p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000514
Chris Lattnerae853632002-07-25 19:27:01 +0000515<pre>
516USAGE: compiler [options] &lt;input file&gt;
Chris Lattner209c7f42001-07-23 23:03:12 +0000517
Chris Lattnerae853632002-07-25 19:27:01 +0000518OPTIONS:
Chris Lattnerc1ae40c2002-08-07 18:27:04 +0000519 <b>Choose optimization level:
Chris Lattnerae853632002-07-25 19:27:01 +0000520 -g - No optimizations, enable debugging
521 -O1 - Enable trivial optimizations
522 -O2 - Enable default optimizations
Chris Lattnerc1ae40c2002-08-07 18:27:04 +0000523 -O3 - Enable expensive optimizations</b>
Chris Lattnerae853632002-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 Lattner209c7f42001-07-23 23:03:12 +0000529
Misha Brukman9718e962003-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 Lattner209c7f42001-07-23 23:03:12 +0000534
535<pre>
536enum OptLevel {
537 Debug, O1, O2, O3
538};
539
Chris Lattnere76d4ab2002-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 Lattnerae853632002-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 Lattnerb406ead2004-07-16 00:10:54 +0000546 clEnumValEnd));
Chris Lattner209c7f42001-07-23 23:03:12 +0000547
548...
549 if (OptimizationLevel == Debug) outputDebugInfo(...);
550...
Misha Brukman9718e962003-10-24 19:59:21 +0000551</pre>
Chris Lattner209c7f42001-07-23 23:03:12 +0000552
Misha Brukman9718e962003-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 Lattnerae853632002-07-25 19:27:01 +0000557
Misha Brukman9718e962003-10-24 19:59:21 +0000558</div>
Chris Lattner209c7f42001-07-23 23:03:12 +0000559
560<!-- ======================================================================= -->
Misha Brukman9718e962003-10-24 19:59:21 +0000561<div class="doc_subsection">
562 <a name="namedalternatives">Named Alternatives</a>
563</div>
Chris Lattner209c7f42001-07-23 23:03:12 +0000564
Misha Brukman9718e962003-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 Lattnerae853632002-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 Brukman9718e962003-10-24 19:59:21 +0000574case, the code looks like this:</p>
Chris Lattner209c7f42001-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 Lattnerdc844fa2003-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 Lattnere76d4ab2002-08-06 19:36:06 +0000583 <a href="#cl::values">cl::values</a>(
Chris Lattnerae853632002-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 Lattnerb406ead2004-07-16 00:10:54 +0000587 clEnumValEnd));
Chris Lattner209c7f42001-07-23 23:03:12 +0000588</pre>
589
Misha Brukman9718e962003-10-24 19:59:21 +0000590<p>This definition defines an enumerated command line variable of type "<tt>enum
Chris Lattnerae853632002-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 Brukman9718e962003-10-24 19:59:21 +0000593the "<tt>--help</tt>" option:</p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000594
595<pre>
Chris Lattnerae853632002-07-25 19:27:01 +0000596USAGE: compiler [options] &lt;input file&gt;
597
Chris Lattner209c7f42001-07-23 23:03:12 +0000598OPTIONS:
Chris Lattnerae853632002-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 Lattnerc1ae40c2002-08-07 18:27:04 +0000604 <b>-debug_level - Set the debugging level:
Chris Lattnerae853632002-07-25 19:27:01 +0000605 =none - disable debug information
606 =quick - enable quick debug information
Chris Lattnerc1ae40c2002-08-07 18:27:04 +0000607 =detailed - enable detailed debug information</b>
Chris Lattnerae853632002-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 Brukman9718e962003-10-24 19:59:21 +0000612</pre>
Chris Lattner209c7f42001-07-23 23:03:12 +0000613
Misha Brukman9718e962003-10-24 19:59:21 +0000614<p>Again, the only structural difference between the debug level declaration and
Chris Lattnerae853632002-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 Brukman9718e962003-10-24 19:59:21 +0000618that you can choose the form most appropriate for your application.</p>
Chris Lattnerae853632002-07-25 19:27:01 +0000619
Misha Brukman9718e962003-10-24 19:59:21 +0000620</div>
Chris Lattner209c7f42001-07-23 23:03:12 +0000621
622<!-- ======================================================================= -->
Misha Brukman9718e962003-10-24 19:59:21 +0000623<div class="doc_subsection">
624 <a name="list">Parsing a list of options</a>
625</div>
Chris Lattner209c7f42001-07-23 23:03:12 +0000626
Misha Brukman9718e962003-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 Lattnerae853632002-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 Lattnere76d4ab2002-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 Brukman9718e962003-10-24 19:59:21 +0000636would like to perform:</p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000637
638<pre>
639enum Opts {
Chris Lattnerae853632002-07-25 19:27:01 +0000640 // 'inline' is a C++ keyword, so name it 'inlining'
Chris Lattner209c7f42001-07-23 23:03:12 +0000641 dce, constprop, inlining, strip
Chris Lattnerae853632002-07-25 19:27:01 +0000642};
Misha Brukman9718e962003-10-24 19:59:21 +0000643</pre>
Chris Lattner209c7f42001-07-23 23:03:12 +0000644
Misha Brukman9718e962003-10-24 19:59:21 +0000645<p>Then define your "<tt><a href="#cl::list">cl::list</a></tt>" variable:</p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000646
647<pre>
Chris Lattnere76d4ab2002-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 Lattnerae853632002-07-25 19:27:01 +0000650 clEnumVal(dce , "<i>Dead Code Elimination</i>"),
Misha Brukman82c89b92003-05-20 21:01:22 +0000651 clEnumVal(constprop , "<i>Constant Propagation</i>"),
Chris Lattnerae853632002-07-25 19:27:01 +0000652 clEnumValN(inlining, "<i>inline</i>", "<i>Procedure Integration</i>"),
653 clEnumVal(strip , "<i>Strip Symbols</i>"),
Chris Lattnerb406ead2004-07-16 00:10:54 +0000654 clEnumValEnd));
Misha Brukman9718e962003-10-24 19:59:21 +0000655</pre>
Chris Lattner209c7f42001-07-23 23:03:12 +0000656
Misha Brukman9718e962003-10-24 19:59:21 +0000657<p>This defines a variable that is conceptually of the type
Chris Lattnerae853632002-07-25 19:27:01 +0000658"<tt>std::vector&lt;enum Opts&gt;</tt>". Thus, you can access it with standard
Misha Brukman9718e962003-10-24 19:59:21 +0000659vector methods:</p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000660
661<pre>
Chris Lattnerae853632002-07-25 19:27:01 +0000662 for (unsigned i = 0; i != OptimizationList.size(); ++i)
Chris Lattner209c7f42001-07-23 23:03:12 +0000663 switch (OptimizationList[i])
664 ...
665</pre>
666
Misha Brukman9718e962003-10-24 19:59:21 +0000667<p>... to iterate through the list of options specified.</p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000668
Misha Brukman9718e962003-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 Lattner3e5fe172002-04-13 18:35:59 +0000676
677<pre>
678...
Chris Lattnere76d4ab2002-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 Lattner3e5fe172002-04-13 18:35:59 +0000680...
Misha Brukman9718e962003-10-24 19:59:21 +0000681</pre>
Chris Lattner3e5fe172002-04-13 18:35:59 +0000682
Misha Brukman9718e962003-10-24 19:59:21 +0000683<p>This variable works just like a "<tt>vector&lt;string&gt;</tt>" object. As
Chris Lattnerae853632002-07-25 19:27:01 +0000684such, accessing the list is simple, just like above. In this example, we used
Chris Lattnere76d4ab2002-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 Brukman9718e962003-10-24 19:59:21 +0000688checking we have to do.</p>
Chris Lattner3e5fe172002-04-13 18:35:59 +0000689
Misha Brukman9718e962003-10-24 19:59:21 +0000690</div>
Chris Lattner3e5fe172002-04-13 18:35:59 +0000691
Chris Lattnerc1ae40c2002-08-07 18:27:04 +0000692<!-- ======================================================================= -->
Misha Brukman9718e962003-10-24 19:59:21 +0000693<div class="doc_subsection">
694 <a name="description">Adding freeform text to help output</a>
695</div>
Chris Lattnerc1ae40c2002-08-07 18:27:04 +0000696
Misha Brukman9718e962003-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 Lattnerc1ae40c2002-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 Brukman9718e962003-10-24 19:59:21 +0000708that you want. For example:</p>
Chris Lattnerc1ae40c2002-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 Brukman9718e962003-10-24 19:59:21 +0000716</pre>
Chris Lattnerc1ae40c2002-08-07 18:27:04 +0000717
Misha Brukman9718e962003-10-24 19:59:21 +0000718<p>Would yield the help output:</p>
Chris Lattnerc1ae40c2002-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 Brukman9718e962003-10-24 19:59:21 +0000731</pre>
Chris Lattnerc1ae40c2002-08-07 18:27:04 +0000732
Misha Brukman9718e962003-10-24 19:59:21 +0000733</div>
Chris Lattnerc1ae40c2002-08-07 18:27:04 +0000734
735
Chris Lattner209c7f42001-07-23 23:03:12 +0000736<!-- *********************************************************************** -->
Misha Brukman9718e962003-10-24 19:59:21 +0000737<div class="doc_section">
738 <a name="referenceguide">Reference Guide</a>
739</div>
Chris Lattner209c7f42001-07-23 23:03:12 +0000740<!-- *********************************************************************** -->
741
Misha Brukman9718e962003-10-24 19:59:21 +0000742<div class="doc_text">
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000743
Misha Brukman9718e962003-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 Lattnere76d4ab2002-08-06 19:36:06 +0000750
751<!-- ======================================================================= -->
Misha Brukman9718e962003-10-24 19:59:21 +0000752<div class="doc_subsection">
753 <a name="positional">Positional Arguments</a>
754</div>
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000755
Misha Brukman9718e962003-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 Lattnere76d4ab2002-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 Brukman9718e962003-10-24 19:59:21 +0000763Using the CommandLine library, this would be specified as:</p>
Chris Lattnere76d4ab2002-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 Brukman9718e962003-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 Lattnere76d4ab2002-08-06 19:36:06 +0000772
773<pre>
Chris Lattnerc1ae40c2002-08-07 18:27:04 +0000774USAGE: spiffygrep [options] <b>&lt;regular expression&gt; &lt;input file&gt;</b>
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000775
776OPTIONS:
777 -help - display available options (--help-hidden for more)
778</pre>
779
Misha Brukman9718e962003-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 Lattnere76d4ab2002-08-06 19:36:06 +0000782
Misha Brukman9718e962003-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 Spencer2c8ab582004-08-13 20:19:14 +0000785.cpp file, but will not have an ordering defined if the positional arguments
Misha Brukman9718e962003-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 Lattnere76d4ab2002-08-06 19:36:06 +0000788
Misha Brukman9718e962003-10-24 19:59:21 +0000789</div>
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000790
791
792<!-- _______________________________________________________________________ -->
Misha Brukman9718e962003-10-24 19:59:21 +0000793<div class="doc_subsubsection">
794 <a name="--">Specifying positional options with hyphens</a>
795</div>
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000796
Misha Brukman9718e962003-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 Lattnere76d4ab2002-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 Brukman9718e962003-10-24 19:59:21 +0000803Note that the system <tt>grep</tt> has the same problem:</p>
Chris Lattnere76d4ab2002-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 Brukman9718e962003-10-24 19:59:21 +0000814</pre>
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000815
Misha Brukman9718e962003-10-24 19:59:21 +0000816<p>The solution for this problem is the same for both your tool and the system
Chris Lattnere76d4ab2002-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 Brukman9718e962003-10-24 19:59:21 +0000820can use it like this:</p>
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000821
822<pre>
823 $ spiffygrep -- -foo test.txt
824 ...output...
Misha Brukman9718e962003-10-24 19:59:21 +0000825</pre>
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000826
Misha Brukman9718e962003-10-24 19:59:21 +0000827</div>
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000828
829<!-- _______________________________________________________________________ -->
Misha Brukman9718e962003-10-24 19:59:21 +0000830<div class="doc_subsubsection">
Reid Spencer2c8ab582004-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 }
878 }</tt></pre></p>
879 <p>Note that, for compatibility reasons, the <tt>cl::opt</tt> also supports an
880 <tt>unsigned getPosition()</tt> option that will provide the absolute position
881 of that option. You can apply the same approach as above with a
882 <tt>cl::opt</tt> and a <tt>cl::list</tt> option as you can with two lists.</p>
883</div>
884
885<!-- _______________________________________________________________________ -->
886<div class="doc_subsubsection">
Misha Brukman9718e962003-10-24 19:59:21 +0000887 <a name="cl::ConsumeAfter">The <tt>cl::ConsumeAfter</tt> modifier</a>
888</div>
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000889
Misha Brukman9718e962003-10-24 19:59:21 +0000890<div class="doc_text">
891
892<p>The <tt>cl::ConsumeAfter</tt> <a href="#formatting">formatting option</a> is
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000893used to construct programs that use "interpreter style" option processing. With
894this style of option processing, all arguments specified after the last
895positional argument are treated as special interpreter arguments that are not
Misha Brukman9718e962003-10-24 19:59:21 +0000896interpreted by the command line argument.</p>
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000897
Misha Brukman9718e962003-10-24 19:59:21 +0000898<p>As a concrete example, lets say we are developing a replacement for the
899standard Unix Bourne shell (<tt>/bin/sh</tt>). To run <tt>/bin/sh</tt>, first
900you specify options to the shell itself (like <tt>-x</tt> which turns on trace
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000901output), then you specify the name of the script to run, then you specify
902arguments to the script. These arguments to the script are parsed by the bourne
903shell command line option processor, but are not interpreted as options to the
Misha Brukman9718e962003-10-24 19:59:21 +0000904shell itself. Using the CommandLine library, we would specify this as:</p>
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000905
906<pre>
907<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>("-"));
908<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>"));
909<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 Brukman9718e962003-10-24 19:59:21 +0000910</pre>
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000911
Misha Brukman9718e962003-10-24 19:59:21 +0000912<p>which automatically provides the help output:</p>
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000913
914<pre>
Chris Lattnerc1ae40c2002-08-07 18:27:04 +0000915USAGE: spiffysh [options] <b>&lt;input script&gt; &lt;program arguments&gt;...</b>
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000916
917OPTIONS:
918 -help - display available options (--help-hidden for more)
Chris Lattnerc1ae40c2002-08-07 18:27:04 +0000919 <b>-x - Enable trace output</b>
Misha Brukman9718e962003-10-24 19:59:21 +0000920</pre>
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000921
Misha Brukman9718e962003-10-24 19:59:21 +0000922<p>At runtime, if we run our new shell replacement as '<tt>spiffysh -x test.sh
923-a -x -y bar</tt>', the <tt>Trace</tt> variable will be set to true, the
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000924<tt>Script</tt> variable will be set to "<tt>test.sh</tt>", and the
Misha Brukman9718e962003-10-24 19:59:21 +0000925<tt>Argv</tt> list will contain <tt>["-a", "-x", "-y", "bar"]</tt>, because they
926were specified after the last positional argument (which is the script
927name).</p>
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000928
Misha Brukman9718e962003-10-24 19:59:21 +0000929<p>There are several limitations to when <tt>cl::ConsumeAfter</tt> options can
930be specified. For example, only one <tt>cl::ConsumeAfter</tt> can be specified
931per program, there must be at least one <a href="#positional">positional
Chris Lattnerbe801bf2004-05-06 22:03:59 +0000932argument</a> specified, there must not be any <a href="#cl::list">cl::list</a>
933positional arguments, and the <tt>cl::ConsumeAfter</tt> option should be a <a
Misha Brukman9718e962003-10-24 19:59:21 +0000934href="#cl::list">cl::list</a> option.</p>
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000935
Misha Brukman9718e962003-10-24 19:59:21 +0000936</div>
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000937
938<!-- ======================================================================= -->
Chris Lattnerbe801bf2004-05-06 22:03:59 +0000939<div class="doc_subsection">
Misha Brukman9718e962003-10-24 19:59:21 +0000940 <a name="storage">Internal vs External Storage</a>
941</div>
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000942
Misha Brukman9718e962003-10-24 19:59:21 +0000943<div class="doc_text">
944
945<p>By default, all command line options automatically hold the value that they
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000946parse from the command line. This is very convenient in the common case,
947especially when combined with the ability to define command line options in the
Misha Brukman9718e962003-10-24 19:59:21 +0000948files that use them. This is called the internal storage model.</p>
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000949
Misha Brukman9718e962003-10-24 19:59:21 +0000950<p>Sometimes, however, it is nice to separate the command line option processing
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000951code from the storage of the value parsed. For example, lets say that we have a
952'<tt>-debug</tt>' option that we would like to use to enable debug information
953across the entire body of our program. In this case, the boolean value
954controlling the debug code should be globally accessable (in a header file, for
955example) yet the command line option processing code should not be exposed to
956all of these clients (requiring lots of .cpp files to #include
Misha Brukman9718e962003-10-24 19:59:21 +0000957<tt>CommandLine.h</tt>).</p>
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000958
Misha Brukman9718e962003-10-24 19:59:21 +0000959<p>To do this, set up your .h file with your option, like this for example:</p>
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000960
961<pre>
962<i>// DebugFlag.h - Get access to the '-debug' command line option
963//
964
965// DebugFlag - This boolean is set to true if the '-debug' command line option
966// is specified. This should probably not be referenced directly, instead, use
967// the DEBUG macro below.
968//</i>
969extern bool DebugFlag;
970
971<i>// DEBUG macro - This macro should be used by code to emit debug information.
972// In the '-debug' option is specified on the command line, and if this is a
973// debug build, then the code specified as the option to the macro will be
974// executed. Otherwise it will not be. Example:
975//
976// DEBUG(cerr << "Bitset contains: " << Bitset << "\n");
977//</i>
Misha Brukman9718e962003-10-24 19:59:21 +0000978<span class="doc_red">#ifdef NDEBUG
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000979#define DEBUG(X)
980#else
Misha Brukman9718e962003-10-24 19:59:21 +0000981#define DEBUG(X)</span> \
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000982 do { if (DebugFlag) { X; } } while (0)
Misha Brukman9718e962003-10-24 19:59:21 +0000983<span class="doc_red">#endif</span>
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000984</pre>
985
Misha Brukman9718e962003-10-24 19:59:21 +0000986<p>This allows clients to blissfully use the <tt>DEBUG()</tt> macro, or the
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000987<tt>DebugFlag</tt> explicitly if they want to. Now we just need to be able to
988set the <tt>DebugFlag</tt> boolean when the option is set. To do this, we pass
989an additial argument to our command line argument processor, and we specify
Misha Brukman9718e962003-10-24 19:59:21 +0000990where to fill in with the <a href="#cl::location">cl::location</a>
991attribute:</p>
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000992
993<pre>
994bool DebugFlag; <i>// the actual value</i>
Chris Lattner589a4cc2003-08-01 21:30:37 +0000995static <a href="#cl::opt">cl::opt</a>&lt;bool, true&gt; <i>// The parser</i>
Misha Brukmanc53aefb2004-05-12 18:42:35 +0000996Debug("<i>debug</i>", <a href="#cl::desc">cl::desc</a>("<i>Enable debug output</i>"), <a href="#cl::Hidden">cl::Hidden</a>,
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000997 <a href="#cl::location">cl::location</a>(DebugFlag));
998</pre>
999
Misha Brukman9718e962003-10-24 19:59:21 +00001000<p>In the above example, we specify "<tt>true</tt>" as the second argument to
1001the <a href="#cl::opt">cl::opt</a> template, indicating that the template should
1002not maintain a copy of the value itself. In addition to this, we specify the <a
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001003href="#cl::location">cl::location</a> attribute, so that <tt>DebugFlag</tt> is
Misha Brukman9718e962003-10-24 19:59:21 +00001004automatically set.</p>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001005
Misha Brukman9718e962003-10-24 19:59:21 +00001006</div>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001007
1008<!-- ======================================================================= -->
Misha Brukman9718e962003-10-24 19:59:21 +00001009<div class="doc_subsection">
1010 <a name="attributes">Option Attributes</a>
1011</div>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001012
Misha Brukman9718e962003-10-24 19:59:21 +00001013<div class="doc_text">
1014
1015<p>This section describes the basic attributes that you can specify on
1016options.</p>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001017
1018<ul>
1019
1020<li>The option name attribute (which is required for all options, except <a
1021href="#positional">positional options</a>) specifies what the option name is.
Misha Brukman9718e962003-10-24 19:59:21 +00001022This option is specified in simple double quotes:
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001023
Chris Lattnerc1ae40c2002-08-07 18:27:04 +00001024<pre>
1025<a href="#cl::opt">cl::opt</a>&lt;<b>bool</b>&gt; Quiet("<i>quiet</i>");
Misha Brukman9718e962003-10-24 19:59:21 +00001026</pre>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001027
Misha Brukman9718e962003-10-24 19:59:21 +00001028</li>
1029
1030<li><a name="cl::desc">The <b><tt>cl::desc</tt></b></a> attribute specifies a
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001031description for the option to be shown in the <tt>--help</tt> output for the
Misha Brukman9718e962003-10-24 19:59:21 +00001032program.</li>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001033
Misha Brukman9718e962003-10-24 19:59:21 +00001034<li><a name="cl::value_desc">The <b><tt>cl::value_desc</tt></b></a> attribute
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001035specifies a string that can be used to fine tune the <tt>--help</tt> output for
1036a command line option. Look <a href="#value_desc_example">here</a> for an
Misha Brukman9718e962003-10-24 19:59:21 +00001037example.</li>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001038
Misha Brukman9718e962003-10-24 19:59:21 +00001039<li><a name="cl::init">The <b><tt>cl::init</tt></b></a> attribute specifies an
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001040inital value for a <a href="#cl::opt">scalar</a> option. If this attribute is
1041not specified then the command line option value defaults to the value created
Brian Gaeke9d292ff2003-08-19 22:56:22 +00001042by the default constructor for the type. <b>Warning</b>: If you specify both
1043<b><tt>cl::init</tt></b> and <b><tt>cl::location</tt></b> for an option,
1044you must specify <b><tt>cl::location</tt></b> first, so that when the
1045command-line parser sees <b><tt>cl::init</tt></b>, it knows where to put the
1046initial value. (You will get an error at runtime if you don't put them in
Misha Brukman9718e962003-10-24 19:59:21 +00001047the right order.)</li>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001048
Misha Brukman9718e962003-10-24 19:59:21 +00001049<li><a name="cl::location">The <b><tt>cl::location</tt></b></a> attribute where to
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001050store the value for a parsed command line option if using external storage. See
1051the section on <a href="#storage">Internal vs External Storage</a> for more
Misha Brukman9718e962003-10-24 19:59:21 +00001052information.</li>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001053
Misha Brukman9718e962003-10-24 19:59:21 +00001054<li><a name="cl::aliasopt">The <b><tt>cl::aliasopt</tt></b></a> attribute
1055specifies which option a <a href="#cl::alias">cl::alias</a> option is an alias
1056for.</li>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001057
Misha Brukman9718e962003-10-24 19:59:21 +00001058<li><a name="cl::values">The <b><tt>cl::values</tt></b></a> attribute specifies
1059the string-to-value mapping to be used by the generic parser. It takes a
Chris Lattnerb406ead2004-07-16 00:10:54 +00001060<b>clEnumValEnd terminated</b> list of (option, value, description) triplets
1061that
Misha Brukman9718e962003-10-24 19:59:21 +00001062specify the option name, the value mapped to, and the description shown in the
1063<tt>--help</tt> for the tool. Because the generic parser is used most
1064frequently with enum values, two macros are often useful:
1065
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001066<ol>
Misha Brukman9718e962003-10-24 19:59:21 +00001067
1068<li><a name="clEnumVal">The <b><tt>clEnumVal</tt></b></a> macro is used as a
1069nice simple way to specify a triplet for an enum. This macro automatically
1070makes the option name be the same as the enum name. The first option to the
1071macro is the enum, the second is the description for the command line
1072option.</li>
1073
1074<li><a name="clEnumValN">The <b><tt>clEnumValN</tt></b></a> macro is used to
1075specify macro options where the option name doesn't equal the enum name. For
1076this macro, the first argument is the enum value, the second is the flag name,
1077and the second is the description.</li>
1078
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001079</ol>
1080
1081You will get a compile time error if you try to use cl::values with a parser
Misha Brukman9718e962003-10-24 19:59:21 +00001082that does not support it.</li>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001083
1084</ul>
1085
Misha Brukman9718e962003-10-24 19:59:21 +00001086</div>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001087
1088<!-- ======================================================================= -->
Misha Brukman9718e962003-10-24 19:59:21 +00001089<div class="doc_subsection">
1090 <a name="modifiers">Option Modifiers</a>
1091</div>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001092
Misha Brukman9718e962003-10-24 19:59:21 +00001093<div class="doc_text">
1094
1095<p>Option modifiers are the flags and expressions that you pass into the
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001096constructors for <tt><a href="#cl::opt">cl::opt</a></tt> and <tt><a
1097href="#cl::list">cl::list</a></tt>. These modifiers give you the ability to
1098tweak how options are parsed and how <tt>--help</tt> output is generated to fit
Misha Brukman9718e962003-10-24 19:59:21 +00001099your application well.</p>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001100
Misha Brukman9718e962003-10-24 19:59:21 +00001101<p>These options fall into five main catagories:</p>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001102
1103<ol>
Misha Brukman9718e962003-10-24 19:59:21 +00001104<li><a href="#hiding">Hiding an option from <tt>--help</tt> output</a></li>
1105<li><a href="#numoccurrences">Controlling the number of occurrences
1106 required and allowed</a></li>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001107<li><a href="#valrequired">Controlling whether or not a value must be
Misha Brukman9718e962003-10-24 19:59:21 +00001108 specified</a></li>
1109<li><a href="#formatting">Controlling other formatting options</a></li>
1110<li><a href="#misc">Miscellaneous option modifiers</a></li>
1111</ol>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001112
Misha Brukman9718e962003-10-24 19:59:21 +00001113<p>It is not possible to specify two options from the same catagory (you'll get
1114a runtime error) to a single option, except for options in the miscellaneous
Chris Lattner32a32842003-05-22 20:36:06 +00001115catagory. The CommandLine library specifies defaults for all of these settings
1116that are the most useful in practice and the most common, which mean that you
Misha Brukman9718e962003-10-24 19:59:21 +00001117usually shouldn't have to worry about these.</p>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001118
Misha Brukman9718e962003-10-24 19:59:21 +00001119</div>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001120
1121<!-- _______________________________________________________________________ -->
Misha Brukman9718e962003-10-24 19:59:21 +00001122<div class="doc_subsubsection">
1123 <a name="hiding">Hiding an option from <tt>--help</tt> output</a>
1124</div>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001125
Misha Brukman9718e962003-10-24 19:59:21 +00001126<div class="doc_text">
1127
1128<p>The <tt>cl::NotHidden</tt>, <tt>cl::Hidden</tt>, and
1129<tt>cl::ReallyHidden</tt> modifiers are used to control whether or not an option
1130appears in the <tt>--help</tt> and <tt>--help-hidden</tt> output for the
1131compiled program:</p>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001132
1133<ul>
1134
Misha Brukman9718e962003-10-24 19:59:21 +00001135<li><a name="cl::NotHidden">The <b><tt>cl::NotHidden</tt></b></a> modifier
1136(which is the default for <tt><a href="#cl::opt">cl::opt</a></tt> and <tt><a
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001137href="#cl::list">cl::list</a></tt> options), indicates the option is to appear
Misha Brukman9718e962003-10-24 19:59:21 +00001138in both help listings.</li>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001139
Misha Brukman9718e962003-10-24 19:59:21 +00001140<li><a name="cl::Hidden">The <b><tt>cl::Hidden</tt></b></a> modifier (which is the
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001141default for <tt><a href="#cl::alias">cl::alias</a></tt> options), indicates that
1142the option should not appear in the <tt>--help</tt> output, but should appear in
Misha Brukman9718e962003-10-24 19:59:21 +00001143the <tt>--help-hidden</tt> output.</li>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001144
Misha Brukman9718e962003-10-24 19:59:21 +00001145<li><a name="cl::ReallyHidden">The <b><tt>cl::ReallyHidden</tt></b></a> modifier,
1146indicates that the option should not appear in any help output.</li>
1147
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001148</ul>
1149
Misha Brukman9718e962003-10-24 19:59:21 +00001150</div>
1151
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001152<!-- _______________________________________________________________________ -->
Misha Brukman9718e962003-10-24 19:59:21 +00001153<div class="doc_subsubsection">
1154 <a name="numoccurrences">Controlling the number of occurrences required and
1155 allowed</a>
1156</div>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001157
Misha Brukman9718e962003-10-24 19:59:21 +00001158<div class="doc_text">
1159
1160<p>This group of options is used to control how many time an option is allowed
1161(or required) to be specified on the command line of your program. Specifying a
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001162value for this setting allows the CommandLine library to do error checking for
Misha Brukman9718e962003-10-24 19:59:21 +00001163you.</p>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001164
Misha Brukman9718e962003-10-24 19:59:21 +00001165<p>The allowed values for this option group are:</p>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001166
1167<ul>
Misha Brukman9718e962003-10-24 19:59:21 +00001168
1169<li><a name="cl::Optional">The <b><tt>cl::Optional</tt></b></a> modifier (which
1170is the default for the <tt><a href="#cl::opt">cl::opt</a></tt> and <tt><a
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001171href="#cl::alias">cl::alias</a></tt> classes) indicates that your program will
Misha Brukman9718e962003-10-24 19:59:21 +00001172allow either zero or one occurrence of the option to be specified.</li>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001173
Misha Brukman9718e962003-10-24 19:59:21 +00001174<li><a name="cl::ZeroOrMore">The <b><tt>cl::ZeroOrMore</tt></b></a> modifier
1175(which is the default for the <tt><a href="#cl::list">cl::list</a></tt> class)
1176indicates that your program will allow the option to be specified zero or more
1177times.</li>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001178
Misha Brukman9718e962003-10-24 19:59:21 +00001179<li><a name="cl::Required">The <b><tt>cl::Required</tt></b></a> modifier
1180indicates that the specified option must be specified exactly one time.</li>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001181
Misha Brukman9718e962003-10-24 19:59:21 +00001182<li><a name="cl::OneOrMore">The <b><tt>cl::OneOrMore</tt></b></a> modifier
1183indicates that the option must be specified at least one time.</li>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001184
Misha Brukman9718e962003-10-24 19:59:21 +00001185<li>The <b><tt>cl::ConsumeAfter</tt></b> modifier is described in the <a
1186href="#positional">Positional arguments section</a></li>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001187
1188</ul>
1189
Misha Brukman9718e962003-10-24 19:59:21 +00001190<p>If an option is not specified, then the value of the option is equal to the
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001191value specified by the <tt><a href="#cl::init">cl::init</a></tt> attribute. If
1192the <tt><a href="#cl::init">cl::init</a></tt> attribute is not specified, the
Misha Brukman9718e962003-10-24 19:59:21 +00001193option value is initialized with the default constructor for the data type.</p>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001194
Misha Brukman9718e962003-10-24 19:59:21 +00001195<p>If an option is specified multiple times for an option of the <tt><a
1196href="#cl::opt">cl::opt</a></tt> class, only the last value will be
1197retained.</p>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001198
Misha Brukman9718e962003-10-24 19:59:21 +00001199</div>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001200
1201<!-- _______________________________________________________________________ -->
Misha Brukman9718e962003-10-24 19:59:21 +00001202<div class="doc_subsubsection">
1203 <a name="valrequired">Controlling whether or not a value must be specified</a>
1204</div>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001205
Misha Brukman9718e962003-10-24 19:59:21 +00001206<div class="doc_text">
1207
1208<p>This group of options is used to control whether or not the option allows a
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001209value to be present. In the case of the CommandLine library, a value is either
1210specified with an equal sign (e.g. '<tt>-index-depth=17</tt>') or as a trailing
Misha Brukman9718e962003-10-24 19:59:21 +00001211string (e.g. '<tt>-o a.out</tt>').</p>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001212
Misha Brukman9718e962003-10-24 19:59:21 +00001213<p>The allowed values for this option group are:</p>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001214
1215<ul>
Misha Brukman9718e962003-10-24 19:59:21 +00001216
1217<li><a name="cl::ValueOptional">The <b><tt>cl::ValueOptional</tt></b></a> modifier
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001218(which is the default for <tt>bool</tt> typed options) specifies that it is
1219acceptable to have a value, or not. A boolean argument can be enabled just by
1220appearing on the command line, or it can have an explicit '<tt>-foo=true</tt>'.
1221If an option is specified with this mode, it is illegal for the value to be
1222provided without the equal sign. Therefore '<tt>-foo true</tt>' is illegal. To
1223get this behavior, you must use the <a
Misha Brukman9718e962003-10-24 19:59:21 +00001224href="#cl::ValueRequired">cl::ValueRequired</a> modifier.</li>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001225
Misha Brukman9718e962003-10-24 19:59:21 +00001226<li><a name="cl::ValueRequired">The <b><tt>cl::ValueRequired</tt></b></a> modifier
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001227(which is the default for all other types except for <a
1228href="#onealternative">unnamed alternatives using the generic parser</a>)
1229specifies that a value must be provided. This mode informs the command line
1230library that if an option is not provides with an equal sign, that the next
1231argument provided must be the value. This allows things like '<tt>-o
Misha Brukman9718e962003-10-24 19:59:21 +00001232a.out</tt>' to work.</li>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001233
Misha Brukman9718e962003-10-24 19:59:21 +00001234<li><a name="cl::ValueDisallowed">The <b><tt>cl::ValueDisallowed</tt></b></a>
1235modifier (which is the default for <a href="#onealternative">unnamed
1236alternatives using the generic parser</a>) indicates that it is a runtime error
1237for the user to specify a value. This can be provided to disallow users from
1238providing options to boolean options (like '<tt>-foo=true</tt>').</li>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001239
1240</ul>
1241
Misha Brukman9718e962003-10-24 19:59:21 +00001242<p>In general, the default values for this option group work just like you would
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001243want them to. As mentioned above, you can specify the <a
1244href="#cl::ValueDisallowed">cl::ValueDisallowed</a> modifier to a boolean
1245argument to restrict your command line parser. These options are mostly useful
Misha Brukman9718e962003-10-24 19:59:21 +00001246when <a href="#extensionguide">extending the library</a>.</p>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001247
Misha Brukman9718e962003-10-24 19:59:21 +00001248</div>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001249
1250<!-- _______________________________________________________________________ -->
Misha Brukman9718e962003-10-24 19:59:21 +00001251<div class="doc_subsubsection">
1252 <a name="formatting">Controlling other formatting options</a>
1253</div>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001254
Misha Brukman9718e962003-10-24 19:59:21 +00001255<div class="doc_text">
1256
1257<p>The formatting option group is used to specify that the command line option
1258has special abilities and is otherwise different from other command line
1259arguments. As usual, you can only specify at most one of these arguments.</p>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001260
1261<ul>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001262
Misha Brukman9718e962003-10-24 19:59:21 +00001263<li><a name="cl::NormalFormatting">The <b><tt>cl::NormalFormatting</tt></b></a>
1264modifier (which is the default all options) specifies that this option is
1265"normal".</li>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001266
Misha Brukman9718e962003-10-24 19:59:21 +00001267<li><a name="cl::Positional">The <b><tt>cl::Positional</tt></b></a> modifier
1268specifies that this is a positional argument, that does not have a command line
1269option associated with it. See the <a href="#positional">Positional
1270Arguments</a> section for more information.</li>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001271
Misha Brukman9718e962003-10-24 19:59:21 +00001272<li>The <b><a href="#cl::ConsumeAfter"><tt>cl::ConsumeAfter</tt></a></b> modifier
1273specifies that this option is used to capture "interpreter style" arguments. See <a href="#cl::ConsumeAfter">this section for more information</a>.</li>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001274
Misha Brukman9718e962003-10-24 19:59:21 +00001275<li><a name="cl::Prefix">The <b><tt>cl::Prefix</tt></b></a> modifier specifies
1276that this option prefixes its value. With 'Prefix' options, there is no equal
1277sign that separates the value from the option name specified. This is useful
1278for processing odd arguments like '<tt>-lmalloc -L/usr/lib'</tt> in a linker
1279tool. Here, the '<tt>l</tt>' and '<tt>L</tt>' options are normal string (list)
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001280options, that have the <a href="#cl::Prefix">cl::Prefix</a> modifier added to
1281allow the CommandLine library to recognize them. Note that <a
1282href="#cl::Prefix">cl::Prefix</a> options must not have the <a
Misha Brukman9718e962003-10-24 19:59:21 +00001283href="#cl::ValueDisallowed">cl::ValueDisallowed</a> modifier specified.</li>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001284
Misha Brukman9718e962003-10-24 19:59:21 +00001285<li><a name="cl::Grouping">The <b><tt>cl::Grouping</tt></b></a> modifier is used
1286to implement unix style tools (like <tt>ls</tt>) that have lots of single letter
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001287arguments, but only require a single dash. For example, the '<tt>ls -labF</tt>'
1288command actually enables four different options, all of which are single
1289letters. Note that <a href="#cl::Grouping">cl::Grouping</a> options cannot have
Misha Brukman9718e962003-10-24 19:59:21 +00001290values.</li>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001291
1292</ul>
1293
Misha Brukman9718e962003-10-24 19:59:21 +00001294<p>The CommandLine library does not restrict how you use the <a
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001295href="#cl::Prefix">cl::Prefix</a> or <a href="#cl::Grouping">cl::Grouping</a>
1296modifiers, but it is possible to specify ambiguous argument settings. Thus, it
1297is possible to have multiple letter options that are prefix or grouping options,
Misha Brukman9718e962003-10-24 19:59:21 +00001298and they will still work as designed.</p>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001299
Misha Brukman9718e962003-10-24 19:59:21 +00001300<p>To do this, the CommandLine library uses a greedy algorithm to parse the
1301input option into (potentially multiple) prefix and grouping options. The
1302strategy basically looks like this:</p>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001303
Misha Brukman9718e962003-10-24 19:59:21 +00001304<p><tt>parse(string OrigInput) {</tt>
Misha Brukmanc53aefb2004-05-12 18:42:35 +00001305
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001306<ol>
1307<li><tt>string input = OrigInput;</tt>
1308<li><tt>if (isOption(input)) return getOption(input).parse();</tt>&nbsp;&nbsp;&nbsp;&nbsp;<i>// Normal option</i>
1309<li><tt>while (!isOption(input) &amp;&amp; !input.empty()) input.pop_back();</tt>&nbsp;&nbsp;&nbsp;&nbsp;<i>// Remove the last letter</i>
1310<li><tt>if (input.empty()) return error();</tt>&nbsp;&nbsp;&nbsp;&nbsp;<i>// No matching option</i>
1311<li><tt>if (getOption(input).isPrefix())<br>
1312&nbsp;&nbsp;return getOption(input).parse(input);</tt>
1313<li><tt>while (!input.empty()) {&nbsp;&nbsp;&nbsp;&nbsp;<i>// Must be grouping options</i><br>
1314&nbsp;&nbsp;getOption(input).parse();<br>
1315&nbsp;&nbsp;OrigInput.erase(OrigInput.begin(), OrigInput.begin()+input.length());<br>
1316&nbsp;&nbsp;input = OrigInput;<br>
1317&nbsp;&nbsp;while (!isOption(input) &amp;&amp; !input.empty()) input.pop_back();<br>
1318}</tt>
Misha Brukmanc53aefb2004-05-12 18:42:35 +00001319<li><tt>if (!OrigInput.empty()) error();</tt></li>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001320
1321</ol>
Misha Brukmanc53aefb2004-05-12 18:42:35 +00001322
1323<p><tt>}</tt></p>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001324
Misha Brukman9718e962003-10-24 19:59:21 +00001325</div>
Chris Lattner32a32842003-05-22 20:36:06 +00001326
1327<!-- _______________________________________________________________________ -->
Misha Brukman9718e962003-10-24 19:59:21 +00001328<div class="doc_subsubsection">
1329 <a name="misc">Miscellaneous option modifiers</a>
1330</div>
Chris Lattner32a32842003-05-22 20:36:06 +00001331
Misha Brukman9718e962003-10-24 19:59:21 +00001332<div class="doc_text">
1333
1334<p>The miscellaneous option modifiers are the only flags where you can specify
1335more than one flag from the set: they are not mutually exclusive. These flags
1336specify boolean properties that modify the option.</p>
Chris Lattner32a32842003-05-22 20:36:06 +00001337
1338<ul>
1339
Misha Brukman9718e962003-10-24 19:59:21 +00001340<li><a name="cl::CommaSeparated">The <b><tt>cl::CommaSeparated</tt></b></a> modifier
Chris Lattner32a32842003-05-22 20:36:06 +00001341indicates that any commas specified for an option's value should be used to
1342split the value up into multiple values for the option. For example, these two
1343options are equivalent when <tt>cl::CommaSeparated</tt> is specified:
1344"<tt>-foo=a -foo=b -foo=c</tt>" and "<tt>-foo=a,b,c</tt>". This option only
1345makes sense to be used in a case where the option is allowed to accept one or
Misha Brukman9718e962003-10-24 19:59:21 +00001346more values (i.e. it is a <a href="#cl::list">cl::list</a> option).</li>
1347
Chris Lattnerbe801bf2004-05-06 22:03:59 +00001348<li><a name="cl::PositionalEatsArgs">The
1349<b><tt>cl::PositionalEatsArgs</tt></b></a> modifier (which only applies to
1350positional arguments, and only makes sense for lists) indicates that positional
1351argument should consume any strings after it (including strings that start with
1352a "-") up until another recognized positional argument. For example, if you
1353have two "eating" positional arguments "<tt>pos1</tt>" and "<tt>pos2</tt>" the
1354string "<tt>-pos1 -foo -bar baz -pos2 -bork</tt>" would cause the "<tt>-foo -bar
1355-baz</tt>" strings to be applied to the "<tt>-pos1</tt>" option and the
1356"<tt>-bork</tt>" string to be applied to the "<tt>-pos2</tt>" option.</li>
1357
Chris Lattner32a32842003-05-22 20:36:06 +00001358</ul>
1359
Chris Lattnerbe801bf2004-05-06 22:03:59 +00001360<p>So far, these are the only two miscellaneous option modifiers.</p>
Chris Lattner32a32842003-05-22 20:36:06 +00001361
Misha Brukman9718e962003-10-24 19:59:21 +00001362</div>
Chris Lattner32a32842003-05-22 20:36:06 +00001363
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001364<!-- ======================================================================= -->
Misha Brukman9718e962003-10-24 19:59:21 +00001365<div class="doc_subsection">
1366 <a name="toplevel">Top-Level Classes and Functions</a>
1367</div>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001368
Misha Brukman9718e962003-10-24 19:59:21 +00001369<div class="doc_text">
1370
1371<p>Despite all of the built-in flexibility, the CommandLine option library
1372really only consists of one function (<a
Chris Lattnerc1ae40c2002-08-07 18:27:04 +00001373href="#cl::ParseCommandLineOptions"><tt>cl::ParseCommandLineOptions</tt></a>)
1374and three main classes: <a href="#cl::opt"><tt>cl::opt</tt></a>, <a
1375href="#cl::list"><tt>cl::list</tt></a>, and <a
1376href="#cl::alias"><tt>cl::alias</tt></a>. This section describes these three
Misha Brukman9718e962003-10-24 19:59:21 +00001377classes in detail.</p>
1378
1379</div>
Chris Lattnerc1ae40c2002-08-07 18:27:04 +00001380
1381<!-- _______________________________________________________________________ -->
Misha Brukman9718e962003-10-24 19:59:21 +00001382<div class="doc_subsubsection">
1383 <a name="cl::ParseCommandLineOptions">The <tt>cl::ParseCommandLineOptions</tt>
1384 function</a>
1385</div>
Chris Lattnerc1ae40c2002-08-07 18:27:04 +00001386
Misha Brukman9718e962003-10-24 19:59:21 +00001387<div class="doc_text">
1388
1389<p>The <tt>cl::ParseCommandLineOptions</tt> function is designed to be called
Chris Lattnerc1ae40c2002-08-07 18:27:04 +00001390directly from <tt>main</tt>, and is used to fill in the values of all of the
1391command line option variables once <tt>argc</tt> and <tt>argv</tt> are
Misha Brukman9718e962003-10-24 19:59:21 +00001392available.</p>
Chris Lattnerc1ae40c2002-08-07 18:27:04 +00001393
Misha Brukman9718e962003-10-24 19:59:21 +00001394<p>The <tt>cl::ParseCommandLineOptions</tt> function requires two parameters
Chris Lattnerc1ae40c2002-08-07 18:27:04 +00001395(<tt>argc</tt> and <tt>argv</tt>), but may also take an optional third parameter
1396which holds <a href="#description">additional extra text</a> to emit when the
Misha Brukman9718e962003-10-24 19:59:21 +00001397<tt>--help</tt> option is invoked.</p>
Chris Lattnerc1ae40c2002-08-07 18:27:04 +00001398
Misha Brukman9718e962003-10-24 19:59:21 +00001399</div>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001400
1401<!-- _______________________________________________________________________ -->
Misha Brukman9718e962003-10-24 19:59:21 +00001402<div class="doc_subsubsection">
1403 <a name="cl::ParseEnvironmentOptions">The <tt>cl::ParseEnvironmentOptions</tt>
1404 function</a>
1405</div>
Brian Gaekee5842852003-08-19 23:11:43 +00001406
Misha Brukman9718e962003-10-24 19:59:21 +00001407<div class="doc_text">
Brian Gaekee5842852003-08-19 23:11:43 +00001408
Misha Brukman9718e962003-10-24 19:59:21 +00001409<p>The <tt>cl::ParseEnvironmentOptions</tt> function has mostly the same effects
1410as <a
1411href="#cl::ParseCommandLineOptions"><tt>cl::ParseCommandLineOptions</tt></a>,
1412except that it is designed to take values for options from an environment
1413variable, for those cases in which reading the command line is not convenient or
1414not desired. It fills in the values of all the command line option variables
1415just like <a
1416href="#cl::ParseCommandLineOptions"><tt>cl::ParseCommandLineOptions</tt></a>
1417does.</p>
1418
1419<p>It takes three parameters: first, the name of the program (since
1420<tt>argv</tt> may not be available, it can't just look in <tt>argv[0]</tt>),
1421second, the name of the environment variable to examine, and third, the optional
Brian Gaekee5842852003-08-19 23:11:43 +00001422<a href="#description">additional extra text</a> to emit when the
Misha Brukman9718e962003-10-24 19:59:21 +00001423<tt>--help</tt> option is invoked.</p>
Brian Gaekee5842852003-08-19 23:11:43 +00001424
Misha Brukman9718e962003-10-24 19:59:21 +00001425<p><tt>cl::ParseEnvironmentOptions</tt> will break the environment
Brian Gaekee5842852003-08-19 23:11:43 +00001426variable's value up into words and then process them using
1427<a href="#cl::ParseCommandLineOptions"><tt>cl::ParseCommandLineOptions</tt></a>.
1428<b>Note:</b> Currently <tt>cl::ParseEnvironmentOptions</tt> does not support
1429quoting, so an environment variable containing <tt>-option "foo bar"</tt> will
1430be parsed as three words, <tt>-option</tt>, <tt>"foo</tt>, and <tt>bar"</tt>,
1431which is different from what you would get from the shell with the same
Misha Brukman9718e962003-10-24 19:59:21 +00001432input.</p>
1433
1434</div>
Brian Gaekee5842852003-08-19 23:11:43 +00001435
1436<!-- _______________________________________________________________________ -->
Misha Brukman9718e962003-10-24 19:59:21 +00001437<div class="doc_subsubsection">
1438 <a name="cl::opt">The <tt>cl::opt</tt> class</a>
1439</div>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001440
Misha Brukman9718e962003-10-24 19:59:21 +00001441<div class="doc_text">
1442
1443<p>The <tt>cl::opt</tt> class is the class used to represent scalar command line
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001444options, and is the one used most of the time. It is a templated class which
1445can take up to three arguments (all except for the first have default values
Misha Brukman9718e962003-10-24 19:59:21 +00001446though):</p>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001447
1448<pre>
Chris Lattnerc1ae40c2002-08-07 18:27:04 +00001449<b>namespace</b> cl {
1450 <b>template</b> &lt;<b>class</b> DataType, <b>bool</b> ExternalStorage = <b>false</b>,
1451 <b>class</b> ParserClass = parser&lt;DataType&gt; &gt;
1452 <b>class</b> opt;
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001453}
Misha Brukman9718e962003-10-24 19:59:21 +00001454</pre>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001455
Misha Brukman9718e962003-10-24 19:59:21 +00001456<p>The first template argument specifies what underlying data type the command
1457line argument is, and is used to select a default parser implementation. The
1458second template argument is used to specify whether the option should contain
1459the storage for the option (the default) or whether external storage should be
1460used to contain the value parsed for the option (see <a href="#storage">Internal
1461vs External Storage</a> for more information).</p>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001462
Misha Brukman9718e962003-10-24 19:59:21 +00001463<p>The third template argument specifies which parser to use. The default value
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001464selects an instantiation of the <tt>parser</tt> class based on the underlying
1465data type of the option. In general, this default works well for most
1466applications, so this option is only used when using a <a
Misha Brukman9718e962003-10-24 19:59:21 +00001467href="#customparser">custom parser</a>.</p>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001468
Misha Brukman9718e962003-10-24 19:59:21 +00001469</div>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001470
1471<!-- _______________________________________________________________________ -->
Misha Brukman9718e962003-10-24 19:59:21 +00001472<div class="doc_subsubsection">
1473 <a name="cl::list">The <tt>cl::list</tt> class</a>
1474</div>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001475
Misha Brukman9718e962003-10-24 19:59:21 +00001476<div class="doc_text">
1477
1478<p>The <tt>cl::list</tt> class is the class used to represent a list of command
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001479line options. It too is a templated class which can take up to three
Misha Brukman9718e962003-10-24 19:59:21 +00001480arguments:</p>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001481
1482<pre>
Chris Lattnerc1ae40c2002-08-07 18:27:04 +00001483<b>namespace</b> cl {
1484 <b>template</b> &lt;<b>class</b> DataType, <b>class</b> Storage = <b>bool</b>,
1485 <b>class</b> ParserClass = parser&lt;DataType&gt; &gt;
1486 <b>class</b> list;
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001487}
Misha Brukman9718e962003-10-24 19:59:21 +00001488</pre>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001489
Misha Brukman9718e962003-10-24 19:59:21 +00001490<p>This class works the exact same as the <a
1491href="#cl::opt"><tt>cl::opt</tt></a> class, except that the second argument is
1492the <b>type</b> of the external storage, not a boolean value. For this class,
1493the marker type '<tt>bool</tt>' is used to indicate that internal storage should
1494be used.</p>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001495
Misha Brukman9718e962003-10-24 19:59:21 +00001496</div>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001497
1498<!-- _______________________________________________________________________ -->
Misha Brukman9718e962003-10-24 19:59:21 +00001499<div class="doc_subsubsection">
1500 <a name="cl::alias">The <tt>cl::alias</tt> class</a>
1501</div>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001502
Misha Brukman9718e962003-10-24 19:59:21 +00001503<div class="doc_text">
1504
1505<p>The <tt>cl::alias</tt> class is a nontemplated class that is used to form
1506aliases for other arguments.</p>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001507
1508<pre>
Chris Lattnerc1ae40c2002-08-07 18:27:04 +00001509<b>namespace</b> cl {
1510 <b>class</b> alias;
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001511}
Misha Brukman9718e962003-10-24 19:59:21 +00001512</pre>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001513
Misha Brukman9718e962003-10-24 19:59:21 +00001514<p>The <a href="#cl::aliasopt"><tt>cl::aliasopt</tt></a> attribute should be
1515used to specify which option this is an alias for. Alias arguments default to
1516being <a href="#cl::Hidden">Hidden</a>, and use the aliased options parser to do
1517the conversion from string to data.</p>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001518
Misha Brukman9718e962003-10-24 19:59:21 +00001519</div>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001520
1521<!-- ======================================================================= -->
Misha Brukman9718e962003-10-24 19:59:21 +00001522<div class="doc_subsection">
1523 <a name="builtinparsers">Builtin parsers</a>
1524</div>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001525
Misha Brukman9718e962003-10-24 19:59:21 +00001526<div class="doc_text">
1527
1528<p>Parsers control how the string value taken from the command line is
1529translated into a typed value, suitable for use in a C++ program. By default,
1530the CommandLine library uses an instance of <tt>parser&lt;type&gt;</tt> if the
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001531command line option specifies that it uses values of type '<tt>type</tt>'.
1532Because of this, custom option processing is specified with specializations of
Misha Brukman9718e962003-10-24 19:59:21 +00001533the '<tt>parser</tt>' class.</p>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001534
Misha Brukman9718e962003-10-24 19:59:21 +00001535<p>The CommandLine library provides the following builtin parser
1536specializations, which are sufficient for most applications. It can, however,
1537also be extended to work with new data types and new ways of interpreting the
1538same data. See the <a href="#customparser">Writing a Custom Parser</a> for more
1539details on this type of library extension.</p>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001540
Misha Brukman9718e962003-10-24 19:59:21 +00001541<ul>
1542
1543<li><a name="genericparser">The <b>generic <tt>parser&lt;t&gt;</tt> parser</b></a>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001544can be used to map strings values to any data type, through the use of the <a
1545href="#cl::values">cl::values</a> property, which specifies the mapping
1546information. The most common use of this parser is for parsing enum values,
1547which allows you to use the CommandLine library for all of the error checking to
1548make sure that only valid enum values are specified (as opposed to accepting
1549arbitrary strings). Despite this, however, the generic parser class can be used
Misha Brukman9718e962003-10-24 19:59:21 +00001550for any data type.</li>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001551
Misha Brukman9718e962003-10-24 19:59:21 +00001552<li><a name="boolparser">The <b><tt>parser&lt;bool&gt;</tt> specialization</b></a>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001553is used to convert boolean strings to a boolean value. Currently accepted
1554strings are "<tt>true</tt>", "<tt>TRUE</tt>", "<tt>True</tt>", "<tt>1</tt>",
Misha Brukman9718e962003-10-24 19:59:21 +00001555"<tt>false</tt>", "<tt>FALSE</tt>", "<tt>False</tt>", and "<tt>0</tt>".</li>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001556
Misha Brukman9718e962003-10-24 19:59:21 +00001557<li><a name="stringparser">The <b><tt>parser&lt;string&gt;</tt>
1558specialization</b></a> simply stores the parsed string into the string value
1559specified. No conversion or modification of the data is performed.</li>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001560
Misha Brukman9718e962003-10-24 19:59:21 +00001561<li><a name="intparser">The <b><tt>parser&lt;int&gt;</tt> specialization</b></a>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001562uses the C <tt>strtol</tt> function to parse the string input. As such, it will
1563accept a decimal number (with an optional '+' or '-' prefix) which must start
1564with a non-zero digit. It accepts octal numbers, which are identified with a
1565'<tt>0</tt>' prefix digit, and hexadecimal numbers with a prefix of
Misha Brukman9718e962003-10-24 19:59:21 +00001566'<tt>0x</tt>' or '<tt>0X</tt>'.</li>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001567
Misha Brukman9718e962003-10-24 19:59:21 +00001568<li><a name="doubleparser">The <b><tt>parser&lt;double&gt;</tt></b></a> and
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001569<b><tt>parser&lt;float&gt;</tt> specializations</b> use the standard C
1570<tt>strtod</tt> function to convert floating point strings into floating point
1571values. As such, a broad range of string formats is supported, including
1572exponential notation (ex: <tt>1.7e15</tt>) and properly supports locales.
Misha Brukman9718e962003-10-24 19:59:21 +00001573</li>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001574
Misha Brukman9718e962003-10-24 19:59:21 +00001575</ul>
Chris Lattner209c7f42001-07-23 23:03:12 +00001576
Misha Brukman9718e962003-10-24 19:59:21 +00001577</div>
Chris Lattner209c7f42001-07-23 23:03:12 +00001578
1579<!-- *********************************************************************** -->
Misha Brukman9718e962003-10-24 19:59:21 +00001580<div class="doc_section">
1581 <a name="extensionguide">Extension Guide</a>
1582</div>
Chris Lattner209c7f42001-07-23 23:03:12 +00001583<!-- *********************************************************************** -->
1584
Misha Brukman9718e962003-10-24 19:59:21 +00001585<div class="doc_text">
1586
1587<p>Although the CommandLine library has a lot of functionality built into it
Chris Lattnerc1ae40c2002-08-07 18:27:04 +00001588already (as discussed previously), one of its true strengths lie in its
1589extensibility. This section discusses how the CommandLine library works under
Misha Brukman9718e962003-10-24 19:59:21 +00001590the covers and illustrates how to do some simple, common, extensions.</p>
Chris Lattnerc1ae40c2002-08-07 18:27:04 +00001591
Misha Brukman9718e962003-10-24 19:59:21 +00001592</div>
Chris Lattner209c7f42001-07-23 23:03:12 +00001593
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001594<!-- ======================================================================= -->
Misha Brukman9718e962003-10-24 19:59:21 +00001595<div class="doc_subsection">
1596 <a name="customparser">Writing a custom parser</a>
1597</div>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001598
Misha Brukman9718e962003-10-24 19:59:21 +00001599<div class="doc_text">
1600
1601<p>One of the simplest and most common extensions is the use of a custom parser.
Chris Lattnerc1ae40c2002-08-07 18:27:04 +00001602As <a href="#builtinparsers">discussed previously</a>, parsers are the portion
1603of the CommandLine library that turns string input from the user into a
Misha Brukman9718e962003-10-24 19:59:21 +00001604particular parsed data type, validating the input in the process.</p>
Chris Lattnerc1ae40c2002-08-07 18:27:04 +00001605
Misha Brukman9718e962003-10-24 19:59:21 +00001606<p>There are two ways to use a new parser:</p>
Chris Lattnerc1ae40c2002-08-07 18:27:04 +00001607
1608<ol>
Chris Lattnerc1ae40c2002-08-07 18:27:04 +00001609
Misha Brukman9718e962003-10-24 19:59:21 +00001610<li>
Chris Lattnerc1ae40c2002-08-07 18:27:04 +00001611
Misha Brukman9718e962003-10-24 19:59:21 +00001612<p>Specialize the <a href="#genericparser"><tt>cl::parser</tt></a> template for
1613your custom data type.<p>
Chris Lattnerc1ae40c2002-08-07 18:27:04 +00001614
Misha Brukman9718e962003-10-24 19:59:21 +00001615<p>This approach has the advantage that users of your custom data type will
1616automatically use your custom parser whenever they define an option with a value
1617type of your data type. The disadvantage of this approach is that it doesn't
1618work if your fundemental data type is something that is already supported.</p>
Chris Lattnerc1ae40c2002-08-07 18:27:04 +00001619
Misha Brukman9718e962003-10-24 19:59:21 +00001620</li>
Chris Lattnerc1ae40c2002-08-07 18:27:04 +00001621
Misha Brukman9718e962003-10-24 19:59:21 +00001622<li>
1623
1624<p>Write an independent class, using it explicitly from options that need
1625it.</p>
1626
1627<p>This approach works well in situations where you would line to parse an
1628option using special syntax for a not-very-special data-type. The drawback of
1629this approach is that users of your parser have to be aware that they are using
1630your parser, instead of the builtin ones.</p>
1631
1632</li>
1633
1634</ol>
1635
1636<p>To guide the discussion, we will discuss a custom parser that accepts file
Chris Lattnerc1ae40c2002-08-07 18:27:04 +00001637sizes, specified with an optional unit after the numeric size. For example, we
1638would like to parse "102kb", "41M", "1G" into the appropriate integer value. In
1639this case, the underlying data type we want to parse into is
1640'<tt>unsigned</tt>'. We choose approach #2 above because we don't want to make
Misha Brukman9718e962003-10-24 19:59:21 +00001641this the default for all <tt>unsigned</tt> options.</p>
Chris Lattnerc1ae40c2002-08-07 18:27:04 +00001642
Misha Brukman9718e962003-10-24 19:59:21 +00001643<p>To start out, we declare our new <tt>FileSizeParser</tt> class:</p>
Chris Lattnerc1ae40c2002-08-07 18:27:04 +00001644
1645<pre>
1646<b>struct</b> FileSizeParser : <b>public</b> cl::basic_parser&lt;<b>unsigned</b>&gt; {
1647 <i>// parse - Return true on error.</i>
1648 <b>bool</b> parse(cl::Option &amp;O, <b>const char</b> *ArgName, <b>const</b> std::string &amp;ArgValue,
1649 <b>unsigned</b> &amp;Val);
1650};
Misha Brukman9718e962003-10-24 19:59:21 +00001651</pre>
Chris Lattnerc1ae40c2002-08-07 18:27:04 +00001652
Misha Brukman9718e962003-10-24 19:59:21 +00001653<p>Our new class inherits from the <tt>cl::basic_parser</tt> template class to
1654fill in the default, boiler plate, code for us. We give it the data type that
1655we parse into (the last argument to the <tt>parse</tt> method so that clients of
Chris Lattnerc1ae40c2002-08-07 18:27:04 +00001656our custom parser know what object type to pass in to the parse method (here we
Misha Brukman9718e962003-10-24 19:59:21 +00001657declare that we parse into '<tt>unsigned</tt>' variables.</p>
Chris Lattnerc1ae40c2002-08-07 18:27:04 +00001658
Misha Brukman9718e962003-10-24 19:59:21 +00001659<p>For most purposes, the only method that must be implemented in a custom
1660parser is the <tt>parse</tt> method. The <tt>parse</tt> method is called
1661whenever the option is invoked, passing in the option itself, the option name,
1662the string to parse, and a reference to a return value. If the string to parse
1663is not well formed, the parser should output an error message and return true.
1664Otherwise it should return false and set '<tt>Val</tt>' to the parsed value. In
1665our example, we implement <tt>parse</tt> as:</p>
Chris Lattnerc1ae40c2002-08-07 18:27:04 +00001666
1667<pre>
1668<b>bool</b> FileSizeParser::parse(cl::Option &amp;O, <b>const char</b> *ArgName,
1669 <b>const</b> std::string &amp;Arg, <b>unsigned</b> &amp;Val) {
1670 <b>const char</b> *ArgStart = Arg.c_str();
1671 <b>char</b> *End;
1672
1673 <i>// Parse integer part, leaving 'End' pointing to the first non-integer char</i>
1674 Val = (unsigned)strtol(ArgStart, &amp;End, 0);
1675
1676 <b>while</b> (1) {
1677 <b>switch</b> (*End++) {
1678 <b>case</b> 0: <b>return</b> false; <i>// No error</i>
1679 <b>case</b> 'i': <i>// Ignore the 'i' in KiB if people use that</i>
1680 <b>case</b> 'b': <b>case</b> 'B': <i>// Ignore B suffix</i>
1681 <b>break</b>;
1682
1683 <b>case</b> 'g': <b>case</b> 'G': Val *= 1024*1024*1024; <b>break</b>;
1684 <b>case</b> 'm': <b>case</b> 'M': Val *= 1024*1024; <b>break</b>;
1685 <b>case</b> 'k': <b>case</b> 'K': Val *= 1024; <b>break</b>;
1686
1687 default:
1688 <i>// Print an error message if unrecognized character!</i>
1689 <b>return</b> O.error(": '" + Arg + "' value invalid for file size argument!");
1690 }
1691 }
1692}
Misha Brukman9718e962003-10-24 19:59:21 +00001693</pre>
Chris Lattnerc1ae40c2002-08-07 18:27:04 +00001694
Misha Brukman9718e962003-10-24 19:59:21 +00001695<p>This function implements a very simple parser for the kinds of strings we are
Chris Lattnerc1ae40c2002-08-07 18:27:04 +00001696interested in. Although it has some holes (it allows "<tt>123KKK</tt>" for
1697example), it is good enough for this example. Note that we use the option
1698itself to print out the error message (the <tt>error</tt> method always returns
1699true) in order to get a nice error message (shown below). Now that we have our
Misha Brukman9718e962003-10-24 19:59:21 +00001700parser class, we can use it like this:</p>
Chris Lattnerc1ae40c2002-08-07 18:27:04 +00001701
1702<pre>
1703<b>static</b> <a href="#cl::opt">cl::opt</a>&lt;<b>unsigned</b>, <b>false</b>, FileSizeParser&gt;
1704MFS(<i>"max-file-size"</i>, <a href="#cl::desc">cl::desc</a>(<i>"Maximum file size to accept"</i>),
1705 <a href="#cl::value_desc">cl::value_desc</a>("<i>size</i>"));
Misha Brukman9718e962003-10-24 19:59:21 +00001706</pre>
Chris Lattnerc1ae40c2002-08-07 18:27:04 +00001707
Misha Brukman9718e962003-10-24 19:59:21 +00001708<p>Which adds this to the output of our program:</p>
Chris Lattnerc1ae40c2002-08-07 18:27:04 +00001709
1710<pre>
1711OPTIONS:
1712 -help - display available options (--help-hidden for more)
1713 ...
1714 <b>-max-file-size=&lt;size&gt; - Maximum file size to accept</b>
Misha Brukman9718e962003-10-24 19:59:21 +00001715</pre>
Chris Lattnerc1ae40c2002-08-07 18:27:04 +00001716
Misha Brukman9718e962003-10-24 19:59:21 +00001717<p>And we can test that our parse works correctly now (the test program just
1718prints out the max-file-size argument value):</p>
Chris Lattnerc1ae40c2002-08-07 18:27:04 +00001719
1720<pre>
1721$ ./test
1722MFS: 0
1723$ ./test -max-file-size=123MB
1724MFS: 128974848
1725$ ./test -max-file-size=3G
1726MFS: 3221225472
1727$ ./test -max-file-size=dog
1728-max-file-size option: 'dog' value invalid for file size argument!
Misha Brukman9718e962003-10-24 19:59:21 +00001729</pre>
Chris Lattnerc1ae40c2002-08-07 18:27:04 +00001730
Misha Brukman9718e962003-10-24 19:59:21 +00001731<p>It looks like it works. The error message that we get is nice and helpful,
1732and we seem to accept reasonable file sizes. This wraps up the "custom parser"
1733tutorial.</p>
Chris Lattnerc1ae40c2002-08-07 18:27:04 +00001734
Misha Brukman9718e962003-10-24 19:59:21 +00001735</div>
Chris Lattnerc1ae40c2002-08-07 18:27:04 +00001736
1737<!-- ======================================================================= -->
Misha Brukman9718e962003-10-24 19:59:21 +00001738<div class="doc_subsection">
1739 <a name="explotingexternal">Exploiting external storage</a>
1740</div>
Chris Lattnerc1ae40c2002-08-07 18:27:04 +00001741
Misha Brukman9718e962003-10-24 19:59:21 +00001742<div class="doc_text">
Reid Spencerb993feb2004-08-30 05:56:51 +00001743 <p>Several of the LLVM libraries define static <tt>cl::opt</tt> instances that
1744 will automatically be included in any program that links with that library.
1745 This is a feature. However, sometimes it is necessary to know the value of the
1746 command line option outside of the library. In these cases the library does or
1747 should provide an external storage location that is accessible to users of the
1748 library. Examples of this include the <tt>llvm::DebugFlag</tt> exported by the
1749 <tt>lib/Support/Debug.cpp</tt> file and the <tt>llvm::TimePassesIsEnabled</tt>
1750 flag exported by the <tt>lib/VMCore/Pass.cpp</tt> file.</p>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001751
Reid Spencerb993feb2004-08-30 05:56:51 +00001752<p>TODO: complete this section</p>
Misha Brukman9718e962003-10-24 19:59:21 +00001753
1754</div>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001755
1756<!-- ======================================================================= -->
Misha Brukman9718e962003-10-24 19:59:21 +00001757<div class="doc_subsection">
1758 <a name="dynamicopts">Dynamically adding command line options</a>
1759</div>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001760
Misha Brukman9718e962003-10-24 19:59:21 +00001761<div class="doc_text">
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001762
Misha Brukman9718e962003-10-24 19:59:21 +00001763<p>TODO: fill in this section</p>
Chris Lattner209c7f42001-07-23 23:03:12 +00001764
Misha Brukman9718e962003-10-24 19:59:21 +00001765</div>
Chris Lattner209c7f42001-07-23 23:03:12 +00001766
Chris Lattner209c7f42001-07-23 23:03:12 +00001767<!-- *********************************************************************** -->
1768
1769<hr>
Misha Brukmanc53aefb2004-05-12 18:42:35 +00001770<address>
1771 <a href="http://jigsaw.w3.org/css-validator/check/referer"><img
1772 src="http://jigsaw.w3.org/css-validator/images/vcss" alt="Valid CSS!"></a>
1773 <a href="http://validator.w3.org/check/referer"><img
1774 src="http://www.w3.org/Icons/valid-html401" alt="Valid HTML 4.01!"></a>
1775
1776 <a href="mailto:sabre@nondot.org">Chris Lattner</a><br>
1777 <a href="http://llvm.cs.uiuc.edu">LLVM Compiler Infrastructure</a><br>
Misha Brukman3c299112003-11-07 18:11:14 +00001778 Last modified: $Date$
Misha Brukmanc53aefb2004-05-12 18:42:35 +00001779</address>
Misha Brukman9718e962003-10-24 19:59:21 +00001780
1781</body>
1782</html>