blob: abc6eb6823eedf014a813d28656beccfe0a1f36b [file] [log] [blame]
Chris Lattner209c7f42001-07-23 23:03:12 +00001<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
Chris Lattnerae853632002-07-25 19:27:01 +00002<html><head><title>CommandLine 2.0 Library Manual</title></head>
Chris Lattner209c7f42001-07-23 23:03:12 +00003<body bgcolor=white>
4
5<table width="100%" bgcolor="#330077" border=0 cellpadding=4 cellspacing=0>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00006<tr><td>&nbsp; <font size=+3 color="#EEEEFF" face="Georgia,Palatino,Times,Roman"><b>CommandLine 2.0 Library Manual</b></font></td>
Chris Lattner209c7f42001-07-23 23:03:12 +00007</tr></table>
8
9<ol>
10 <li><a href="#introduction">Introduction</a>
11 <li><a href="#quickstart">Quick Start Guide</a>
12 <ol>
Chris Lattnerae853632002-07-25 19:27:01 +000013 <li><a href="#bool">Boolean Arguments</a>
14 <li><a href="#alias">Argument Aliases</a>
Chris Lattnere76d4ab2002-08-06 19:36:06 +000015 <li><a href="#onealternative">Selecting an alternative from a
16 set of possibilities</a>
Chris Lattner209c7f42001-07-23 23:03:12 +000017 <li><a href="#namedalternatives">Named alternatives</a>
Chris Lattnerae853632002-07-25 19:27:01 +000018 <li><a href="#list">Parsing a list of options</a>
Chris Lattnerc1ae40c2002-08-07 18:27:04 +000019 <li><a href="#description">Adding freeform text to help output</a>
Chris Lattner209c7f42001-07-23 23:03:12 +000020 </ol>
21 <li><a href="#referenceguide">Reference Guide</a>
Chris Lattnerae853632002-07-25 19:27:01 +000022 <ol>
Chris Lattnere76d4ab2002-08-06 19:36:06 +000023 <li><a href="#positional">Positional Arguments</a>
Chris Lattnerae853632002-07-25 19:27:01 +000024 <ul>
Chris Lattnere76d4ab2002-08-06 19:36:06 +000025 <li><a href="#--">Specifying positional options with hyphens</a>
26 <li><a href="#cl::ConsumeAfter">The <tt>cl::ConsumeAfter</tt>
27 modifier</a>
Chris Lattnerae853632002-07-25 19:27:01 +000028 </ul>
Chris Lattnere76d4ab2002-08-06 19:36:06 +000029 <li><a href="#storage">Internal vs External Storage</a>
30 <li><a href="#attributes">Option Attributes</a>
31 <li><a href="#modifiers">Option Modifiers</a>
Chris Lattnerae853632002-07-25 19:27:01 +000032 <ul>
Chris Lattnere76d4ab2002-08-06 19:36:06 +000033 <li><a href="#hiding">Hiding an option from <tt>--help</tt> output</a>
34 <li><a href="#numoccurances">Controlling the number of occurances
35 required and allowed</a>
36 <li><a href="#valrequired">Controlling whether or not a value must be
37 specified</a>
38 <li><a href="#formatting">Controlling other formatting options</a>
Chris Lattner32a32842003-05-22 20:36:06 +000039 <li><a href="#misc">Miscellaneous option modifiers</a>
Chris Lattnere76d4ab2002-08-06 19:36:06 +000040 </ul>
Chris Lattnerc1ae40c2002-08-07 18:27:04 +000041 <li><a href="#toplevel">Top-Level Classes and Functions</a>
Chris Lattnere76d4ab2002-08-06 19:36:06 +000042 <ul>
Chris Lattnerc1ae40c2002-08-07 18:27:04 +000043 <li><a href="#cl::ParseCommandLineOptions">The
44 <tt>cl::ParseCommandLineOptions</tt> function</a>
Chris Lattnere76d4ab2002-08-06 19:36:06 +000045 <li><a href="#cl::opt">The <tt>cl::opt</tt> class</a>
46 <li><a href="#cl::list">The <tt>cl::list</tt> class</a>
47 <li><a href="#cl::alias">The <tt>cl::alias</tt> class</a>
48 </ul>
49 <li><a href="#builtinparsers">Builtin parsers</a>
50 <ul>
51 <li><a href="#genericparser">The Generic <tt>parser&lt;t&gt;</tt>
52 parser</a>
53 <li><a href="#boolparser">The <tt>parser&lt;bool&gt;</tt>
54 specialization</a>
55 <li><a href="#stringparser">The <tt>parser&lt;string&gt;</tt>
56 specialization</a>
57 <li><a href="#intparser">The <tt>parser&lt;int&gt;</tt>
58 specialization</a>
59 <li><a href="#doubleparser">The <tt>parser&lt;double&gt;</tt> and
60 <tt>parser&lt;float&gt;</tt> specializations</a>
Chris Lattnerae853632002-07-25 19:27:01 +000061 </ul>
62 </ol>
Chris Lattner209c7f42001-07-23 23:03:12 +000063 <li><a href="#extensionguide">Extension Guide</a>
Chris Lattnerae853632002-07-25 19:27:01 +000064 <ol>
Chris Lattnere76d4ab2002-08-06 19:36:06 +000065 <li><a href="#customparser">Writing a custom parser</a>
66 <li><a href="#explotingexternal">Exploiting external storage</a>
67 <li><a href="#dynamicopts">Dynamically adding command line options</a>
Chris Lattnerae853632002-07-25 19:27:01 +000068 </ol>
Chris Lattnere76d4ab2002-08-06 19:36:06 +000069
70 <p><b>Written by <a href="mailto:sabre@nondot.org">Chris Lattner</a></b><p>
Chris Lattner209c7f42001-07-23 23:03:12 +000071</ol><p>
72
73
74<!-- *********************************************************************** -->
Chris Lattnerc1ae40c2002-08-07 18:27:04 +000075<table width="100%" bgcolor="#330077" border=0 cellpadding=4 cellspacing=0>
Chris Lattner209c7f42001-07-23 23:03:12 +000076<tr><td align=center><font color="#EEEEFF" size=+2 face="Georgia,Palatino"><b>
77<a name="introduction">Introduction
78</b></font></td></tr></table><ul>
79<!-- *********************************************************************** -->
80
Chris Lattnerae853632002-07-25 19:27:01 +000081This document describes the CommandLine argument processing library. It will
Chris Lattnere76d4ab2002-08-06 19:36:06 +000082show you how to use it, and what it can do. The CommandLine library uses a
83declarative approach to specifying the command line options that your program
84takes. By default, these options declarations implicitly hold the value parsed
85for the option declared (of course this <a href="#storage">can be
86changed</a>).<p>
Chris Lattner209c7f42001-07-23 23:03:12 +000087
Chris Lattnerae853632002-07-25 19:27:01 +000088Although there are a <b>lot</b> of command line argument parsing libraries out
89there in many different languages, none of them fit well with what I needed. By
90looking at the features and problems of other libraries, I designed the
91CommandLine library to have the following features:<p>
Chris Lattner209c7f42001-07-23 23:03:12 +000092
93<ol>
Chris Lattnerae853632002-07-25 19:27:01 +000094<li>Speed: The CommandLine library is very quick and uses little resources. The
95parsing time of the library is directly proportional to the number of arguments
96parsed, not the the number of options recognized. Additionally, command line
Chris Lattneredf351f2003-06-21 21:45:56 +000097argument values are captured transparently into user defined global variables,
98which can be accessed like any other variable (and with the same
99performance).<p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000100
Chris Lattnerae853632002-07-25 19:27:01 +0000101<li>Type Safe: As a user of CommandLine, you don't have to worry about
102remembering the type of arguments that you want (is it an int? a string? a
103bool? an enum?) and keep casting it around. Not only does this help prevent
104error prone constructs, it also leads to dramatically cleaner source code.<p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000105
Chris Lattnerae853632002-07-25 19:27:01 +0000106<li>No subclasses required: To use CommandLine, you instantiate variables that
107correspond to the arguments that you would like to capture, you don't subclass a
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000108parser. This means that you don't have to write <b>any</b> boilerplate code.<p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000109
Chris Lattnerae853632002-07-25 19:27:01 +0000110<li>Globally accessible: Libraries can specify command line arguments that are
111automatically enabled in any tool that links to the library. This is possible
112because the application doesn't have to keep a "list" of arguments to pass to
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000113the parser. This also makes supporting <a href="#dynamicopts">dynamically
114loaded options</a> trivial.<p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000115
Chris Lattnerc1ae40c2002-08-07 18:27:04 +0000116<li>Cleaner: CommandLine supports enum and other types directly, meaning that
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000117there is less error and more security built into the library. You don't have to
118worry about whether your integral command line argument accidentally got
119assigned a value that is not valid for your enum type.<p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000120
Chris Lattnerae853632002-07-25 19:27:01 +0000121<li>Powerful: The CommandLine library supports many different types of
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000122arguments, from simple <a href="#boolparser">boolean flags</a> to <a
123href="#cl::opt">scalars arguments</a> (<a href="#stringparser">strings</a>, <a
124href="#intparser">integers</a>, <a href="#genericparser">enums</a>, <a
125href="#doubleparser">doubles</a>), to <a href="#cl::list">lists of
126arguments</a>. This is possible because CommandLine is...<p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000127
Chris Lattnerae853632002-07-25 19:27:01 +0000128<li>Extensible: It is very simple to add a new argument type to CommandLine.
129Simply specify the parser that you want to use with the command line option when
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000130you declare it. <a href="#customparser">Custom parsers</a> are no problem.<p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000131
Chris Lattnerae853632002-07-25 19:27:01 +0000132<li>Labor Saving: The CommandLine library cuts down on the amount of grunt work
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000133that you, the user, have to do. For example, it automatically provides a
134<tt>--help</tt> option that shows the available command line options for your
135tool. Additionally, it does most of the basic correctness checking for you.<p>
136
137<li>Capable: The CommandLine library can handle lots of different forms of
138options often found in real programs. For example, <a
139href="#positional">positional</a> arguments, <tt>ls</tt> style <a
140href="#cl::Grouping">grouping</a> options (to allow processing '<tt>ls
141-lad</tt>' naturally), <tt>ld</tt> style <a href="#cl::Prefix">prefix</a>
142options (to parse '<tt>-lmalloc -L/usr/lib</tt>'), and <a
143href="#cl::ConsumeAfter">interpreter style options</a>.<p>
144
Chris Lattner209c7f42001-07-23 23:03:12 +0000145</ol>
146
Chris Lattnerae853632002-07-25 19:27:01 +0000147This document will hopefully let you jump in and start using CommandLine in your
148utility quickly and painlessly. Additionally it should be a simple reference
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000149manual to figure out how stuff works. If it is failing in some area (or you
150want an extension to the library), nag the author, <a
151href="mailto:sabre@nondot.org">Chris Lattner</a>.<p>
152
Chris Lattner209c7f42001-07-23 23:03:12 +0000153
154
155<!-- *********************************************************************** -->
156</ul><table width="100%" bgcolor="#330077" border=0 cellpadding=4 cellspacing=0><tr><td align=center><font color="#EEEEFF" size=+2 face="Georgia,Palatino"><b>
157<a name="quickstart">Quick Start Guide
158</b></font></td></tr></table><ul>
159<!-- *********************************************************************** -->
160
Chris Lattnerae853632002-07-25 19:27:01 +0000161This section of the manual runs through a simple CommandLine'ification of a
162basic compiler tool. This is intended to show you how to jump into using the
163CommandLine library in your own program, and show you some of the cool things it
164can do.<p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000165
Chris Lattnerae853632002-07-25 19:27:01 +0000166To start out, you need to include the CommandLine header file into your
167program:<p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000168
169<pre>
Chris Lattnerae853632002-07-25 19:27:01 +0000170 #include "Support/CommandLine.h"
Chris Lattner209c7f42001-07-23 23:03:12 +0000171</pre><p>
172
173Additionally, you need to add this as the first line of your main program:<p>
174
175<pre>
176int main(int argc, char **argv) {
Chris Lattnerc1ae40c2002-08-07 18:27:04 +0000177 <a href="#cl::ParseCommandLineOptions">cl::ParseCommandLineOptions</a>(argc, argv);
Chris Lattner209c7f42001-07-23 23:03:12 +0000178 ...
179}
180</pre><p>
181
Chris Lattnerae853632002-07-25 19:27:01 +0000182... which actually parses the arguments and fills in the variable
183declarations.<p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000184
Chris Lattnerae853632002-07-25 19:27:01 +0000185Now that you are ready to support command line arguments, we need to tell the
186system which ones we want, and what type of argument they are. The CommandLine
Chris Lattneredf351f2003-06-21 21:45:56 +0000187library uses a declarative syntax to model command line arguments with the
188global variable declarations that capture the parsed values. This means that
189for every command line option that you would like to support, there should be a
190global variable declaration to capture the result. For example, in a compiler,
191we would like to support the unix standard '<tt>-o &lt;filename&gt;</tt>' option
192to specify where to put the output. With the CommandLine library, this is
193represented like this:<p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000194
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000195<pre><a name="value_desc_example">
196<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>"));
Chris Lattner209c7f42001-07-23 23:03:12 +0000197</pre><p>
198
Chris Lattneredf351f2003-06-21 21:45:56 +0000199This declares a global variable "<tt>OutputFilename</tt>" that is used to
200capture the result of the "<tt>o</tt>" argument (first parameter). We specify
201that this is a simple scalar option by using the "<tt><a
202href="#cl::opt">cl::opt</a></tt>" template (as opposed to the <a
203href="#list">"<tt>cl::list</tt> template</a>), and tell the CommandLine library
204that the data type that we are parsing is a string.<p>
Chris Lattnerae853632002-07-25 19:27:01 +0000205
206The second and third parameters (which are optional) are used to specify what to
207output for the "<tt>--help</tt>" option. In this case, we get a line that looks
208like this:<p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000209
210<pre>
Chris Lattnerae853632002-07-25 19:27:01 +0000211USAGE: compiler [options]
Chris Lattner209c7f42001-07-23 23:03:12 +0000212
Chris Lattnerae853632002-07-25 19:27:01 +0000213OPTIONS:
214 -help - display available options (--help-hidden for more)
Chris Lattnerc1ae40c2002-08-07 18:27:04 +0000215 <b>-o &lt;filename&gt; - Specify output filename</b>
Chris Lattnerae853632002-07-25 19:27:01 +0000216</pre>
217
218Because we specified that the command line option should parse using the
219<tt>string</tt> data type, the variable declared is automatically usable as a
220real string in all contexts that a normal C++ string object may be used. For
221example:<p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000222
223<pre>
224 ...
225 ofstream Output(OutputFilename.c_str());
226 if (Out.good()) ...
227 ...
228</pre><p>
229
Chris Lattnerae853632002-07-25 19:27:01 +0000230There are many different options that you can use to customize the command line
231option handling library, but the above example shows the general interface to
232these options. The options can be specified in any order, and are specified
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000233with helper functions like <a href="#cl::desc"><tt>cl::desc(...)</tt></a>, so
Chris Lattnerc1ae40c2002-08-07 18:27:04 +0000234there are no positional dependencies to remember. The available options are
235discussed in detail in the <a href="#referenceguide">Reference Guide</a>.<p>
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000236
Chris Lattner209c7f42001-07-23 23:03:12 +0000237
Chris Lattnerae853632002-07-25 19:27:01 +0000238Continuing the example, we would like to have our compiler take an input
239filename as well as an output filename, but we do not want the input filename to
240be specified with a hyphen (ie, not <tt>-filename.c</tt>). To support this
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000241style of argument, the CommandLine library allows for <a
242href="#positional">positional</a> arguments to be specified for the program.
243These positional arguments are filled with command line parameters that are not
244in option form. We use this feature like this:<p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000245
246<pre>
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000247<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 +0000248</pre>
249
Chris Lattnerae853632002-07-25 19:27:01 +0000250This declaration indicates that the first positional argument should be treated
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000251as the input filename. Here we use the <tt><a
252href="#cl::init">cl::init</a></tt> option to specify an initial value for the
253command line option, which is used if the option is not specified (if you do not
254specify a <tt><a href="#cl::init">cl::init</a></tt> modifier for an option, then
255the default constructor for the data type is used to initialize the value).
Chris Lattnerae853632002-07-25 19:27:01 +0000256Command line options default to being optional, so if we would like to require
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000257that the user always specify an input filename, we would add the <tt><a
258href="#cl::Required">cl::Required</a></tt> flag, and we could eliminate the
259<tt><a href="#cl::init">cl::init</a></tt> modifier, like this:<p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000260
261<pre>
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000262<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 +0000263</pre>
264
Chris Lattnerae853632002-07-25 19:27:01 +0000265Again, the CommandLine library does not require the options to be specified in
266any particular order, so the above declaration is equivalent to:<p>
267
268<pre>
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000269<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 +0000270</pre>
271
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000272By simply adding the <tt><a href="#cl::Required">cl::Required</a></tt> flag, the
273CommandLine library will automatically issue an error if the argument is not
274specified, which shifts all of the command line option verification code out of
275your application into the library. This is just one example of how using flags
276can alter the default behaviour of the library, on a per-option basis. By
277adding one of the declarations above, the <tt>--help</tt> option synopsis is now
278extended to:<p>
Chris Lattnerae853632002-07-25 19:27:01 +0000279
280<pre>
Chris Lattnerc1ae40c2002-08-07 18:27:04 +0000281USAGE: compiler [options] <b>&lt;input file&gt;</b>
Chris Lattnerae853632002-07-25 19:27:01 +0000282
283OPTIONS:
284 -help - display available options (--help-hidden for more)
285 -o &lt;filename&gt; - Specify output filename
286</pre>
287
288... indicating that an input filename is expected.<p>
289
Chris Lattner209c7f42001-07-23 23:03:12 +0000290
Chris Lattnerc1ae40c2002-08-07 18:27:04 +0000291
Chris Lattner209c7f42001-07-23 23:03:12 +0000292<!-- ======================================================================= -->
293</ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0><tr><td>&nbsp;</td><td width="100%">&nbsp; <font color="#EEEEFF" face="Georgia,Palatino"><b>
Chris Lattnerae853632002-07-25 19:27:01 +0000294<a name="bool">Boolean Arguments
Chris Lattner209c7f42001-07-23 23:03:12 +0000295</b></font></td></tr></table><ul>
296
Chris Lattnerae853632002-07-25 19:27:01 +0000297In addition to input and output filenames, we would like the compiler example to
298support three boolean flags: "<tt>-f</tt>" to force overwriting of the output
299file, "<tt>--quiet</tt>" to enable quiet mode, and "<tt>-q</tt>" for backwards
300compatibility with some of our users. We can support these by declaring options
301of boolean type like this:<p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000302
303<pre>
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000304<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>"));
305<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>"));
306<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>);
Chris Lattner209c7f42001-07-23 23:03:12 +0000307</pre><p>
308
Chris Lattnerae853632002-07-25 19:27:01 +0000309This does what you would expect: it declares three boolean variables
310("<tt>Force</tt>", "<tt>Quiet</tt>", and "<tt>Quiet2</tt>") to recognize these
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000311options. Note that the "<tt>-q</tt>" option is specified with the "<a
312href="#cl::Hidden"><tt>cl::Hidden</tt></a>" flag. This modifier prevents it
313from being shown by the standard "<tt>--help</tt>" output (note that it is still
314shown in the "<tt>--help-hidden</tt>" output).<p>
Chris Lattnerae853632002-07-25 19:27:01 +0000315
Chris Lattnerc1ae40c2002-08-07 18:27:04 +0000316The CommandLine library uses a <a href="#builtinparsers">different parser</a>
317for different data types. For example, in the string case, the argument passed
318to the option is copied literally into the content of the string variable... we
319obviously cannot do that in the boolean case, however, so we must use a smarter
320parser. In the case of the boolean parser, it allows no options (in which case
321it assigns the value of true to the variable), or it allows the values
322"<tt>true</tt>" or "<tt>false</tt>" to be specified, allowing any of the
323following inputs:<p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000324
325<pre>
Chris Lattnerae853632002-07-25 19:27:01 +0000326 compiler -f # No value, 'Force' == true
327 compiler -f=true # Value specified, 'Force' == true
328 compiler -f=TRUE # Value specified, 'Force' == true
329 compiler -f=FALSE # Value specified, 'Force' == false
330</pre>
331
Chris Lattnerc1ae40c2002-08-07 18:27:04 +0000332... you get the idea. The <a href="#boolparser">bool parser</a> just turns the
333string values into boolean values, and rejects things like '<tt>compiler
334-f=foo</tt>'. Similarly, the <a href="#doubleparser">float</a>, <a
335href="#doubleparser">double</a>, and <a href="#intparser">int</a> parsers work
336like you would expect, using the '<tt>strtol</tt>' and '<tt>strtod</tt>' C
337library calls to parse the string value into the specified data type.<p>
Chris Lattnerae853632002-07-25 19:27:01 +0000338
339With the declarations above, "<tt>compiler --help</tt>" emits this:<p>
340
341<pre>
342USAGE: compiler [options] &lt;input file&gt;
Chris Lattner209c7f42001-07-23 23:03:12 +0000343
344OPTIONS:
Chris Lattnerc1ae40c2002-08-07 18:27:04 +0000345 <b>-f - Overwrite output files</b>
Chris Lattner209c7f42001-07-23 23:03:12 +0000346 -o - Override output filename
Chris Lattnerc1ae40c2002-08-07 18:27:04 +0000347 <b>-quiet - Don't print informational messages</b>
Chris Lattner209c7f42001-07-23 23:03:12 +0000348 -help - display available options (--help-hidden for more)
349</pre><p>
350
Chris Lattnerae853632002-07-25 19:27:01 +0000351and "<tt>opt --help-hidden</tt>" prints this:<p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000352
353<pre>
Chris Lattnerc1ae40c2002-08-07 18:27:04 +0000354USAGE: compiler [options] &lt;input file&gt;
Chris Lattner209c7f42001-07-23 23:03:12 +0000355
356OPTIONS:
357 -f - Overwrite output files
358 -o - Override output filename
Chris Lattnerc1ae40c2002-08-07 18:27:04 +0000359 <b>-q - Don't print informational messages</b>
Chris Lattner209c7f42001-07-23 23:03:12 +0000360 -quiet - Don't print informational messages
361 -help - display available options (--help-hidden for more)
362</pre><p>
363
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000364This brief example has shown you how to use the '<tt><a
365href="#cl::opt">cl::opt</a></tt>' class to parse simple scalar command line
366arguments. In addition to simple scalar arguments, the CommandLine library also
367provides primitives to support CommandLine option <a href="#alias">aliases</a>,
368and <a href="#list">lists</a> of options.<p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000369
370
371<!-- ======================================================================= -->
372</ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0><tr><td>&nbsp;</td><td width="100%">&nbsp; <font color="#EEEEFF" face="Georgia,Palatino"><b>
Chris Lattnerae853632002-07-25 19:27:01 +0000373<a name="alias">Argument Aliases
Chris Lattner209c7f42001-07-23 23:03:12 +0000374</b></font></td></tr></table><ul>
375
Chris Lattnerae853632002-07-25 19:27:01 +0000376So far, the example works well, except for the fact that we need to check the
377quiet condition like this now:<p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000378
379<pre>
380...
381 if (!Quiet &amp;&amp; !Quiet2) printInformationalMessage(...);
382...
383</pre><p>
384
Chris Lattnerae853632002-07-25 19:27:01 +0000385... which is a real pain! Instead of defining two values for the same
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000386condition, 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 +0000387option an <b>alias</b> for the "<tt>-quiet</tt>" option, instead of providing
388a value itself:<p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000389
390<pre>
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000391<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>"));
392<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>"));
393<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));
Chris Lattner209c7f42001-07-23 23:03:12 +0000394</pre><p>
395
Chris Lattnerae853632002-07-25 19:27:01 +0000396The third line (which is the only one we modified from above) defines a
397"<tt>-q</tt> alias that updates the "<tt>Quiet</tt>" variable (as specified by
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000398the <tt><a href="#cl::aliasopt">cl::aliasopt</a></tt> modifier) whenever it is
399specified. Because aliases do not hold state, the only thing the program has to
400query is the <tt>Quiet</tt> variable now. Another nice feature of aliases is
401that they automatically hide themselves from the <tt>-help</tt> output
402(although, again, they are still visible in the <tt>--help-hidden
403output</tt>).<p>
Chris Lattnerae853632002-07-25 19:27:01 +0000404
405Now the application code can simply use:<p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000406
407<pre>
408...
409 if (!Quiet) printInformationalMessage(...);
410...
411</pre><p>
412
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000413... which is much nicer! The "<tt><a href="#cl::alias">cl::alias</a></tt>" can be used to specify an
Chris Lattnerae853632002-07-25 19:27:01 +0000414alternative name for any variable type, and has many uses.<p>
415
Chris Lattner209c7f42001-07-23 23:03:12 +0000416
417
418<!-- ======================================================================= -->
419</ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0><tr><td>&nbsp;</td><td width="100%">&nbsp; <font color="#EEEEFF" face="Georgia,Palatino"><b>
Chris Lattnerae853632002-07-25 19:27:01 +0000420<a name="onealternative">Selecting an alternative from a set of possibilities
Chris Lattner209c7f42001-07-23 23:03:12 +0000421</b></font></td></tr></table><ul>
422
Chris Lattnerae853632002-07-25 19:27:01 +0000423So far, we have seen how the CommandLine library handles builtin types like
424<tt>std::string</tt>, <tt>bool</tt> and <tt>int</tt>, but how does it handle
425things it doesn't know about, like enums or '<tt>int*</tt>'s?<p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000426
Chris Lattnerae853632002-07-25 19:27:01 +0000427The answer is that it uses a table driven generic parser (unless you specify
428your own parser, as described in the <a href="#extensionguide">Extension
429Guide</a>). This parser maps literal strings to whatever type is required, are
430requires you to tell it what this mapping should be.<p>
431
432Lets say that we would like to add four optimizations levels to our optimizer,
433using the standard flags "<tt>-g</tt>", "<tt>-O0</tt>", "<tt>-O1</tt>", and
434"<tt>-O2</tt>". We could easily implement this with boolean options like above,
435but there are several problems with this strategy:<p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000436
437<ol>
Chris Lattnerae853632002-07-25 19:27:01 +0000438<li>A user could specify more than one of the options at a time, for example,
439"<tt>opt -O3 -O2</tt>". The CommandLine library would not be able to catch this
440erroneous input for us.
441
Chris Lattner209c7f42001-07-23 23:03:12 +0000442<li>We would have to test 4 different variables to see which ones are set.
Chris Lattnerae853632002-07-25 19:27:01 +0000443
444<li>This doesn't map to the numeric levels that we want... so we cannot easily
445see if some level &gt;= "<tt>-O1</tt>" is enabled.
446
Chris Lattner209c7f42001-07-23 23:03:12 +0000447</ol><p>
448
Chris Lattnerae853632002-07-25 19:27:01 +0000449To cope with these problems, we can use an enum value, and have the CommandLine
450library fill it in with the appropriate level directly, which is used like
451this:<p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000452
453<pre>
454enum OptLevel {
455 g, O1, O2, O3
456};
457
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000458<a href="#cl::opt">cl::opt</a>&lt;OptLevel&gt; OptimizationLevel(<a href="#cl::desc">cl::desc</a>("<i>Choose optimization level:</i>"),
459 <a href="#cl::values">cl::values</a>(
Chris Lattnerae853632002-07-25 19:27:01 +0000460 clEnumVal(g , "<i>No optimizations, enable debugging</i>"),
461 clEnumVal(O1, "<i>Enable trivial optimizations</i>"),
462 clEnumVal(O2, "<i>Enable default optimizations</i>"),
463 clEnumVal(O3, "<i>Enable expensive optimizations</i>"),
464 0));
Chris Lattner209c7f42001-07-23 23:03:12 +0000465
466...
Chris Lattnerae853632002-07-25 19:27:01 +0000467 if (OptimizationLevel &gt;= O2) doPartialRedundancyElimination(...);
Chris Lattner209c7f42001-07-23 23:03:12 +0000468...
469</pre><p>
470
Chris Lattnerae853632002-07-25 19:27:01 +0000471This declaration defines a variable "<tt>OptimizationLevel</tt>" of the
472"<tt>OptLevel</tt>" enum type. This variable can be assigned any of the values
473that are listed in the declaration (Note that the declaration list must be
474terminated with the "<tt>0</tt>" argument!). The CommandLine library enforces
475that the user can only specify one of the options, and it ensure that only valid
476enum values can be specified. The "<tt>clEnumVal</tt>" macros ensure that the
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000477command line arguments matched the enum values. With this option added, our
478help output now is:<p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000479
Chris Lattnerae853632002-07-25 19:27:01 +0000480<pre>
481USAGE: compiler [options] &lt;input file&gt;
Chris Lattner209c7f42001-07-23 23:03:12 +0000482
Chris Lattnerae853632002-07-25 19:27:01 +0000483OPTIONS:
Chris Lattnerc1ae40c2002-08-07 18:27:04 +0000484 <b>Choose optimization level:
Chris Lattnerae853632002-07-25 19:27:01 +0000485 -g - No optimizations, enable debugging
486 -O1 - Enable trivial optimizations
487 -O2 - Enable default optimizations
Chris Lattnerc1ae40c2002-08-07 18:27:04 +0000488 -O3 - Enable expensive optimizations</b>
Chris Lattnerae853632002-07-25 19:27:01 +0000489 -f - Overwrite output files
490 -help - display available options (--help-hidden for more)
491 -o &lt;filename&gt; - Specify output filename
492 -quiet - Don't print informational messages
493</pre>
Chris Lattner209c7f42001-07-23 23:03:12 +0000494
Chris Lattnerae853632002-07-25 19:27:01 +0000495In this case, it is sort of awkward that flag names correspond directly to enum
496names, because we probably don't want a enum definition named "<tt>g</tt>" in
497our program. Because of this, we can alternatively write this example like
498this:<p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000499
500<pre>
501enum OptLevel {
502 Debug, O1, O2, O3
503};
504
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000505<a href="#cl::opt">cl::opt</a>&lt;OptLevel&gt; OptimizationLevel(<a href="#cl::desc">cl::desc</a>("<i>Choose optimization level:</i>"),
506 <a href="#cl::values">cl::values</a>(
Chris Lattnerae853632002-07-25 19:27:01 +0000507 clEnumValN(Debug, "g", "<i>No optimizations, enable debugging</i>"),
508 clEnumVal(O1 , "<i>Enable trivial optimizations</i>"),
509 clEnumVal(O2 , "<i>Enable default optimizations</i>"),
510 clEnumVal(O3 , "<i>Enable expensive optimizations</i>"),
511 0));
Chris Lattner209c7f42001-07-23 23:03:12 +0000512
513...
514 if (OptimizationLevel == Debug) outputDebugInfo(...);
515...
516</pre><p>
517
Chris Lattnerae853632002-07-25 19:27:01 +0000518By using the "<tt>clEnumValN</tt>" macro instead of "<tt>clEnumVal</tt>", we can
519directly specify the name that the flag should get. In general a direct mapping
520is nice, but sometimes you can't or don't want to preserve the mapping, which is
521when you would use it.<p>
522
523
Chris Lattner209c7f42001-07-23 23:03:12 +0000524
525<!-- ======================================================================= -->
526</ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0><tr><td>&nbsp;</td><td width="100%">&nbsp; <font color="#EEEEFF" face="Georgia,Palatino"><b>
527<a name="namedalternatives">Named Alternatives
528</b></font></td></tr></table><ul>
529
Chris Lattnerae853632002-07-25 19:27:01 +0000530Another useful argument form is a named alternative style. We shall use this
531style in our compiler to specify different debug levels that can be used.
532Instead of each debug level being its own switch, we want to support the
533following options, of which only one can be specified at a time:
534"<tt>--debug-level=none</tt>", "<tt>--debug-level=quick</tt>",
535"<tt>--debug-level=detailed</tt>". To do this, we use the exact same format as
536our optimization level flags, but we also specify an option name. For this
537case, the code looks like this:<p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000538
539<pre>
540enum DebugLev {
541 nodebuginfo, quick, detailed
542};
543
544// Enable Debug Options to be specified on the command line
Chris Lattnerdc844fa2003-06-03 04:40:06 +0000545<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 +0000546 <a href="#cl::values">cl::values</a>(
Chris Lattnerae853632002-07-25 19:27:01 +0000547 clEnumValN(nodebuginfo, "none", "<i>disable debug information</i>"),
548 clEnumVal(quick, "<i>enable quick debug information</i>"),
549 clEnumVal(detailed, "<i>enable detailed debug information</i>"),
550 0));
Chris Lattner209c7f42001-07-23 23:03:12 +0000551</pre>
552
Chris Lattnerae853632002-07-25 19:27:01 +0000553This definition defines an enumerated command line variable of type "<tt>enum
554DebugLev</tt>", which works exactly the same way as before. The difference here
555is just the interface exposed to the user of your program and the help output by
556the "<tt>--help</tt>" option:<p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000557
558<pre>
Chris Lattnerae853632002-07-25 19:27:01 +0000559USAGE: compiler [options] &lt;input file&gt;
560
Chris Lattner209c7f42001-07-23 23:03:12 +0000561OPTIONS:
Chris Lattnerae853632002-07-25 19:27:01 +0000562 Choose optimization level:
563 -g - No optimizations, enable debugging
564 -O1 - Enable trivial optimizations
565 -O2 - Enable default optimizations
566 -O3 - Enable expensive optimizations
Chris Lattnerc1ae40c2002-08-07 18:27:04 +0000567 <b>-debug_level - Set the debugging level:
Chris Lattnerae853632002-07-25 19:27:01 +0000568 =none - disable debug information
569 =quick - enable quick debug information
Chris Lattnerc1ae40c2002-08-07 18:27:04 +0000570 =detailed - enable detailed debug information</b>
Chris Lattnerae853632002-07-25 19:27:01 +0000571 -f - Overwrite output files
572 -help - display available options (--help-hidden for more)
573 -o &lt;filename&gt; - Specify output filename
574 -quiet - Don't print informational messages
Chris Lattner209c7f42001-07-23 23:03:12 +0000575</pre><p>
576
Chris Lattnerae853632002-07-25 19:27:01 +0000577Again, the only structural difference between the debug level declaration and
578the optimiation level declaration is that the debug level declaration includes
579an option name (<tt>"debug_level"</tt>), which automatically changes how the
580library processes the argument. The CommandLine library supports both forms so
581that you can choose the form most appropriate for your application.<p>
582
Chris Lattner209c7f42001-07-23 23:03:12 +0000583
584
585<!-- ======================================================================= -->
586</ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0><tr><td>&nbsp;</td><td width="100%">&nbsp; <font color="#EEEEFF" face="Georgia,Palatino"><b>
Chris Lattnerae853632002-07-25 19:27:01 +0000587<a name="list">Parsing a list of options
Chris Lattner209c7f42001-07-23 23:03:12 +0000588</b></font></td></tr></table><ul>
589
Chris Lattnerae853632002-07-25 19:27:01 +0000590Now that we have the standard run of the mill argument types out of the way,
591lets get a little wild and crazy. Lets say that we want our optimizer to accept
592a <b>list</b> of optimizations to perform, allowing duplicates. For example, we
593might want to run: "<tt>compiler -dce -constprop -inline -dce -strip</tt>". In
594this case, the order of the arguments and the number of appearances is very
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000595important. This is what the "<tt><a href="#cl::list">cl::list</a></tt>"
596template is for. First, start by defining an enum of the optimizations that you
597would like to perform:<p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000598
599<pre>
600enum Opts {
Chris Lattnerae853632002-07-25 19:27:01 +0000601 // 'inline' is a C++ keyword, so name it 'inlining'
Chris Lattner209c7f42001-07-23 23:03:12 +0000602 dce, constprop, inlining, strip
Chris Lattnerae853632002-07-25 19:27:01 +0000603};
Chris Lattner209c7f42001-07-23 23:03:12 +0000604</pre><p>
605
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000606Then define your "<tt><a href="#cl::list">cl::list</a></tt>" variable:<p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000607
608<pre>
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000609<a href="#cl::list">cl::list</a>&lt;Opts&gt; OptimizationList(<a href="#cl::desc">cl::desc</a>("<i>Available Optimizations:</i>"),
610 <a href="#cl::values">cl::values</a>(
Chris Lattnerae853632002-07-25 19:27:01 +0000611 clEnumVal(dce , "<i>Dead Code Elimination</i>"),
Misha Brukman82c89b92003-05-20 21:01:22 +0000612 clEnumVal(constprop , "<i>Constant Propagation</i>"),
Chris Lattnerae853632002-07-25 19:27:01 +0000613 clEnumValN(inlining, "<i>inline</i>", "<i>Procedure Integration</i>"),
614 clEnumVal(strip , "<i>Strip Symbols</i>"),
615 0));
Chris Lattner209c7f42001-07-23 23:03:12 +0000616</pre><p>
617
Chris Lattnerae853632002-07-25 19:27:01 +0000618This defines a variable that is conceptually of the type
619"<tt>std::vector&lt;enum Opts&gt;</tt>". Thus, you can access it with standard
620vector methods:<p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000621
622<pre>
Chris Lattnerae853632002-07-25 19:27:01 +0000623 for (unsigned i = 0; i != OptimizationList.size(); ++i)
Chris Lattner209c7f42001-07-23 23:03:12 +0000624 switch (OptimizationList[i])
625 ...
626</pre>
627
Chris Lattnerae853632002-07-25 19:27:01 +0000628... to iterate through the list of options specified.<p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000629
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000630Note that the "<tt><a href="#cl::list">cl::list</a></tt>" template is completely general and may be used
Chris Lattnerae853632002-07-25 19:27:01 +0000631with any data types or other arguments that you can use with the
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000632"<tt><a href="#cl::opt">cl::opt</a></tt>" template. One especially useful way to use a list is to
Chris Lattnerae853632002-07-25 19:27:01 +0000633capture all of the positional arguments together if there may be more than one
634specified. In the case of a linker, for example, the linker takes several
635'<tt>.o</tt>' files, and needs to capture them into a list. This is naturally
636specified as:<p>
Chris Lattner3e5fe172002-04-13 18:35:59 +0000637
638<pre>
639...
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000640<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 +0000641...
642</pre><p>
643
Chris Lattnerae853632002-07-25 19:27:01 +0000644This variable works just like a "<tt>vector&lt;string&gt;</tt>" object. As
645such, accessing the list is simple, just like above. In this example, we used
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000646the <tt><a href="#cl::OneOrMore">cl::OneOrMore</a></tt> modifier to inform the
647CommandLine library that it is an error if the user does not specify any
648<tt>.o</tt> files on our command line. Again, this just reduces the amount of
649checking we have to do.<p>
Chris Lattner3e5fe172002-04-13 18:35:59 +0000650
Chris Lattner3e5fe172002-04-13 18:35:59 +0000651
652
Chris Lattnerc1ae40c2002-08-07 18:27:04 +0000653<!-- ======================================================================= -->
654</ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0><tr><td>&nbsp;</td><td width="100%">&nbsp; <font color="#EEEEFF" face="Georgia,Palatino"><b>
655<a name="description">Adding freeform text to help output
656</b></font></td></tr></table><ul>
657
658As our program grows and becomes more mature, we may decide to put summary
659information about what it does into the help output. The help output is styled
660to look similar to a Unix <tt>man</tt> page, providing concise information about
661a program. Unix <tt>man</tt> pages, however often have a description about what
662the program does. To add this to your CommandLine program, simply pass a third
663argument to the <a
664href="#cl::ParseCommandLineOptions"><tt>cl::ParseCommandLineOptions</tt></a>
665call in main. This additional argument is then printed as the overview
666information for your program, allowing you to include any additional information
667that you want. For example:<p>
668
669<pre>
670int main(int argc, char **argv) {
671 <a href="#cl::ParseCommandLineOptions">cl::ParseCommandLineOptions</a>(argc, argv, " CommandLine compiler example\n\n"
672 " This program blah blah blah...\n");
673 ...
674}
675</pre><p>
676
677Would yield the help output:
678
679<pre>
680<b>OVERVIEW: CommandLine compiler example
681
682 This program blah blah blah...</b>
683
684USAGE: compiler [options] &lt;input file&gt;
685
686OPTIONS:
687 ...
688 -help - display available options (--help-hidden for more)
689 -o &lt;filename&gt; - Specify output filename
690</pre><p>
691
692
693
694
Chris Lattner209c7f42001-07-23 23:03:12 +0000695<!-- *********************************************************************** -->
696</ul><table width="100%" bgcolor="#330077" border=0 cellpadding=4 cellspacing=0><tr><td align=center><font color="#EEEEFF" size=+2 face="Georgia,Palatino"><b>
697<a name="referenceguide">Reference Guide
698</b></font></td></tr></table><ul>
699<!-- *********************************************************************** -->
700
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000701Now that you know the basics of how to use the CommandLine library, this section
702will give you the detailed information you need to tune how command line options
703work, as well as information on more "advanced" command line option processing
704capabilities.<p>
705
706
707<!-- ======================================================================= -->
708</ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0><tr><td>&nbsp;</td><td width="100%">&nbsp; <font color="#EEEEFF" face="Georgia,Palatino"><b>
709<a name="positional">Positional Arguments
710</b></font></td></tr></table><ul>
711
712Positional arguments are those arguments that are not named, and are not
713specified with a hyphen. Positional arguments should be used when an option is
714specified by its position alone. For example, the standard Unix <tt>grep</tt>
715tool takes a regular expression argument, and an optional filename to search
716through (which defaults to standard input if a filename is not specified).
717Using the CommandLine library, this would be specified as:<p>
718
719<pre>
720<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>);
721<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>"));
722</pre>
723
724Given these two option declarations, the <tt>--help</tt> output for our grep
725replacement would look like this:<p>
726
727<pre>
Chris Lattnerc1ae40c2002-08-07 18:27:04 +0000728USAGE: spiffygrep [options] <b>&lt;regular expression&gt; &lt;input file&gt;</b>
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000729
730OPTIONS:
731 -help - display available options (--help-hidden for more)
732</pre>
733
734... and the resultant program could be used just like the standard <tt>grep</tt>
735tool.<p>
736
737Positional arguments are sorted by their order of construction. This means that
738command line options will be ordered according to how they are listed in a .cpp
739file, but will not have an ordering defined if they positional arguments are
740defined in multiple .cpp files. The fix for this problem is simply to define
741all of your positional arguments in one .cpp file.<p>
742
743
744
745<!-- _______________________________________________________________________ -->
746</ul><a name="--"><h4><hr size=0>Specifying positional options with hyphens</h4><ul>
747
748Sometimes you may want to specify a value to your positional argument that
749starts with a hyphen (for example, searching for '<tt>-foo</tt>' in a file). At
750first, you will have trouble doing this, because it will try to find an argument
751named '<tt>-foo</tt>', and will fail (and single quotes will not save you).
752Note that the system <tt>grep</tt> has the same problem:<p>
753
754<pre>
755 $ spiffygrep '-foo' test.txt
756 Unknown command line argument '-foo'. Try: spiffygrep --help'
757
758 $ grep '-foo' test.txt
759 grep: illegal option -- f
760 grep: illegal option -- o
761 grep: illegal option -- o
762 Usage: grep -hblcnsviw pattern file . . .
763</pre><p>
764
765The solution for this problem is the same for both your tool and the system
766version: use the '<tt>--</tt>' marker. When the user specifies '<tt>--</tt>' on
767the command line, it is telling the program that all options after the
768'<tt>--</tt>' should be treated as positional arguments, not options. Thus, we
769can use it like this:<p>
770
771<pre>
772 $ spiffygrep -- -foo test.txt
773 ...output...
774</pre><p>
775
776
777
778<!-- _______________________________________________________________________ -->
779</ul><a name="cl::ConsumeAfter"><h4><hr size=0>The <tt>cl::ConsumeAfter</tt> modifier</h4><ul>
780
781The <tt>cl::ConsumeAfter</tt> <a href="#formatting">formatting option</a> is
782used to construct programs that use "interpreter style" option processing. With
783this style of option processing, all arguments specified after the last
784positional argument are treated as special interpreter arguments that are not
785interpreted by the command line argument.<p>
786
787As a concrete example, lets say we are developing a replacement for the standard
788Unix Bourne shell (<tt>/bin/sh</tt>). To run <tt>/bin/sh</tt>, first you
789specify options to the shell itself (like <tt>-x</tt> which turns on trace
790output), then you specify the name of the script to run, then you specify
791arguments to the script. These arguments to the script are parsed by the bourne
792shell command line option processor, but are not interpreted as options to the
793shell itself. Using the CommandLine library, we would specify this as:<p>
794
795<pre>
796<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>("-"));
797<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>"));
798<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>"));
799</pre><p>
800
801which automatically provides the help output:<p>
802
803<pre>
Chris Lattnerc1ae40c2002-08-07 18:27:04 +0000804USAGE: spiffysh [options] <b>&lt;input script&gt; &lt;program arguments&gt;...</b>
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000805
806OPTIONS:
807 -help - display available options (--help-hidden for more)
Chris Lattnerc1ae40c2002-08-07 18:27:04 +0000808 <b>-x - Enable trace output</b>
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000809</pre><p>
810
811At runtime, if we run our new shell replacement as '<tt>spiffysh -x test.sh -a
812-x -y bar</tt>', the <tt>Trace</tt> variable will be set to true, the
813<tt>Script</tt> variable will be set to "<tt>test.sh</tt>", and the
814<tt>Argv</tt> list will contain <tt>["-a", "-x", "-y", "bar"]</tt>, because
815they were specified after the last positional argument (which is the script
816name).<p>
817
818There are several limitations to when <tt>cl::ConsumeAfter</tt> options can be
819specified. For example, only one <tt>cl::ConsumeAfter</tt> can be specified per
820program, there must be at least one <a href="#positional">positional
821argument</a> specified, and the <tt>cl::ConsumeAfter</tt> option should be a <a
822href="#cl::list">cl::list</a> option.<p>
823
824
825
826<!-- ======================================================================= -->
827</ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0><tr><td>&nbsp;</td><td width="100%">&nbsp; <font color="#EEEEFF" face="Georgia,Palatino"><b>
828<a name="storage">Internal vs External Storage
829</b></font></td></tr></table><ul>
830
831By default, all command line options automatically hold the value that they
832parse from the command line. This is very convenient in the common case,
833especially when combined with the ability to define command line options in the
834files that use them. This is called the internal storage model.<p>
835
836Sometimes, however, it is nice to separate the command line option processing
837code from the storage of the value parsed. For example, lets say that we have a
838'<tt>-debug</tt>' option that we would like to use to enable debug information
839across the entire body of our program. In this case, the boolean value
840controlling the debug code should be globally accessable (in a header file, for
841example) yet the command line option processing code should not be exposed to
842all of these clients (requiring lots of .cpp files to #include
843<tt>CommandLine.h</tt>).<p>
844
845To do this, set up your .h file with your option, like this for example:<p>
846
847<pre>
848<i>// DebugFlag.h - Get access to the '-debug' command line option
849//
850
851// DebugFlag - This boolean is set to true if the '-debug' command line option
852// is specified. This should probably not be referenced directly, instead, use
853// the DEBUG macro below.
854//</i>
855extern bool DebugFlag;
856
857<i>// DEBUG macro - This macro should be used by code to emit debug information.
858// In the '-debug' option is specified on the command line, and if this is a
859// debug build, then the code specified as the option to the macro will be
860// executed. Otherwise it will not be. Example:
861//
862// DEBUG(cerr << "Bitset contains: " << Bitset << "\n");
863//</i>
864<font color=red>#ifdef NDEBUG
865#define DEBUG(X)
866#else
867#define DEBUG(X)</font> \
868 do { if (DebugFlag) { X; } } while (0)
869<font color=red>#endif</font>
870</pre>
871
872This allows clients to blissfully use the <tt>DEBUG()</tt> macro, or the
873<tt>DebugFlag</tt> explicitly if they want to. Now we just need to be able to
874set the <tt>DebugFlag</tt> boolean when the option is set. To do this, we pass
875an additial argument to our command line argument processor, and we specify
876where to fill in with the <a href="#cl::location">cl::location</a> attribute:<p>
877
878<pre>
879bool DebugFlag; <i>// the actual value</i>
Chris Lattner589a4cc2003-08-01 21:30:37 +0000880static <a href="#cl::opt">cl::opt</a>&lt;bool, true&gt; <i>// The parser</i>
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000881Debug("<i>debug</i>", <a href="#cl::desc">cl::desc</a>("<i>Enable debug output</i>")</a>, <a href="#cl::Hidden">cl::Hidden</a>,
882 <a href="#cl::location">cl::location</a>(DebugFlag));
883</pre>
884
885In the above example, we specify "<tt>true</tt>" as the second argument to the
886<a href="#cl::opt">cl::opt</a> template, indicating that the template should not
887maintain a copy of the value itself. In addition to this, we specify the <a
888href="#cl::location">cl::location</a> attribute, so that <tt>DebugFlag</tt> is
889automatically set.<p>
890
891
892
893<!-- ======================================================================= -->
894</ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0><tr><td>&nbsp;</td><td width="100%">&nbsp; <font color="#EEEEFF" face="Georgia,Palatino"><b>
895<a name="attributes">Option Attributes
896</b></font></td></tr></table><ul>
897
898This section describes the basic attributes that you can specify on options.<p>
899
900<ul>
901
902<li>The option name attribute (which is required for all options, except <a
903href="#positional">positional options</a>) specifies what the option name is.
904This option is specified in simple double quotes:<p>
905
Chris Lattnerc1ae40c2002-08-07 18:27:04 +0000906<pre>
907<a href="#cl::opt">cl::opt</a>&lt;<b>bool</b>&gt; Quiet("<i>quiet</i>");
908</pre><p>
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000909
910<li><a name="cl::desc">The <b><tt>cl::desc</tt></b> attribute specifies a
911description for the option to be shown in the <tt>--help</tt> output for the
912program.<p>
913
914<li><a name="cl::value_desc">The <b><tt>cl::value_desc</tt></b> attribute
915specifies a string that can be used to fine tune the <tt>--help</tt> output for
916a command line option. Look <a href="#value_desc_example">here</a> for an
917example.<p>
918
919<li><a name="cl::init">The <b><tt>cl::init</tt></b> attribute specifies an
920inital value for a <a href="#cl::opt">scalar</a> option. If this attribute is
921not specified then the command line option value defaults to the value created
922by the default constructor for the type.<p>
923
924<li><a name="cl::location">The <b><tt>cl::location</tt></b> attribute where to
925store the value for a parsed command line option if using external storage. See
926the section on <a href="#storage">Internal vs External Storage</a> for more
927information.<p>
928
929<li><a name="cl::aliasopt">The <b><tt>cl::aliasopt</tt></b> attribute specifies
930which option a <a href="#cl::alias">cl::alias</a> option is an alias for.<p>
931
932<li><a name="cl::values">The <b><tt>cl::values</tt></b> attribute specifies the
933string-to-value mapping to be used by the generic parser. It takes a <b>null
934terminated</b> list of (option, value, description) triplets that specify the
935option name, the value mapped to, and the description shown in the
936<tt>--help</tt> for the tool. Because the generic parser is used most frequently with enum values, two macros are often useful:<p>
937<ol>
938<li><a name="clEnumVal">The <b><tt>clEnumVal</tt></b> macro is used as a nice
939simple way to specify a triplet for an enum. This macro automatically makes the
940option name be the same as the enum name. The first option to the macro is the
941enum, the second is the description for the command line option.<p> <li><a
942name="clEnumValN">The <b><tt>clEnumValN</tt></b> macro is used to specify macro
943options where the option name doesn't equal the enum name. For this macro, the
944first argument is the enum value, the second is the flag name, and the second is
945the description.<p>
946</ol>
947
948You will get a compile time error if you try to use cl::values with a parser
949that does not support it.<p>
950
951</ul>
952
953
954
955<!-- ======================================================================= -->
956</ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0><tr><td>&nbsp;</td><td width="100%">&nbsp; <font color="#EEEEFF" face="Georgia,Palatino"><b>
957<a name="modifiers">Option Modifiers
958</b></font></td></tr></table><ul>
959
960Option modifiers are the flags and expressions that you pass into the
961constructors for <tt><a href="#cl::opt">cl::opt</a></tt> and <tt><a
962href="#cl::list">cl::list</a></tt>. These modifiers give you the ability to
963tweak how options are parsed and how <tt>--help</tt> output is generated to fit
964your application well.<p>
965
Chris Lattner32a32842003-05-22 20:36:06 +0000966These options fall into five main catagories:<p>
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000967
968<ol>
969<li><a href="#hiding">Hiding an option from <tt>--help</tt> output</a>
970<li><a href="#numoccurances">Controlling the number of occurances
971 required and allowed</a>
972<li><a href="#valrequired">Controlling whether or not a value must be
973 specified</a>
974<li><a href="#formatting">Controlling other formatting options</a>
Chris Lattner32a32842003-05-22 20:36:06 +0000975<li><a href="#misc">Miscellaneous option modifiers</a>
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000976</ol><p>
977
978It is not possible to specify two options from the same catagory (you'll get a
Chris Lattner32a32842003-05-22 20:36:06 +0000979runtime error) to a single option, except for options in the miscellaneous
980catagory. The CommandLine library specifies defaults for all of these settings
981that are the most useful in practice and the most common, which mean that you
982usually shouldn't have to worry about these.<p>
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000983
984
985<!-- _______________________________________________________________________ -->
986</ul><a name="hiding"><h4><hr size=0>Hiding an option from <tt>--help</tt> output</h4><ul>
987
988The <tt>cl::NotHidden</tt>, <tt>cl::Hidden</tt>, and <tt>cl::ReallyHidden</tt>
989modifiers are used to control whether or not an option appears in the
990<tt>--help</tt> and <tt>--help-hidden</tt> output for the compiled program:<p>
991
992<ul>
993
994<a name="cl::NotHidden">The <b><tt>cl::NotHidden</tt></b> modifier (which is the
995default for <tt><a href="#cl::opt">cl::opt</a></tt> and <tt><a
996href="#cl::list">cl::list</a></tt> options), indicates the option is to appear
997in both help listings.<p>
998
999<a name="cl::Hidden">The <b><tt>cl::Hidden</tt></b> modifier (which is the
1000default for <tt><a href="#cl::alias">cl::alias</a></tt> options), indicates that
1001the option should not appear in the <tt>--help</tt> output, but should appear in
1002the <tt>--help-hidden</tt> output.<p>
1003
1004<a name="cl::ReallyHidden">The <b><tt>cl::ReallyHidden</tt></b> modifier,
1005indicates that the option should not appear in any help output.<p>
1006</ul>
1007
1008<!-- _______________________________________________________________________ -->
1009</ul><a name="numoccurances"><h4><hr size=0>Controlling the number of occurances required and allowed</h4><ul>
1010
1011This group of options is used to control how many time an option is allowed (or
1012required) to be specified on the command line of your program. Specifying a
1013value for this setting allows the CommandLine library to do error checking for
1014you.<p>
1015
1016The allowed values for this option group are:<p>
1017
1018<ul>
1019<a name="cl::Optional">The <b><tt>cl::Optional</tt></b> modifier (which is the
1020default for the <tt><a href="#cl::opt">cl::opt</a></tt> and <tt><a
1021href="#cl::alias">cl::alias</a></tt> classes) indicates that your program will
1022allow either zero or one occurance of the option to be specified.<p>
1023
1024<a name="cl::ZeroOrMore">The <b><tt>cl::ZeroOrMore</tt></b> modifier (which is
1025the default for the <tt><a href="#cl::list">cl::list</a></tt> class) indicates
1026that your program will allow the option to be specified zero or more times.<p>
1027
1028<a name="cl::Required">The <b><tt>cl::Required</tt></b> modifier indicates that
1029the specified option must be specified exactly one time.<p>
1030
1031<a name="cl::OneOrMore">The <b><tt>cl::OneOrMore</tt></b> modifier indicates
1032that the option must be specified at least one time.<p>
1033
1034The <b><tt>cl::ConsumeAfter</tt></b> modifier is described in the <a
1035href="#positional">Positional arguments section</a><p>
1036
1037</ul>
1038
1039If an option is not specified, then the value of the option is equal to the
1040value specified by the <tt><a href="#cl::init">cl::init</a></tt> attribute. If
1041the <tt><a href="#cl::init">cl::init</a></tt> attribute is not specified, the
1042option value is initialized with the default constructor for the data type.<p>
1043
1044If an option is specified multiple times for an option of the <tt><a
1045href="#cl::opt">cl::opt</a></tt> class, only the last value will be retained.<p>
1046
1047
1048<!-- _______________________________________________________________________ -->
1049</ul><a name="valrequired"><h4><hr size=0>Controlling whether or not a value must be specified</h4><ul>
1050
1051This group of options is used to control whether or not the option allows a
1052value to be present. In the case of the CommandLine library, a value is either
1053specified with an equal sign (e.g. '<tt>-index-depth=17</tt>') or as a trailing
1054string (e.g. '<tt>-o a.out</tt>').<p>
1055
1056The allowed values for this option group are:<p>
1057
1058<ul>
1059<a name="cl::ValueOptional">The <b><tt>cl::ValueOptional</tt></b> modifier
1060(which is the default for <tt>bool</tt> typed options) specifies that it is
1061acceptable to have a value, or not. A boolean argument can be enabled just by
1062appearing on the command line, or it can have an explicit '<tt>-foo=true</tt>'.
1063If an option is specified with this mode, it is illegal for the value to be
1064provided without the equal sign. Therefore '<tt>-foo true</tt>' is illegal. To
1065get this behavior, you must use the <a
1066href="#cl::ValueRequired">cl::ValueRequired</a> modifier.<p>
1067
1068<a name="cl::ValueRequired">The <b><tt>cl::ValueRequired</tt></b> modifier
1069(which is the default for all other types except for <a
1070href="#onealternative">unnamed alternatives using the generic parser</a>)
1071specifies that a value must be provided. This mode informs the command line
1072library that if an option is not provides with an equal sign, that the next
1073argument provided must be the value. This allows things like '<tt>-o
1074a.out</tt>' to work.<p>
1075
1076<a name="cl::ValueDisallowed">The <b><tt>cl::ValueDisallowed</tt></b> modifier
1077(which is the default for <a href="#onealternative">unnamed alternatives using
1078the generic parser</a>) indicates that it is a runtime error for the user to specify a value. This can be provided to disallow users from providing options to boolean options (like '<tt>-foo=true</tt>').<p>
1079
1080</ul>
1081
1082In general, the default values for this option group work just like you would
1083want them to. As mentioned above, you can specify the <a
1084href="#cl::ValueDisallowed">cl::ValueDisallowed</a> modifier to a boolean
1085argument to restrict your command line parser. These options are mostly useful
1086when <a href="#extensionguide">extending the library</a>.<p>
1087
1088
1089
1090<!-- _______________________________________________________________________ -->
1091</ul><a name="formatting"><h4><hr size=0>Controlling other formatting options</h4><ul>
1092
1093The formatting option group is used to specify that the command line option has
1094special abilities and is otherwise different from other command line arguments.
1095As usual, you can only specify at most one of these arguments.<p>
1096
1097<ul>
1098<a name="cl::NormalFormatting">The <b><tt>cl::NormalFormatting</tt></b> modifier
1099(which is the default all options) specifies that this option is "normal".<p>
1100
1101<a name="cl::Positional">The <b><tt>cl::Positional</tt></b> modifier specifies
1102that this is a positional argument, that does not have a command line option
1103associated with it. See the <a href="#positional">Positional Arguments</a>
1104section for more information.<p>
1105
1106The <b><a href="#cl::ConsumeAfter"><tt>cl::ConsumeAfter</tt></a></b> modifier
1107specifies that this option is used to capture "interpreter style" arguments. See <a href="#cl::ConsumeAfter">this section for more information</a>.<p>
1108
1109
1110<a name="cl::Prefix">The <b><tt>cl::Prefix</tt></b> modifier specifies that this
1111option prefixes its value. With 'Prefix' options, there is no equal sign that
Misha Brukmanbc0e9982003-07-14 17:20:40 +00001112separates the value from the option name specified. This is useful for
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001113processing odd arguments like '<tt>-lmalloc -L/usr/lib'</tt> in a linker tool.
1114Here, the '<tt>l</tt>' and '<tt>L</tt>' options are normal string (list)
1115options, that have the <a href="#cl::Prefix">cl::Prefix</a> modifier added to
1116allow the CommandLine library to recognize them. Note that <a
1117href="#cl::Prefix">cl::Prefix</a> options must not have the <a
1118href="#cl::ValueDisallowed">cl::ValueDisallowed</a> modifier specified.<p>
1119
1120<a name="cl::Grouping">The <b><tt>cl::Grouping</tt></b> modifier is used to
1121implement unix style tools (like <tt>ls</tt>) that have lots of single letter
1122arguments, but only require a single dash. For example, the '<tt>ls -labF</tt>'
1123command actually enables four different options, all of which are single
1124letters. Note that <a href="#cl::Grouping">cl::Grouping</a> options cannot have
1125values.<p>
1126
1127</ul>
1128
1129The CommandLine library does not restrict how you use the <a
1130href="#cl::Prefix">cl::Prefix</a> or <a href="#cl::Grouping">cl::Grouping</a>
1131modifiers, but it is possible to specify ambiguous argument settings. Thus, it
1132is possible to have multiple letter options that are prefix or grouping options,
1133and they will still work as designed.<p>
1134
1135To do this, the CommandLine library uses a greedy algorithm to parse the input
1136option into (potentially multiple) prefix and grouping options. The strategy
1137basically looks like this:<p>
1138
1139<tt>parse(string OrigInput) {</tt>
1140<ol>
1141<li><tt>string input = OrigInput;</tt>
1142<li><tt>if (isOption(input)) return getOption(input).parse();</tt>&nbsp;&nbsp;&nbsp;&nbsp;<i>// Normal option</i>
1143<li><tt>while (!isOption(input) &amp;&amp; !input.empty()) input.pop_back();</tt>&nbsp;&nbsp;&nbsp;&nbsp;<i>// Remove the last letter</i>
1144<li><tt>if (input.empty()) return error();</tt>&nbsp;&nbsp;&nbsp;&nbsp;<i>// No matching option</i>
1145<li><tt>if (getOption(input).isPrefix())<br>
1146&nbsp;&nbsp;return getOption(input).parse(input);</tt>
1147<li><tt>while (!input.empty()) {&nbsp;&nbsp;&nbsp;&nbsp;<i>// Must be grouping options</i><br>
1148&nbsp;&nbsp;getOption(input).parse();<br>
1149&nbsp;&nbsp;OrigInput.erase(OrigInput.begin(), OrigInput.begin()+input.length());<br>
1150&nbsp;&nbsp;input = OrigInput;<br>
1151&nbsp;&nbsp;while (!isOption(input) &amp;&amp; !input.empty()) input.pop_back();<br>
1152}</tt>
1153<li><tt>if (!OrigInput.empty()) error();</tt>
1154</tt>
1155
1156</ol>
1157<tt>}</tt><p>
1158
1159
Chris Lattner32a32842003-05-22 20:36:06 +00001160
1161<!-- _______________________________________________________________________ -->
1162</ul><a name="misc"><h4><hr size=0>Miscellaneous option modifiers</h4><ul>
1163
1164The miscellaneous option modifiers are the only flags where you can specify more
1165than one flag from the set: they are not mutually exclusive. These flags
1166specify boolean properties that modify the option.<p>
1167
1168<ul>
1169
1170<a name="cl::CommaSeparated">The <b><tt>cl::CommaSeparated</tt></b> modifier
1171indicates that any commas specified for an option's value should be used to
1172split the value up into multiple values for the option. For example, these two
1173options are equivalent when <tt>cl::CommaSeparated</tt> is specified:
1174"<tt>-foo=a -foo=b -foo=c</tt>" and "<tt>-foo=a,b,c</tt>". This option only
1175makes sense to be used in a case where the option is allowed to accept one or
1176more values (i.e. it is a <a href="#cl::list">cl::list</a> option).<p>
1177</ul>
1178
1179So far, the only miscellaneous option modifier is the
1180<tt>cl::CommaSeparated</tt> modifier.<p>
1181
1182
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001183<!-- ======================================================================= -->
1184</ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0><tr><td>&nbsp;</td><td width="100%">&nbsp; <font color="#EEEEFF" face="Georgia,Palatino"><b>
Chris Lattnerc1ae40c2002-08-07 18:27:04 +00001185<a name="toplevel">Top-Level Classes and Functions
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001186</b></font></td></tr></table><ul>
1187
1188Despite all of the builtin flexibility, the CommandLine option library really
Chris Lattnerc1ae40c2002-08-07 18:27:04 +00001189only consists of one function (<a
1190href="#cl::ParseCommandLineOptions"><tt>cl::ParseCommandLineOptions</tt></a>)
1191and three main classes: <a href="#cl::opt"><tt>cl::opt</tt></a>, <a
1192href="#cl::list"><tt>cl::list</tt></a>, and <a
1193href="#cl::alias"><tt>cl::alias</tt></a>. This section describes these three
1194classes in detail.<p>
1195
1196<!-- _______________________________________________________________________ -->
1197</ul><a name="cl::ParseCommandLineOptions"><h4><hr size=0>The
1198<tt>cl::ParseCommandLineOptions</tt> function</h4><ul>
1199
1200The <tt>cl::ParseCommandLineOptions</tt> function is designed to be called
1201directly from <tt>main</tt>, and is used to fill in the values of all of the
1202command line option variables once <tt>argc</tt> and <tt>argv</tt> are
1203available.<p>
1204
1205The <tt>cl::ParseCommandLineOptions</tt> function requires two parameters
1206(<tt>argc</tt> and <tt>argv</tt>), but may also take an optional third parameter
1207which holds <a href="#description">additional extra text</a> to emit when the
1208<tt>--help</tt> option is invoked.<p>
1209
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001210
1211<!-- _______________________________________________________________________ -->
1212</ul><a name="cl::opt"><h4><hr size=0>The <tt>cl::opt</tt> class</h4><ul>
1213
1214The <tt>cl::opt</tt> class is the class used to represent scalar command line
1215options, and is the one used most of the time. It is a templated class which
1216can take up to three arguments (all except for the first have default values
1217though):<p>
1218
1219<pre>
Chris Lattnerc1ae40c2002-08-07 18:27:04 +00001220<b>namespace</b> cl {
1221 <b>template</b> &lt;<b>class</b> DataType, <b>bool</b> ExternalStorage = <b>false</b>,
1222 <b>class</b> ParserClass = parser&lt;DataType&gt; &gt;
1223 <b>class</b> opt;
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001224}
1225</pre><p>
1226
1227The first template argument specifies what underlying data type the command line
1228argument is, and is used to select a default parser implementation. The second
1229template argument is used to specify whether the option should contain the
1230storage for the option (the default) or whether external storage should be used
1231to contain the value parsed for the option (see <a href="#storage">Internal vs
1232External Storage</a> for more information).<p>
1233
1234The third template argument specifies which parser to use. The default value
1235selects an instantiation of the <tt>parser</tt> class based on the underlying
1236data type of the option. In general, this default works well for most
1237applications, so this option is only used when using a <a
1238href="#customparser">custom parser</a>.<p>
1239
1240
1241<!-- _______________________________________________________________________ -->
1242</ul><a name="cl::list"><h4><hr size=0>The <tt>cl::list</tt> class</h4><ul>
1243
1244The <tt>cl::list</tt> class is the class used to represent a list of command
1245line options. It too is a templated class which can take up to three
1246arguments:<p>
1247
1248<pre>
Chris Lattnerc1ae40c2002-08-07 18:27:04 +00001249<b>namespace</b> cl {
1250 <b>template</b> &lt;<b>class</b> DataType, <b>class</b> Storage = <b>bool</b>,
1251 <b>class</b> ParserClass = parser&lt;DataType&gt; &gt;
1252 <b>class</b> list;
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001253}
1254</pre><p>
1255
1256This class works the exact same as the <a href="#cl::opt"><tt>cl::opt</tt></a>
1257class, except that the second argument is the <b>type</b> of the external
1258storage, not a boolean value. For this class, the marker type '<tt>bool</tt>'
1259is used to indicate that internal storage should be used.<p>
1260
1261
1262<!-- _______________________________________________________________________ -->
1263</ul><a name="cl::alias"><h4><hr size=0>The <tt>cl::alias</tt> class</h4><ul>
1264
1265The <tt>cl::alias</tt> class is a nontemplated class that is used to form
1266aliases for other arguments.<p>
1267
1268<pre>
Chris Lattnerc1ae40c2002-08-07 18:27:04 +00001269<b>namespace</b> cl {
1270 <b>class</b> alias;
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001271}
1272</pre></p>
1273
1274The <a href="#cl::aliasopt"><tt>cl::aliasopt</tt></a> attribute should be used
1275to specify which option this is an alias for. Alias arguments default to being
1276<a href="#cl::Hidden">Hidden</a>, and use the aliased options parser to do the
1277conversion from string to data.<p>
1278
1279
1280<!-- ======================================================================= -->
1281</ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0><tr><td>&nbsp;</td><td width="100%">&nbsp; <font color="#EEEEFF" face="Georgia,Palatino"><b>
1282<a name="builtinparsers">Builtin parsers
1283</b></font></td></tr></table><ul>
1284
1285Parsers control how the string value taken from the command line is translated
1286into a typed value, suitable for use in a C++ program. By default, the
1287CommandLine library uses an instance of <tt>parser&lt;type&gt;</tt> if the
1288command line option specifies that it uses values of type '<tt>type</tt>'.
1289Because of this, custom option processing is specified with specializations of
1290the '<tt>parser</tt>' class.<p>
1291
1292The CommandLine library provides the following builtin parser specializations,
1293which are sufficient for most applications. It can, however, also be extended to
1294work with new data types and new ways of interpreting the same data. See the <a
1295href="#customparser">Writing a Custom Parser</a> for more details on this type
1296of library extension.<p>
1297
1298<li><a name="genericparser">The <b>generic <tt>parser&lt;t&gt;</tt> parser</b>
1299can be used to map strings values to any data type, through the use of the <a
1300href="#cl::values">cl::values</a> property, which specifies the mapping
1301information. The most common use of this parser is for parsing enum values,
1302which allows you to use the CommandLine library for all of the error checking to
1303make sure that only valid enum values are specified (as opposed to accepting
1304arbitrary strings). Despite this, however, the generic parser class can be used
1305for any data type.<p>
1306
1307<li><a name="boolparser">The <b><tt>parser&lt;bool&gt;</tt> specialization</b>
1308is used to convert boolean strings to a boolean value. Currently accepted
1309strings are "<tt>true</tt>", "<tt>TRUE</tt>", "<tt>True</tt>", "<tt>1</tt>",
1310"<tt>false</tt>", "<tt>FALSE</tt>", "<tt>False</tt>", and "<tt>0</tt>".<p>
1311
1312<li><a name="stringparser">The <b><tt>parser&lt;string&gt;</tt> specialization</b> simply stores the parsed string into the string value specified. No conversion or modification of the data is performed.<p>
1313
1314<li><a name="intparser">The <b><tt>parser&lt;int&gt;</tt> specialization</b>
1315uses the C <tt>strtol</tt> function to parse the string input. As such, it will
1316accept a decimal number (with an optional '+' or '-' prefix) which must start
1317with a non-zero digit. It accepts octal numbers, which are identified with a
1318'<tt>0</tt>' prefix digit, and hexadecimal numbers with a prefix of
1319'<tt>0x</tt>' or '<tt>0X</tt>'.<p>
1320
1321<li><a name="doubleparser">The <b><tt>parser&lt;double&gt;</tt></b> and
1322<b><tt>parser&lt;float&gt;</tt> specializations</b> use the standard C
1323<tt>strtod</tt> function to convert floating point strings into floating point
1324values. As such, a broad range of string formats is supported, including
1325exponential notation (ex: <tt>1.7e15</tt>) and properly supports locales.
1326<p>
1327
Chris Lattner209c7f42001-07-23 23:03:12 +00001328
1329
1330<!-- *********************************************************************** -->
1331</ul><table width="100%" bgcolor="#330077" border=0 cellpadding=4 cellspacing=0><tr><td align=center><font color="#EEEEFF" size=+2 face="Georgia,Palatino"><b>
1332<a name="extensionguide">Extension Guide
1333</b></font></td></tr></table><ul>
1334<!-- *********************************************************************** -->
1335
Chris Lattnerc1ae40c2002-08-07 18:27:04 +00001336Although the CommandLine library has a lot of functionality built into it
1337already (as discussed previously), one of its true strengths lie in its
1338extensibility. This section discusses how the CommandLine library works under
1339the covers and illustrates how to do some simple, common, extensions.<p>
1340
Chris Lattner209c7f42001-07-23 23:03:12 +00001341
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001342<!-- ======================================================================= -->
Chris Lattnerc1ae40c2002-08-07 18:27:04 +00001343</ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0>
1344<tr><td>&nbsp;</td><td width="100%">&nbsp; <font color="#EEEEFF"
1345face="Georgia,Palatino"><b> <a name="customparser">Writing a custom parser
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001346</b></font></td></tr></table><ul>
1347
Chris Lattnerc1ae40c2002-08-07 18:27:04 +00001348One of the simplest and most common extensions is the use of a custom parser.
1349As <a href="#builtinparsers">discussed previously</a>, parsers are the portion
1350of the CommandLine library that turns string input from the user into a
1351particular parsed data type, validating the input in the process.<p>
1352
1353There are two ways to use a new parser:<p>
1354
1355<ol>
1356<li>Specialize the <a href="#genericparser"><tt>cl::parser</tt></a> template for
1357 your custom data type.<p>
1358
1359 This approach has the advantage that users of your custom data type will
1360 automatically use your custom parser whenever they define an option with a
1361 value type of your data type. The disadvantage of this approach is that it
1362 doesn't work if your fundemental data type is something that is already
1363 supported.<p>
1364
1365<li>Write an independant class, using it explicitly from options that need
1366 it.<p>
1367
1368 This approach works well in situations where you would line to parse an
1369 option using special syntax for a not-very-special data-type. The drawback
1370 of this approach is that users of your parser have to be aware that they are
1371 using your parser, instead of the builtin ones.<p>
1372
1373</ol><p>
1374
1375To guide the discussion, we will discuss a custom parser that accepts file
1376sizes, specified with an optional unit after the numeric size. For example, we
1377would like to parse "102kb", "41M", "1G" into the appropriate integer value. In
1378this case, the underlying data type we want to parse into is
1379'<tt>unsigned</tt>'. We choose approach #2 above because we don't want to make
1380this the default for all <tt>unsigned</tt> options.<p>
1381
1382To start out, we declare our new <tt>FileSizeParser</tt> class:<p>
1383
1384<pre>
1385<b>struct</b> FileSizeParser : <b>public</b> cl::basic_parser&lt;<b>unsigned</b>&gt; {
1386 <i>// parse - Return true on error.</i>
1387 <b>bool</b> parse(cl::Option &amp;O, <b>const char</b> *ArgName, <b>const</b> std::string &amp;ArgValue,
1388 <b>unsigned</b> &amp;Val);
1389};
1390</pre><p>
1391
1392Our new class inherits from the <tt>cl::basic_parser</tt> template class to fill
1393in the default, boiler plate, code for us. We give it the data type that we
1394parse into (the last argument to the <tt>parse</tt> method so that clients of
1395our custom parser know what object type to pass in to the parse method (here we
1396declare that we parse into '<tt>unsigned</tt>' variables.<p>
1397
1398For most purposes, the only method that must be implemented in a custom parser
1399is the <tt>parse</tt> method. The <tt>parse</tt> method is called whenever the
1400option is invoked, passing in the option itself, the option name, the string to
1401parse, and a reference to a return value. If the string to parse is not well formed, the parser should output an error message and return true. Otherwise it should return false and set '<tt>Val</tt>' to the parsed value. In our example, we implement <tt>parse</tt> as:<p>
1402
1403<pre>
1404<b>bool</b> FileSizeParser::parse(cl::Option &amp;O, <b>const char</b> *ArgName,
1405 <b>const</b> std::string &amp;Arg, <b>unsigned</b> &amp;Val) {
1406 <b>const char</b> *ArgStart = Arg.c_str();
1407 <b>char</b> *End;
1408
1409 <i>// Parse integer part, leaving 'End' pointing to the first non-integer char</i>
1410 Val = (unsigned)strtol(ArgStart, &amp;End, 0);
1411
1412 <b>while</b> (1) {
1413 <b>switch</b> (*End++) {
1414 <b>case</b> 0: <b>return</b> false; <i>// No error</i>
1415 <b>case</b> 'i': <i>// Ignore the 'i' in KiB if people use that</i>
1416 <b>case</b> 'b': <b>case</b> 'B': <i>// Ignore B suffix</i>
1417 <b>break</b>;
1418
1419 <b>case</b> 'g': <b>case</b> 'G': Val *= 1024*1024*1024; <b>break</b>;
1420 <b>case</b> 'm': <b>case</b> 'M': Val *= 1024*1024; <b>break</b>;
1421 <b>case</b> 'k': <b>case</b> 'K': Val *= 1024; <b>break</b>;
1422
1423 default:
1424 <i>// Print an error message if unrecognized character!</i>
1425 <b>return</b> O.error(": '" + Arg + "' value invalid for file size argument!");
1426 }
1427 }
1428}
1429</pre><p>
1430
1431This function implements a very simple parser for the kinds of strings we are
1432interested in. Although it has some holes (it allows "<tt>123KKK</tt>" for
1433example), it is good enough for this example. Note that we use the option
1434itself to print out the error message (the <tt>error</tt> method always returns
1435true) in order to get a nice error message (shown below). Now that we have our
1436parser class, we can use it like this:<p>
1437
1438<pre>
1439<b>static</b> <a href="#cl::opt">cl::opt</a>&lt;<b>unsigned</b>, <b>false</b>, FileSizeParser&gt;
1440MFS(<i>"max-file-size"</i>, <a href="#cl::desc">cl::desc</a>(<i>"Maximum file size to accept"</i>),
1441 <a href="#cl::value_desc">cl::value_desc</a>("<i>size</i>"));
1442</pre><p>
1443
1444Which adds this to the output of our program:<p>
1445
1446<pre>
1447OPTIONS:
1448 -help - display available options (--help-hidden for more)
1449 ...
1450 <b>-max-file-size=&lt;size&gt; - Maximum file size to accept</b>
1451</pre><p>
1452
1453And we can test that our parse works correctly now (the test program just prints
1454out the max-file-size argument value):<p>
1455
1456<pre>
1457$ ./test
1458MFS: 0
1459$ ./test -max-file-size=123MB
1460MFS: 128974848
1461$ ./test -max-file-size=3G
1462MFS: 3221225472
1463$ ./test -max-file-size=dog
1464-max-file-size option: 'dog' value invalid for file size argument!
1465</pre><p>
1466
1467It looks like it works. The error message that we get is nice and helpful, and
1468we seem to accept reasonable file sizes. This wraps up the "custom parser"
1469tutorial.<p>
1470
1471
1472<!-- ======================================================================= -->
1473</ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0>
1474<tr><td>&nbsp;</td><td width="100%">&nbsp; <font color="#EEEEFF"
1475face="Georgia,Palatino"><b> <a name="explotingexternal">Exploiting external
1476storage </b></font></td></tr></table><ul>
1477
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001478
1479
1480<!-- ======================================================================= -->
Chris Lattnerc1ae40c2002-08-07 18:27:04 +00001481</ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0>
1482<tr><td>&nbsp;</td><td width="100%">&nbsp; <font color="#EEEEFF"
1483face="Georgia,Palatino"><b> <a name="dynamicopts">Dynamically adding command
1484line options </b></font></td></tr></table><ul>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001485
1486
Chris Lattner209c7f42001-07-23 23:03:12 +00001487
1488
1489
1490<!-- *********************************************************************** -->
1491</ul>
1492<!-- *********************************************************************** -->
1493
1494<hr>
1495<font size=-1>
1496<address><a href="mailto:sabre@nondot.org">Chris Lattner</a></address>
1497<!-- Created: Tue Jan 23 15:19:28 CST 2001 -->
1498<!-- hhmts start -->
Chris Lattner589a4cc2003-08-01 21:30:37 +00001499Last modified: Fri Aug 1 16:30:11 CDT 2003
Chris Lattner209c7f42001-07-23 23:03:12 +00001500<!-- hhmts end -->
1501</font>
1502</body></html>