blob: 1d46720f3be87ca3e61c800b4012fa209172739c [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>
Brian Gaekee5842852003-08-19 23:11:43 +000045 <li><a href="#cl::ParseEnvironmentOptions">The
46 <tt>cl::ParseEnvironmentOptions</tt> function</a>
Chris Lattnere76d4ab2002-08-06 19:36:06 +000047 <li><a href="#cl::opt">The <tt>cl::opt</tt> class</a>
48 <li><a href="#cl::list">The <tt>cl::list</tt> class</a>
49 <li><a href="#cl::alias">The <tt>cl::alias</tt> class</a>
50 </ul>
51 <li><a href="#builtinparsers">Builtin parsers</a>
52 <ul>
53 <li><a href="#genericparser">The Generic <tt>parser&lt;t&gt;</tt>
54 parser</a>
55 <li><a href="#boolparser">The <tt>parser&lt;bool&gt;</tt>
56 specialization</a>
57 <li><a href="#stringparser">The <tt>parser&lt;string&gt;</tt>
58 specialization</a>
59 <li><a href="#intparser">The <tt>parser&lt;int&gt;</tt>
60 specialization</a>
61 <li><a href="#doubleparser">The <tt>parser&lt;double&gt;</tt> and
62 <tt>parser&lt;float&gt;</tt> specializations</a>
Chris Lattnerae853632002-07-25 19:27:01 +000063 </ul>
64 </ol>
Chris Lattner209c7f42001-07-23 23:03:12 +000065 <li><a href="#extensionguide">Extension Guide</a>
Chris Lattnerae853632002-07-25 19:27:01 +000066 <ol>
Chris Lattnere76d4ab2002-08-06 19:36:06 +000067 <li><a href="#customparser">Writing a custom parser</a>
68 <li><a href="#explotingexternal">Exploiting external storage</a>
69 <li><a href="#dynamicopts">Dynamically adding command line options</a>
Chris Lattnerae853632002-07-25 19:27:01 +000070 </ol>
Chris Lattnere76d4ab2002-08-06 19:36:06 +000071
72 <p><b>Written by <a href="mailto:sabre@nondot.org">Chris Lattner</a></b><p>
Chris Lattner209c7f42001-07-23 23:03:12 +000073</ol><p>
74
75
76<!-- *********************************************************************** -->
Chris Lattnerc1ae40c2002-08-07 18:27:04 +000077<table width="100%" bgcolor="#330077" border=0 cellpadding=4 cellspacing=0>
Chris Lattner209c7f42001-07-23 23:03:12 +000078<tr><td align=center><font color="#EEEEFF" size=+2 face="Georgia,Palatino"><b>
79<a name="introduction">Introduction
80</b></font></td></tr></table><ul>
81<!-- *********************************************************************** -->
82
Chris Lattnerae853632002-07-25 19:27:01 +000083This document describes the CommandLine argument processing library. It will
Chris Lattnere76d4ab2002-08-06 19:36:06 +000084show you how to use it, and what it can do. The CommandLine library uses a
85declarative approach to specifying the command line options that your program
86takes. By default, these options declarations implicitly hold the value parsed
87for the option declared (of course this <a href="#storage">can be
88changed</a>).<p>
Chris Lattner209c7f42001-07-23 23:03:12 +000089
Chris Lattnerae853632002-07-25 19:27:01 +000090Although there are a <b>lot</b> of command line argument parsing libraries out
91there in many different languages, none of them fit well with what I needed. By
92looking at the features and problems of other libraries, I designed the
93CommandLine library to have the following features:<p>
Chris Lattner209c7f42001-07-23 23:03:12 +000094
95<ol>
Chris Lattnerae853632002-07-25 19:27:01 +000096<li>Speed: The CommandLine library is very quick and uses little resources. The
97parsing time of the library is directly proportional to the number of arguments
98parsed, not the the number of options recognized. Additionally, command line
Chris Lattneredf351f2003-06-21 21:45:56 +000099argument values are captured transparently into user defined global variables,
100which can be accessed like any other variable (and with the same
101performance).<p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000102
Chris Lattnerae853632002-07-25 19:27:01 +0000103<li>Type Safe: As a user of CommandLine, you don't have to worry about
104remembering the type of arguments that you want (is it an int? a string? a
105bool? an enum?) and keep casting it around. Not only does this help prevent
106error prone constructs, it also leads to dramatically cleaner source code.<p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000107
Chris Lattnerae853632002-07-25 19:27:01 +0000108<li>No subclasses required: To use CommandLine, you instantiate variables that
109correspond to the arguments that you would like to capture, you don't subclass a
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000110parser. This means that you don't have to write <b>any</b> boilerplate code.<p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000111
Chris Lattnerae853632002-07-25 19:27:01 +0000112<li>Globally accessible: Libraries can specify command line arguments that are
113automatically enabled in any tool that links to the library. This is possible
114because the application doesn't have to keep a "list" of arguments to pass to
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000115the parser. This also makes supporting <a href="#dynamicopts">dynamically
116loaded options</a> trivial.<p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000117
Chris Lattnerc1ae40c2002-08-07 18:27:04 +0000118<li>Cleaner: CommandLine supports enum and other types directly, meaning that
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000119there is less error and more security built into the library. You don't have to
120worry about whether your integral command line argument accidentally got
121assigned a value that is not valid for your enum type.<p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000122
Chris Lattnerae853632002-07-25 19:27:01 +0000123<li>Powerful: The CommandLine library supports many different types of
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000124arguments, from simple <a href="#boolparser">boolean flags</a> to <a
125href="#cl::opt">scalars arguments</a> (<a href="#stringparser">strings</a>, <a
126href="#intparser">integers</a>, <a href="#genericparser">enums</a>, <a
127href="#doubleparser">doubles</a>), to <a href="#cl::list">lists of
128arguments</a>. This is possible because CommandLine is...<p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000129
Chris Lattnerae853632002-07-25 19:27:01 +0000130<li>Extensible: It is very simple to add a new argument type to CommandLine.
131Simply specify the parser that you want to use with the command line option when
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000132you declare it. <a href="#customparser">Custom parsers</a> are no problem.<p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000133
Chris Lattnerae853632002-07-25 19:27:01 +0000134<li>Labor Saving: The CommandLine library cuts down on the amount of grunt work
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000135that you, the user, have to do. For example, it automatically provides a
136<tt>--help</tt> option that shows the available command line options for your
137tool. Additionally, it does most of the basic correctness checking for you.<p>
138
139<li>Capable: The CommandLine library can handle lots of different forms of
140options often found in real programs. For example, <a
141href="#positional">positional</a> arguments, <tt>ls</tt> style <a
142href="#cl::Grouping">grouping</a> options (to allow processing '<tt>ls
143-lad</tt>' naturally), <tt>ld</tt> style <a href="#cl::Prefix">prefix</a>
144options (to parse '<tt>-lmalloc -L/usr/lib</tt>'), and <a
145href="#cl::ConsumeAfter">interpreter style options</a>.<p>
146
Chris Lattner209c7f42001-07-23 23:03:12 +0000147</ol>
148
Chris Lattnerae853632002-07-25 19:27:01 +0000149This document will hopefully let you jump in and start using CommandLine in your
150utility quickly and painlessly. Additionally it should be a simple reference
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000151manual to figure out how stuff works. If it is failing in some area (or you
152want an extension to the library), nag the author, <a
153href="mailto:sabre@nondot.org">Chris Lattner</a>.<p>
154
Chris Lattner209c7f42001-07-23 23:03:12 +0000155
156
157<!-- *********************************************************************** -->
158</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>
159<a name="quickstart">Quick Start Guide
160</b></font></td></tr></table><ul>
161<!-- *********************************************************************** -->
162
Chris Lattnerae853632002-07-25 19:27:01 +0000163This section of the manual runs through a simple CommandLine'ification of a
164basic compiler tool. This is intended to show you how to jump into using the
165CommandLine library in your own program, and show you some of the cool things it
166can do.<p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000167
Chris Lattnerae853632002-07-25 19:27:01 +0000168To start out, you need to include the CommandLine header file into your
169program:<p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000170
171<pre>
Chris Lattnerae853632002-07-25 19:27:01 +0000172 #include "Support/CommandLine.h"
Chris Lattner209c7f42001-07-23 23:03:12 +0000173</pre><p>
174
175Additionally, you need to add this as the first line of your main program:<p>
176
177<pre>
178int main(int argc, char **argv) {
Chris Lattnerc1ae40c2002-08-07 18:27:04 +0000179 <a href="#cl::ParseCommandLineOptions">cl::ParseCommandLineOptions</a>(argc, argv);
Chris Lattner209c7f42001-07-23 23:03:12 +0000180 ...
181}
182</pre><p>
183
Chris Lattnerae853632002-07-25 19:27:01 +0000184... which actually parses the arguments and fills in the variable
185declarations.<p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000186
Chris Lattnerae853632002-07-25 19:27:01 +0000187Now that you are ready to support command line arguments, we need to tell the
188system which ones we want, and what type of argument they are. The CommandLine
Chris Lattneredf351f2003-06-21 21:45:56 +0000189library uses a declarative syntax to model command line arguments with the
190global variable declarations that capture the parsed values. This means that
191for every command line option that you would like to support, there should be a
192global variable declaration to capture the result. For example, in a compiler,
193we would like to support the unix standard '<tt>-o &lt;filename&gt;</tt>' option
194to specify where to put the output. With the CommandLine library, this is
195represented like this:<p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000196
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000197<pre><a name="value_desc_example">
198<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 +0000199</pre><p>
200
Chris Lattneredf351f2003-06-21 21:45:56 +0000201This declares a global variable "<tt>OutputFilename</tt>" that is used to
202capture the result of the "<tt>o</tt>" argument (first parameter). We specify
203that this is a simple scalar option by using the "<tt><a
204href="#cl::opt">cl::opt</a></tt>" template (as opposed to the <a
205href="#list">"<tt>cl::list</tt> template</a>), and tell the CommandLine library
206that the data type that we are parsing is a string.<p>
Chris Lattnerae853632002-07-25 19:27:01 +0000207
208The second and third parameters (which are optional) are used to specify what to
209output for the "<tt>--help</tt>" option. In this case, we get a line that looks
210like this:<p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000211
212<pre>
Chris Lattnerae853632002-07-25 19:27:01 +0000213USAGE: compiler [options]
Chris Lattner209c7f42001-07-23 23:03:12 +0000214
Chris Lattnerae853632002-07-25 19:27:01 +0000215OPTIONS:
216 -help - display available options (--help-hidden for more)
Chris Lattnerc1ae40c2002-08-07 18:27:04 +0000217 <b>-o &lt;filename&gt; - Specify output filename</b>
Chris Lattnerae853632002-07-25 19:27:01 +0000218</pre>
219
220Because we specified that the command line option should parse using the
221<tt>string</tt> data type, the variable declared is automatically usable as a
222real string in all contexts that a normal C++ string object may be used. For
223example:<p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000224
225<pre>
226 ...
227 ofstream Output(OutputFilename.c_str());
228 if (Out.good()) ...
229 ...
230</pre><p>
231
Chris Lattnerae853632002-07-25 19:27:01 +0000232There are many different options that you can use to customize the command line
233option handling library, but the above example shows the general interface to
234these options. The options can be specified in any order, and are specified
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000235with helper functions like <a href="#cl::desc"><tt>cl::desc(...)</tt></a>, so
Chris Lattnerc1ae40c2002-08-07 18:27:04 +0000236there are no positional dependencies to remember. The available options are
237discussed in detail in the <a href="#referenceguide">Reference Guide</a>.<p>
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000238
Chris Lattner209c7f42001-07-23 23:03:12 +0000239
Chris Lattnerae853632002-07-25 19:27:01 +0000240Continuing the example, we would like to have our compiler take an input
241filename as well as an output filename, but we do not want the input filename to
242be specified with a hyphen (ie, not <tt>-filename.c</tt>). To support this
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000243style of argument, the CommandLine library allows for <a
244href="#positional">positional</a> arguments to be specified for the program.
245These positional arguments are filled with command line parameters that are not
246in option form. We use this feature like this:<p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000247
248<pre>
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000249<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 +0000250</pre>
251
Chris Lattnerae853632002-07-25 19:27:01 +0000252This declaration indicates that the first positional argument should be treated
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000253as the input filename. Here we use the <tt><a
254href="#cl::init">cl::init</a></tt> option to specify an initial value for the
255command line option, which is used if the option is not specified (if you do not
256specify a <tt><a href="#cl::init">cl::init</a></tt> modifier for an option, then
257the default constructor for the data type is used to initialize the value).
Chris Lattnerae853632002-07-25 19:27:01 +0000258Command line options default to being optional, so if we would like to require
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000259that the user always specify an input filename, we would add the <tt><a
260href="#cl::Required">cl::Required</a></tt> flag, and we could eliminate the
261<tt><a href="#cl::init">cl::init</a></tt> modifier, like this:<p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000262
263<pre>
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000264<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 +0000265</pre>
266
Chris Lattnerae853632002-07-25 19:27:01 +0000267Again, the CommandLine library does not require the options to be specified in
268any particular order, so the above declaration is equivalent to:<p>
269
270<pre>
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000271<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 +0000272</pre>
273
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000274By simply adding the <tt><a href="#cl::Required">cl::Required</a></tt> flag, the
275CommandLine library will automatically issue an error if the argument is not
276specified, which shifts all of the command line option verification code out of
277your application into the library. This is just one example of how using flags
278can alter the default behaviour of the library, on a per-option basis. By
279adding one of the declarations above, the <tt>--help</tt> option synopsis is now
280extended to:<p>
Chris Lattnerae853632002-07-25 19:27:01 +0000281
282<pre>
Chris Lattnerc1ae40c2002-08-07 18:27:04 +0000283USAGE: compiler [options] <b>&lt;input file&gt;</b>
Chris Lattnerae853632002-07-25 19:27:01 +0000284
285OPTIONS:
286 -help - display available options (--help-hidden for more)
287 -o &lt;filename&gt; - Specify output filename
288</pre>
289
290... indicating that an input filename is expected.<p>
291
Chris Lattner209c7f42001-07-23 23:03:12 +0000292
Chris Lattnerc1ae40c2002-08-07 18:27:04 +0000293
Chris Lattner209c7f42001-07-23 23:03:12 +0000294<!-- ======================================================================= -->
295</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 +0000296<a name="bool">Boolean Arguments
Chris Lattner209c7f42001-07-23 23:03:12 +0000297</b></font></td></tr></table><ul>
298
Chris Lattnerae853632002-07-25 19:27:01 +0000299In addition to input and output filenames, we would like the compiler example to
300support three boolean flags: "<tt>-f</tt>" to force overwriting of the output
301file, "<tt>--quiet</tt>" to enable quiet mode, and "<tt>-q</tt>" for backwards
302compatibility with some of our users. We can support these by declaring options
303of boolean type like this:<p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000304
305<pre>
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000306<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>"));
307<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>"));
308<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 +0000309</pre><p>
310
Chris Lattnerae853632002-07-25 19:27:01 +0000311This does what you would expect: it declares three boolean variables
312("<tt>Force</tt>", "<tt>Quiet</tt>", and "<tt>Quiet2</tt>") to recognize these
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000313options. Note that the "<tt>-q</tt>" option is specified with the "<a
314href="#cl::Hidden"><tt>cl::Hidden</tt></a>" flag. This modifier prevents it
315from being shown by the standard "<tt>--help</tt>" output (note that it is still
316shown in the "<tt>--help-hidden</tt>" output).<p>
Chris Lattnerae853632002-07-25 19:27:01 +0000317
Chris Lattnerc1ae40c2002-08-07 18:27:04 +0000318The CommandLine library uses a <a href="#builtinparsers">different parser</a>
319for different data types. For example, in the string case, the argument passed
320to the option is copied literally into the content of the string variable... we
321obviously cannot do that in the boolean case, however, so we must use a smarter
322parser. In the case of the boolean parser, it allows no options (in which case
323it assigns the value of true to the variable), or it allows the values
324"<tt>true</tt>" or "<tt>false</tt>" to be specified, allowing any of the
325following inputs:<p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000326
327<pre>
Chris Lattnerae853632002-07-25 19:27:01 +0000328 compiler -f # No value, 'Force' == true
329 compiler -f=true # Value specified, 'Force' == true
330 compiler -f=TRUE # Value specified, 'Force' == true
331 compiler -f=FALSE # Value specified, 'Force' == false
332</pre>
333
Chris Lattnerc1ae40c2002-08-07 18:27:04 +0000334... you get the idea. The <a href="#boolparser">bool parser</a> just turns the
335string values into boolean values, and rejects things like '<tt>compiler
336-f=foo</tt>'. Similarly, the <a href="#doubleparser">float</a>, <a
337href="#doubleparser">double</a>, and <a href="#intparser">int</a> parsers work
338like you would expect, using the '<tt>strtol</tt>' and '<tt>strtod</tt>' C
339library calls to parse the string value into the specified data type.<p>
Chris Lattnerae853632002-07-25 19:27:01 +0000340
341With the declarations above, "<tt>compiler --help</tt>" emits this:<p>
342
343<pre>
344USAGE: compiler [options] &lt;input file&gt;
Chris Lattner209c7f42001-07-23 23:03:12 +0000345
346OPTIONS:
Chris Lattnerc1ae40c2002-08-07 18:27:04 +0000347 <b>-f - Overwrite output files</b>
Chris Lattner209c7f42001-07-23 23:03:12 +0000348 -o - Override output filename
Chris Lattnerc1ae40c2002-08-07 18:27:04 +0000349 <b>-quiet - Don't print informational messages</b>
Chris Lattner209c7f42001-07-23 23:03:12 +0000350 -help - display available options (--help-hidden for more)
351</pre><p>
352
Chris Lattnerae853632002-07-25 19:27:01 +0000353and "<tt>opt --help-hidden</tt>" prints this:<p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000354
355<pre>
Chris Lattnerc1ae40c2002-08-07 18:27:04 +0000356USAGE: compiler [options] &lt;input file&gt;
Chris Lattner209c7f42001-07-23 23:03:12 +0000357
358OPTIONS:
359 -f - Overwrite output files
360 -o - Override output filename
Chris Lattnerc1ae40c2002-08-07 18:27:04 +0000361 <b>-q - Don't print informational messages</b>
Chris Lattner209c7f42001-07-23 23:03:12 +0000362 -quiet - Don't print informational messages
363 -help - display available options (--help-hidden for more)
364</pre><p>
365
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000366This brief example has shown you how to use the '<tt><a
367href="#cl::opt">cl::opt</a></tt>' class to parse simple scalar command line
368arguments. In addition to simple scalar arguments, the CommandLine library also
369provides primitives to support CommandLine option <a href="#alias">aliases</a>,
370and <a href="#list">lists</a> of options.<p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000371
372
373<!-- ======================================================================= -->
374</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 +0000375<a name="alias">Argument Aliases
Chris Lattner209c7f42001-07-23 23:03:12 +0000376</b></font></td></tr></table><ul>
377
Chris Lattnerae853632002-07-25 19:27:01 +0000378So far, the example works well, except for the fact that we need to check the
379quiet condition like this now:<p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000380
381<pre>
382...
383 if (!Quiet &amp;&amp; !Quiet2) printInformationalMessage(...);
384...
385</pre><p>
386
Chris Lattnerae853632002-07-25 19:27:01 +0000387... which is a real pain! Instead of defining two values for the same
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000388condition, 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 +0000389option an <b>alias</b> for the "<tt>-quiet</tt>" option, instead of providing
390a value itself:<p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000391
392<pre>
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000393<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>"));
394<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>"));
395<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 +0000396</pre><p>
397
Chris Lattnerae853632002-07-25 19:27:01 +0000398The third line (which is the only one we modified from above) defines a
399"<tt>-q</tt> alias that updates the "<tt>Quiet</tt>" variable (as specified by
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000400the <tt><a href="#cl::aliasopt">cl::aliasopt</a></tt> modifier) whenever it is
401specified. Because aliases do not hold state, the only thing the program has to
402query is the <tt>Quiet</tt> variable now. Another nice feature of aliases is
403that they automatically hide themselves from the <tt>-help</tt> output
404(although, again, they are still visible in the <tt>--help-hidden
405output</tt>).<p>
Chris Lattnerae853632002-07-25 19:27:01 +0000406
407Now the application code can simply use:<p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000408
409<pre>
410...
411 if (!Quiet) printInformationalMessage(...);
412...
413</pre><p>
414
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000415... 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 +0000416alternative name for any variable type, and has many uses.<p>
417
Chris Lattner209c7f42001-07-23 23:03:12 +0000418
419
420<!-- ======================================================================= -->
421</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 +0000422<a name="onealternative">Selecting an alternative from a set of possibilities
Chris Lattner209c7f42001-07-23 23:03:12 +0000423</b></font></td></tr></table><ul>
424
Chris Lattnerae853632002-07-25 19:27:01 +0000425So far, we have seen how the CommandLine library handles builtin types like
426<tt>std::string</tt>, <tt>bool</tt> and <tt>int</tt>, but how does it handle
427things it doesn't know about, like enums or '<tt>int*</tt>'s?<p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000428
Chris Lattnerae853632002-07-25 19:27:01 +0000429The answer is that it uses a table driven generic parser (unless you specify
430your own parser, as described in the <a href="#extensionguide">Extension
431Guide</a>). This parser maps literal strings to whatever type is required, are
432requires you to tell it what this mapping should be.<p>
433
434Lets say that we would like to add four optimizations levels to our optimizer,
435using the standard flags "<tt>-g</tt>", "<tt>-O0</tt>", "<tt>-O1</tt>", and
436"<tt>-O2</tt>". We could easily implement this with boolean options like above,
437but there are several problems with this strategy:<p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000438
439<ol>
Chris Lattnerae853632002-07-25 19:27:01 +0000440<li>A user could specify more than one of the options at a time, for example,
441"<tt>opt -O3 -O2</tt>". The CommandLine library would not be able to catch this
442erroneous input for us.
443
Chris Lattner209c7f42001-07-23 23:03:12 +0000444<li>We would have to test 4 different variables to see which ones are set.
Chris Lattnerae853632002-07-25 19:27:01 +0000445
446<li>This doesn't map to the numeric levels that we want... so we cannot easily
447see if some level &gt;= "<tt>-O1</tt>" is enabled.
448
Chris Lattner209c7f42001-07-23 23:03:12 +0000449</ol><p>
450
Chris Lattnerae853632002-07-25 19:27:01 +0000451To cope with these problems, we can use an enum value, and have the CommandLine
452library fill it in with the appropriate level directly, which is used like
453this:<p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000454
455<pre>
456enum OptLevel {
457 g, O1, O2, O3
458};
459
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000460<a href="#cl::opt">cl::opt</a>&lt;OptLevel&gt; OptimizationLevel(<a href="#cl::desc">cl::desc</a>("<i>Choose optimization level:</i>"),
461 <a href="#cl::values">cl::values</a>(
Chris Lattnerae853632002-07-25 19:27:01 +0000462 clEnumVal(g , "<i>No optimizations, enable debugging</i>"),
463 clEnumVal(O1, "<i>Enable trivial optimizations</i>"),
464 clEnumVal(O2, "<i>Enable default optimizations</i>"),
465 clEnumVal(O3, "<i>Enable expensive optimizations</i>"),
466 0));
Chris Lattner209c7f42001-07-23 23:03:12 +0000467
468...
Chris Lattnerae853632002-07-25 19:27:01 +0000469 if (OptimizationLevel &gt;= O2) doPartialRedundancyElimination(...);
Chris Lattner209c7f42001-07-23 23:03:12 +0000470...
471</pre><p>
472
Chris Lattnerae853632002-07-25 19:27:01 +0000473This declaration defines a variable "<tt>OptimizationLevel</tt>" of the
474"<tt>OptLevel</tt>" enum type. This variable can be assigned any of the values
475that are listed in the declaration (Note that the declaration list must be
476terminated with the "<tt>0</tt>" argument!). The CommandLine library enforces
477that the user can only specify one of the options, and it ensure that only valid
478enum values can be specified. The "<tt>clEnumVal</tt>" macros ensure that the
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000479command line arguments matched the enum values. With this option added, our
480help output now is:<p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000481
Chris Lattnerae853632002-07-25 19:27:01 +0000482<pre>
483USAGE: compiler [options] &lt;input file&gt;
Chris Lattner209c7f42001-07-23 23:03:12 +0000484
Chris Lattnerae853632002-07-25 19:27:01 +0000485OPTIONS:
Chris Lattnerc1ae40c2002-08-07 18:27:04 +0000486 <b>Choose optimization level:
Chris Lattnerae853632002-07-25 19:27:01 +0000487 -g - No optimizations, enable debugging
488 -O1 - Enable trivial optimizations
489 -O2 - Enable default optimizations
Chris Lattnerc1ae40c2002-08-07 18:27:04 +0000490 -O3 - Enable expensive optimizations</b>
Chris Lattnerae853632002-07-25 19:27:01 +0000491 -f - Overwrite output files
492 -help - display available options (--help-hidden for more)
493 -o &lt;filename&gt; - Specify output filename
494 -quiet - Don't print informational messages
495</pre>
Chris Lattner209c7f42001-07-23 23:03:12 +0000496
Chris Lattnerae853632002-07-25 19:27:01 +0000497In this case, it is sort of awkward that flag names correspond directly to enum
498names, because we probably don't want a enum definition named "<tt>g</tt>" in
499our program. Because of this, we can alternatively write this example like
500this:<p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000501
502<pre>
503enum OptLevel {
504 Debug, O1, O2, O3
505};
506
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000507<a href="#cl::opt">cl::opt</a>&lt;OptLevel&gt; OptimizationLevel(<a href="#cl::desc">cl::desc</a>("<i>Choose optimization level:</i>"),
508 <a href="#cl::values">cl::values</a>(
Chris Lattnerae853632002-07-25 19:27:01 +0000509 clEnumValN(Debug, "g", "<i>No optimizations, enable debugging</i>"),
510 clEnumVal(O1 , "<i>Enable trivial optimizations</i>"),
511 clEnumVal(O2 , "<i>Enable default optimizations</i>"),
512 clEnumVal(O3 , "<i>Enable expensive optimizations</i>"),
513 0));
Chris Lattner209c7f42001-07-23 23:03:12 +0000514
515...
516 if (OptimizationLevel == Debug) outputDebugInfo(...);
517...
518</pre><p>
519
Chris Lattnerae853632002-07-25 19:27:01 +0000520By using the "<tt>clEnumValN</tt>" macro instead of "<tt>clEnumVal</tt>", we can
521directly specify the name that the flag should get. In general a direct mapping
522is nice, but sometimes you can't or don't want to preserve the mapping, which is
523when you would use it.<p>
524
525
Chris Lattner209c7f42001-07-23 23:03:12 +0000526
527<!-- ======================================================================= -->
528</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>
529<a name="namedalternatives">Named Alternatives
530</b></font></td></tr></table><ul>
531
Chris Lattnerae853632002-07-25 19:27:01 +0000532Another useful argument form is a named alternative style. We shall use this
533style in our compiler to specify different debug levels that can be used.
534Instead of each debug level being its own switch, we want to support the
535following options, of which only one can be specified at a time:
536"<tt>--debug-level=none</tt>", "<tt>--debug-level=quick</tt>",
537"<tt>--debug-level=detailed</tt>". To do this, we use the exact same format as
538our optimization level flags, but we also specify an option name. For this
539case, the code looks like this:<p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000540
541<pre>
542enum DebugLev {
543 nodebuginfo, quick, detailed
544};
545
546// Enable Debug Options to be specified on the command line
Chris Lattnerdc844fa2003-06-03 04:40:06 +0000547<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 +0000548 <a href="#cl::values">cl::values</a>(
Chris Lattnerae853632002-07-25 19:27:01 +0000549 clEnumValN(nodebuginfo, "none", "<i>disable debug information</i>"),
550 clEnumVal(quick, "<i>enable quick debug information</i>"),
551 clEnumVal(detailed, "<i>enable detailed debug information</i>"),
552 0));
Chris Lattner209c7f42001-07-23 23:03:12 +0000553</pre>
554
Chris Lattnerae853632002-07-25 19:27:01 +0000555This definition defines an enumerated command line variable of type "<tt>enum
556DebugLev</tt>", which works exactly the same way as before. The difference here
557is just the interface exposed to the user of your program and the help output by
558the "<tt>--help</tt>" option:<p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000559
560<pre>
Chris Lattnerae853632002-07-25 19:27:01 +0000561USAGE: compiler [options] &lt;input file&gt;
562
Chris Lattner209c7f42001-07-23 23:03:12 +0000563OPTIONS:
Chris Lattnerae853632002-07-25 19:27:01 +0000564 Choose optimization level:
565 -g - No optimizations, enable debugging
566 -O1 - Enable trivial optimizations
567 -O2 - Enable default optimizations
568 -O3 - Enable expensive optimizations
Chris Lattnerc1ae40c2002-08-07 18:27:04 +0000569 <b>-debug_level - Set the debugging level:
Chris Lattnerae853632002-07-25 19:27:01 +0000570 =none - disable debug information
571 =quick - enable quick debug information
Chris Lattnerc1ae40c2002-08-07 18:27:04 +0000572 =detailed - enable detailed debug information</b>
Chris Lattnerae853632002-07-25 19:27:01 +0000573 -f - Overwrite output files
574 -help - display available options (--help-hidden for more)
575 -o &lt;filename&gt; - Specify output filename
576 -quiet - Don't print informational messages
Chris Lattner209c7f42001-07-23 23:03:12 +0000577</pre><p>
578
Chris Lattnerae853632002-07-25 19:27:01 +0000579Again, the only structural difference between the debug level declaration and
580the optimiation level declaration is that the debug level declaration includes
581an option name (<tt>"debug_level"</tt>), which automatically changes how the
582library processes the argument. The CommandLine library supports both forms so
583that you can choose the form most appropriate for your application.<p>
584
Chris Lattner209c7f42001-07-23 23:03:12 +0000585
586
587<!-- ======================================================================= -->
588</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 +0000589<a name="list">Parsing a list of options
Chris Lattner209c7f42001-07-23 23:03:12 +0000590</b></font></td></tr></table><ul>
591
Chris Lattnerae853632002-07-25 19:27:01 +0000592Now that we have the standard run of the mill argument types out of the way,
593lets get a little wild and crazy. Lets say that we want our optimizer to accept
594a <b>list</b> of optimizations to perform, allowing duplicates. For example, we
595might want to run: "<tt>compiler -dce -constprop -inline -dce -strip</tt>". In
596this case, the order of the arguments and the number of appearances is very
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000597important. This is what the "<tt><a href="#cl::list">cl::list</a></tt>"
598template is for. First, start by defining an enum of the optimizations that you
599would like to perform:<p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000600
601<pre>
602enum Opts {
Chris Lattnerae853632002-07-25 19:27:01 +0000603 // 'inline' is a C++ keyword, so name it 'inlining'
Chris Lattner209c7f42001-07-23 23:03:12 +0000604 dce, constprop, inlining, strip
Chris Lattnerae853632002-07-25 19:27:01 +0000605};
Chris Lattner209c7f42001-07-23 23:03:12 +0000606</pre><p>
607
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000608Then define your "<tt><a href="#cl::list">cl::list</a></tt>" variable:<p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000609
610<pre>
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000611<a href="#cl::list">cl::list</a>&lt;Opts&gt; OptimizationList(<a href="#cl::desc">cl::desc</a>("<i>Available Optimizations:</i>"),
612 <a href="#cl::values">cl::values</a>(
Chris Lattnerae853632002-07-25 19:27:01 +0000613 clEnumVal(dce , "<i>Dead Code Elimination</i>"),
Misha Brukman82c89b92003-05-20 21:01:22 +0000614 clEnumVal(constprop , "<i>Constant Propagation</i>"),
Chris Lattnerae853632002-07-25 19:27:01 +0000615 clEnumValN(inlining, "<i>inline</i>", "<i>Procedure Integration</i>"),
616 clEnumVal(strip , "<i>Strip Symbols</i>"),
617 0));
Chris Lattner209c7f42001-07-23 23:03:12 +0000618</pre><p>
619
Chris Lattnerae853632002-07-25 19:27:01 +0000620This defines a variable that is conceptually of the type
621"<tt>std::vector&lt;enum Opts&gt;</tt>". Thus, you can access it with standard
622vector methods:<p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000623
624<pre>
Chris Lattnerae853632002-07-25 19:27:01 +0000625 for (unsigned i = 0; i != OptimizationList.size(); ++i)
Chris Lattner209c7f42001-07-23 23:03:12 +0000626 switch (OptimizationList[i])
627 ...
628</pre>
629
Chris Lattnerae853632002-07-25 19:27:01 +0000630... to iterate through the list of options specified.<p>
Chris Lattner209c7f42001-07-23 23:03:12 +0000631
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000632Note 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 +0000633with any data types or other arguments that you can use with the
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000634"<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 +0000635capture all of the positional arguments together if there may be more than one
636specified. In the case of a linker, for example, the linker takes several
637'<tt>.o</tt>' files, and needs to capture them into a list. This is naturally
638specified as:<p>
Chris Lattner3e5fe172002-04-13 18:35:59 +0000639
640<pre>
641...
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000642<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 +0000643...
644</pre><p>
645
Chris Lattnerae853632002-07-25 19:27:01 +0000646This variable works just like a "<tt>vector&lt;string&gt;</tt>" object. As
647such, accessing the list is simple, just like above. In this example, we used
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000648the <tt><a href="#cl::OneOrMore">cl::OneOrMore</a></tt> modifier to inform the
649CommandLine library that it is an error if the user does not specify any
650<tt>.o</tt> files on our command line. Again, this just reduces the amount of
651checking we have to do.<p>
Chris Lattner3e5fe172002-04-13 18:35:59 +0000652
Chris Lattner3e5fe172002-04-13 18:35:59 +0000653
654
Chris Lattnerc1ae40c2002-08-07 18:27:04 +0000655<!-- ======================================================================= -->
656</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>
657<a name="description">Adding freeform text to help output
658</b></font></td></tr></table><ul>
659
660As our program grows and becomes more mature, we may decide to put summary
661information about what it does into the help output. The help output is styled
662to look similar to a Unix <tt>man</tt> page, providing concise information about
663a program. Unix <tt>man</tt> pages, however often have a description about what
664the program does. To add this to your CommandLine program, simply pass a third
665argument to the <a
666href="#cl::ParseCommandLineOptions"><tt>cl::ParseCommandLineOptions</tt></a>
667call in main. This additional argument is then printed as the overview
668information for your program, allowing you to include any additional information
669that you want. For example:<p>
670
671<pre>
672int main(int argc, char **argv) {
673 <a href="#cl::ParseCommandLineOptions">cl::ParseCommandLineOptions</a>(argc, argv, " CommandLine compiler example\n\n"
674 " This program blah blah blah...\n");
675 ...
676}
677</pre><p>
678
679Would yield the help output:
680
681<pre>
682<b>OVERVIEW: CommandLine compiler example
683
684 This program blah blah blah...</b>
685
686USAGE: compiler [options] &lt;input file&gt;
687
688OPTIONS:
689 ...
690 -help - display available options (--help-hidden for more)
691 -o &lt;filename&gt; - Specify output filename
692</pre><p>
693
694
695
696
Chris Lattner209c7f42001-07-23 23:03:12 +0000697<!-- *********************************************************************** -->
698</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>
699<a name="referenceguide">Reference Guide
700</b></font></td></tr></table><ul>
701<!-- *********************************************************************** -->
702
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000703Now that you know the basics of how to use the CommandLine library, this section
704will give you the detailed information you need to tune how command line options
705work, as well as information on more "advanced" command line option processing
706capabilities.<p>
707
708
709<!-- ======================================================================= -->
710</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>
711<a name="positional">Positional Arguments
712</b></font></td></tr></table><ul>
713
714Positional arguments are those arguments that are not named, and are not
715specified with a hyphen. Positional arguments should be used when an option is
716specified by its position alone. For example, the standard Unix <tt>grep</tt>
717tool takes a regular expression argument, and an optional filename to search
718through (which defaults to standard input if a filename is not specified).
719Using the CommandLine library, this would be specified as:<p>
720
721<pre>
722<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>);
723<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>"));
724</pre>
725
726Given these two option declarations, the <tt>--help</tt> output for our grep
727replacement would look like this:<p>
728
729<pre>
Chris Lattnerc1ae40c2002-08-07 18:27:04 +0000730USAGE: spiffygrep [options] <b>&lt;regular expression&gt; &lt;input file&gt;</b>
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000731
732OPTIONS:
733 -help - display available options (--help-hidden for more)
734</pre>
735
736... and the resultant program could be used just like the standard <tt>grep</tt>
737tool.<p>
738
739Positional arguments are sorted by their order of construction. This means that
740command line options will be ordered according to how they are listed in a .cpp
741file, but will not have an ordering defined if they positional arguments are
742defined in multiple .cpp files. The fix for this problem is simply to define
743all of your positional arguments in one .cpp file.<p>
744
745
746
747<!-- _______________________________________________________________________ -->
748</ul><a name="--"><h4><hr size=0>Specifying positional options with hyphens</h4><ul>
749
750Sometimes you may want to specify a value to your positional argument that
751starts with a hyphen (for example, searching for '<tt>-foo</tt>' in a file). At
752first, you will have trouble doing this, because it will try to find an argument
753named '<tt>-foo</tt>', and will fail (and single quotes will not save you).
754Note that the system <tt>grep</tt> has the same problem:<p>
755
756<pre>
757 $ spiffygrep '-foo' test.txt
758 Unknown command line argument '-foo'. Try: spiffygrep --help'
759
760 $ grep '-foo' test.txt
761 grep: illegal option -- f
762 grep: illegal option -- o
763 grep: illegal option -- o
764 Usage: grep -hblcnsviw pattern file . . .
765</pre><p>
766
767The solution for this problem is the same for both your tool and the system
768version: use the '<tt>--</tt>' marker. When the user specifies '<tt>--</tt>' on
769the command line, it is telling the program that all options after the
770'<tt>--</tt>' should be treated as positional arguments, not options. Thus, we
771can use it like this:<p>
772
773<pre>
774 $ spiffygrep -- -foo test.txt
775 ...output...
776</pre><p>
777
778
779
780<!-- _______________________________________________________________________ -->
781</ul><a name="cl::ConsumeAfter"><h4><hr size=0>The <tt>cl::ConsumeAfter</tt> modifier</h4><ul>
782
783The <tt>cl::ConsumeAfter</tt> <a href="#formatting">formatting option</a> is
784used to construct programs that use "interpreter style" option processing. With
785this style of option processing, all arguments specified after the last
786positional argument are treated as special interpreter arguments that are not
787interpreted by the command line argument.<p>
788
789As a concrete example, lets say we are developing a replacement for the standard
790Unix Bourne shell (<tt>/bin/sh</tt>). To run <tt>/bin/sh</tt>, first you
791specify options to the shell itself (like <tt>-x</tt> which turns on trace
792output), then you specify the name of the script to run, then you specify
793arguments to the script. These arguments to the script are parsed by the bourne
794shell command line option processor, but are not interpreted as options to the
795shell itself. Using the CommandLine library, we would specify this as:<p>
796
797<pre>
798<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>("-"));
799<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>"));
800<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>"));
801</pre><p>
802
803which automatically provides the help output:<p>
804
805<pre>
Chris Lattnerc1ae40c2002-08-07 18:27:04 +0000806USAGE: spiffysh [options] <b>&lt;input script&gt; &lt;program arguments&gt;...</b>
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000807
808OPTIONS:
809 -help - display available options (--help-hidden for more)
Chris Lattnerc1ae40c2002-08-07 18:27:04 +0000810 <b>-x - Enable trace output</b>
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000811</pre><p>
812
813At runtime, if we run our new shell replacement as '<tt>spiffysh -x test.sh -a
814-x -y bar</tt>', the <tt>Trace</tt> variable will be set to true, the
815<tt>Script</tt> variable will be set to "<tt>test.sh</tt>", and the
816<tt>Argv</tt> list will contain <tt>["-a", "-x", "-y", "bar"]</tt>, because
817they were specified after the last positional argument (which is the script
818name).<p>
819
820There are several limitations to when <tt>cl::ConsumeAfter</tt> options can be
821specified. For example, only one <tt>cl::ConsumeAfter</tt> can be specified per
822program, there must be at least one <a href="#positional">positional
823argument</a> specified, and the <tt>cl::ConsumeAfter</tt> option should be a <a
824href="#cl::list">cl::list</a> option.<p>
825
826
827
828<!-- ======================================================================= -->
829</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>
830<a name="storage">Internal vs External Storage
831</b></font></td></tr></table><ul>
832
833By default, all command line options automatically hold the value that they
834parse from the command line. This is very convenient in the common case,
835especially when combined with the ability to define command line options in the
836files that use them. This is called the internal storage model.<p>
837
838Sometimes, however, it is nice to separate the command line option processing
839code from the storage of the value parsed. For example, lets say that we have a
840'<tt>-debug</tt>' option that we would like to use to enable debug information
841across the entire body of our program. In this case, the boolean value
842controlling the debug code should be globally accessable (in a header file, for
843example) yet the command line option processing code should not be exposed to
844all of these clients (requiring lots of .cpp files to #include
845<tt>CommandLine.h</tt>).<p>
846
847To do this, set up your .h file with your option, like this for example:<p>
848
849<pre>
850<i>// DebugFlag.h - Get access to the '-debug' command line option
851//
852
853// DebugFlag - This boolean is set to true if the '-debug' command line option
854// is specified. This should probably not be referenced directly, instead, use
855// the DEBUG macro below.
856//</i>
857extern bool DebugFlag;
858
859<i>// DEBUG macro - This macro should be used by code to emit debug information.
860// In the '-debug' option is specified on the command line, and if this is a
861// debug build, then the code specified as the option to the macro will be
862// executed. Otherwise it will not be. Example:
863//
864// DEBUG(cerr << "Bitset contains: " << Bitset << "\n");
865//</i>
866<font color=red>#ifdef NDEBUG
867#define DEBUG(X)
868#else
869#define DEBUG(X)</font> \
870 do { if (DebugFlag) { X; } } while (0)
871<font color=red>#endif</font>
872</pre>
873
874This allows clients to blissfully use the <tt>DEBUG()</tt> macro, or the
875<tt>DebugFlag</tt> explicitly if they want to. Now we just need to be able to
876set the <tt>DebugFlag</tt> boolean when the option is set. To do this, we pass
877an additial argument to our command line argument processor, and we specify
878where to fill in with the <a href="#cl::location">cl::location</a> attribute:<p>
879
880<pre>
881bool DebugFlag; <i>// the actual value</i>
Chris Lattner589a4cc2003-08-01 21:30:37 +0000882static <a href="#cl::opt">cl::opt</a>&lt;bool, true&gt; <i>// The parser</i>
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000883Debug("<i>debug</i>", <a href="#cl::desc">cl::desc</a>("<i>Enable debug output</i>")</a>, <a href="#cl::Hidden">cl::Hidden</a>,
884 <a href="#cl::location">cl::location</a>(DebugFlag));
885</pre>
886
887In the above example, we specify "<tt>true</tt>" as the second argument to the
888<a href="#cl::opt">cl::opt</a> template, indicating that the template should not
889maintain a copy of the value itself. In addition to this, we specify the <a
890href="#cl::location">cl::location</a> attribute, so that <tt>DebugFlag</tt> is
891automatically set.<p>
892
893
894
895<!-- ======================================================================= -->
896</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>
897<a name="attributes">Option Attributes
898</b></font></td></tr></table><ul>
899
900This section describes the basic attributes that you can specify on options.<p>
901
902<ul>
903
904<li>The option name attribute (which is required for all options, except <a
905href="#positional">positional options</a>) specifies what the option name is.
906This option is specified in simple double quotes:<p>
907
Chris Lattnerc1ae40c2002-08-07 18:27:04 +0000908<pre>
909<a href="#cl::opt">cl::opt</a>&lt;<b>bool</b>&gt; Quiet("<i>quiet</i>");
910</pre><p>
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000911
912<li><a name="cl::desc">The <b><tt>cl::desc</tt></b> attribute specifies a
913description for the option to be shown in the <tt>--help</tt> output for the
914program.<p>
915
916<li><a name="cl::value_desc">The <b><tt>cl::value_desc</tt></b> attribute
917specifies a string that can be used to fine tune the <tt>--help</tt> output for
918a command line option. Look <a href="#value_desc_example">here</a> for an
919example.<p>
920
921<li><a name="cl::init">The <b><tt>cl::init</tt></b> attribute specifies an
922inital value for a <a href="#cl::opt">scalar</a> option. If this attribute is
923not specified then the command line option value defaults to the value created
Brian Gaeke9d292ff2003-08-19 22:56:22 +0000924by the default constructor for the type. <b>Warning</b>: If you specify both
925<b><tt>cl::init</tt></b> and <b><tt>cl::location</tt></b> for an option,
926you must specify <b><tt>cl::location</tt></b> first, so that when the
927command-line parser sees <b><tt>cl::init</tt></b>, it knows where to put the
928initial value. (You will get an error at runtime if you don't put them in
929the right order.)<p>
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000930
931<li><a name="cl::location">The <b><tt>cl::location</tt></b> attribute where to
932store the value for a parsed command line option if using external storage. See
933the section on <a href="#storage">Internal vs External Storage</a> for more
934information.<p>
935
936<li><a name="cl::aliasopt">The <b><tt>cl::aliasopt</tt></b> attribute specifies
937which option a <a href="#cl::alias">cl::alias</a> option is an alias for.<p>
938
939<li><a name="cl::values">The <b><tt>cl::values</tt></b> attribute specifies the
940string-to-value mapping to be used by the generic parser. It takes a <b>null
941terminated</b> list of (option, value, description) triplets that specify the
942option name, the value mapped to, and the description shown in the
943<tt>--help</tt> for the tool. Because the generic parser is used most frequently with enum values, two macros are often useful:<p>
944<ol>
945<li><a name="clEnumVal">The <b><tt>clEnumVal</tt></b> macro is used as a nice
946simple way to specify a triplet for an enum. This macro automatically makes the
947option name be the same as the enum name. The first option to the macro is the
948enum, the second is the description for the command line option.<p> <li><a
949name="clEnumValN">The <b><tt>clEnumValN</tt></b> macro is used to specify macro
950options where the option name doesn't equal the enum name. For this macro, the
951first argument is the enum value, the second is the flag name, and the second is
952the description.<p>
953</ol>
954
955You will get a compile time error if you try to use cl::values with a parser
956that does not support it.<p>
957
958</ul>
959
960
961
962<!-- ======================================================================= -->
963</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>
964<a name="modifiers">Option Modifiers
965</b></font></td></tr></table><ul>
966
967Option modifiers are the flags and expressions that you pass into the
968constructors for <tt><a href="#cl::opt">cl::opt</a></tt> and <tt><a
969href="#cl::list">cl::list</a></tt>. These modifiers give you the ability to
970tweak how options are parsed and how <tt>--help</tt> output is generated to fit
971your application well.<p>
972
Chris Lattner32a32842003-05-22 20:36:06 +0000973These options fall into five main catagories:<p>
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000974
975<ol>
976<li><a href="#hiding">Hiding an option from <tt>--help</tt> output</a>
977<li><a href="#numoccurances">Controlling the number of occurances
978 required and allowed</a>
979<li><a href="#valrequired">Controlling whether or not a value must be
980 specified</a>
981<li><a href="#formatting">Controlling other formatting options</a>
Chris Lattner32a32842003-05-22 20:36:06 +0000982<li><a href="#misc">Miscellaneous option modifiers</a>
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000983</ol><p>
984
985It is not possible to specify two options from the same catagory (you'll get a
Chris Lattner32a32842003-05-22 20:36:06 +0000986runtime error) to a single option, except for options in the miscellaneous
987catagory. The CommandLine library specifies defaults for all of these settings
988that are the most useful in practice and the most common, which mean that you
989usually shouldn't have to worry about these.<p>
Chris Lattnere76d4ab2002-08-06 19:36:06 +0000990
991
992<!-- _______________________________________________________________________ -->
993</ul><a name="hiding"><h4><hr size=0>Hiding an option from <tt>--help</tt> output</h4><ul>
994
995The <tt>cl::NotHidden</tt>, <tt>cl::Hidden</tt>, and <tt>cl::ReallyHidden</tt>
996modifiers are used to control whether or not an option appears in the
997<tt>--help</tt> and <tt>--help-hidden</tt> output for the compiled program:<p>
998
999<ul>
1000
1001<a name="cl::NotHidden">The <b><tt>cl::NotHidden</tt></b> modifier (which is the
1002default for <tt><a href="#cl::opt">cl::opt</a></tt> and <tt><a
1003href="#cl::list">cl::list</a></tt> options), indicates the option is to appear
1004in both help listings.<p>
1005
1006<a name="cl::Hidden">The <b><tt>cl::Hidden</tt></b> modifier (which is the
1007default for <tt><a href="#cl::alias">cl::alias</a></tt> options), indicates that
1008the option should not appear in the <tt>--help</tt> output, but should appear in
1009the <tt>--help-hidden</tt> output.<p>
1010
1011<a name="cl::ReallyHidden">The <b><tt>cl::ReallyHidden</tt></b> modifier,
1012indicates that the option should not appear in any help output.<p>
1013</ul>
1014
1015<!-- _______________________________________________________________________ -->
1016</ul><a name="numoccurances"><h4><hr size=0>Controlling the number of occurances required and allowed</h4><ul>
1017
1018This group of options is used to control how many time an option is allowed (or
1019required) to be specified on the command line of your program. Specifying a
1020value for this setting allows the CommandLine library to do error checking for
1021you.<p>
1022
1023The allowed values for this option group are:<p>
1024
1025<ul>
1026<a name="cl::Optional">The <b><tt>cl::Optional</tt></b> modifier (which is the
1027default for the <tt><a href="#cl::opt">cl::opt</a></tt> and <tt><a
1028href="#cl::alias">cl::alias</a></tt> classes) indicates that your program will
1029allow either zero or one occurance of the option to be specified.<p>
1030
1031<a name="cl::ZeroOrMore">The <b><tt>cl::ZeroOrMore</tt></b> modifier (which is
1032the default for the <tt><a href="#cl::list">cl::list</a></tt> class) indicates
1033that your program will allow the option to be specified zero or more times.<p>
1034
1035<a name="cl::Required">The <b><tt>cl::Required</tt></b> modifier indicates that
1036the specified option must be specified exactly one time.<p>
1037
1038<a name="cl::OneOrMore">The <b><tt>cl::OneOrMore</tt></b> modifier indicates
1039that the option must be specified at least one time.<p>
1040
1041The <b><tt>cl::ConsumeAfter</tt></b> modifier is described in the <a
1042href="#positional">Positional arguments section</a><p>
1043
1044</ul>
1045
1046If an option is not specified, then the value of the option is equal to the
1047value specified by the <tt><a href="#cl::init">cl::init</a></tt> attribute. If
1048the <tt><a href="#cl::init">cl::init</a></tt> attribute is not specified, the
1049option value is initialized with the default constructor for the data type.<p>
1050
1051If an option is specified multiple times for an option of the <tt><a
1052href="#cl::opt">cl::opt</a></tt> class, only the last value will be retained.<p>
1053
1054
1055<!-- _______________________________________________________________________ -->
1056</ul><a name="valrequired"><h4><hr size=0>Controlling whether or not a value must be specified</h4><ul>
1057
1058This group of options is used to control whether or not the option allows a
1059value to be present. In the case of the CommandLine library, a value is either
1060specified with an equal sign (e.g. '<tt>-index-depth=17</tt>') or as a trailing
1061string (e.g. '<tt>-o a.out</tt>').<p>
1062
1063The allowed values for this option group are:<p>
1064
1065<ul>
1066<a name="cl::ValueOptional">The <b><tt>cl::ValueOptional</tt></b> modifier
1067(which is the default for <tt>bool</tt> typed options) specifies that it is
1068acceptable to have a value, or not. A boolean argument can be enabled just by
1069appearing on the command line, or it can have an explicit '<tt>-foo=true</tt>'.
1070If an option is specified with this mode, it is illegal for the value to be
1071provided without the equal sign. Therefore '<tt>-foo true</tt>' is illegal. To
1072get this behavior, you must use the <a
1073href="#cl::ValueRequired">cl::ValueRequired</a> modifier.<p>
1074
1075<a name="cl::ValueRequired">The <b><tt>cl::ValueRequired</tt></b> modifier
1076(which is the default for all other types except for <a
1077href="#onealternative">unnamed alternatives using the generic parser</a>)
1078specifies that a value must be provided. This mode informs the command line
1079library that if an option is not provides with an equal sign, that the next
1080argument provided must be the value. This allows things like '<tt>-o
1081a.out</tt>' to work.<p>
1082
1083<a name="cl::ValueDisallowed">The <b><tt>cl::ValueDisallowed</tt></b> modifier
1084(which is the default for <a href="#onealternative">unnamed alternatives using
1085the 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>
1086
1087</ul>
1088
1089In general, the default values for this option group work just like you would
1090want them to. As mentioned above, you can specify the <a
1091href="#cl::ValueDisallowed">cl::ValueDisallowed</a> modifier to a boolean
1092argument to restrict your command line parser. These options are mostly useful
1093when <a href="#extensionguide">extending the library</a>.<p>
1094
1095
1096
1097<!-- _______________________________________________________________________ -->
1098</ul><a name="formatting"><h4><hr size=0>Controlling other formatting options</h4><ul>
1099
1100The formatting option group is used to specify that the command line option has
1101special abilities and is otherwise different from other command line arguments.
1102As usual, you can only specify at most one of these arguments.<p>
1103
1104<ul>
1105<a name="cl::NormalFormatting">The <b><tt>cl::NormalFormatting</tt></b> modifier
1106(which is the default all options) specifies that this option is "normal".<p>
1107
1108<a name="cl::Positional">The <b><tt>cl::Positional</tt></b> modifier specifies
1109that this is a positional argument, that does not have a command line option
1110associated with it. See the <a href="#positional">Positional Arguments</a>
1111section for more information.<p>
1112
1113The <b><a href="#cl::ConsumeAfter"><tt>cl::ConsumeAfter</tt></a></b> modifier
1114specifies that this option is used to capture "interpreter style" arguments. See <a href="#cl::ConsumeAfter">this section for more information</a>.<p>
1115
1116
1117<a name="cl::Prefix">The <b><tt>cl::Prefix</tt></b> modifier specifies that this
1118option prefixes its value. With 'Prefix' options, there is no equal sign that
Misha Brukmanbc0e9982003-07-14 17:20:40 +00001119separates the value from the option name specified. This is useful for
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001120processing odd arguments like '<tt>-lmalloc -L/usr/lib'</tt> in a linker tool.
1121Here, the '<tt>l</tt>' and '<tt>L</tt>' options are normal string (list)
1122options, that have the <a href="#cl::Prefix">cl::Prefix</a> modifier added to
1123allow the CommandLine library to recognize them. Note that <a
1124href="#cl::Prefix">cl::Prefix</a> options must not have the <a
1125href="#cl::ValueDisallowed">cl::ValueDisallowed</a> modifier specified.<p>
1126
1127<a name="cl::Grouping">The <b><tt>cl::Grouping</tt></b> modifier is used to
1128implement unix style tools (like <tt>ls</tt>) that have lots of single letter
1129arguments, but only require a single dash. For example, the '<tt>ls -labF</tt>'
1130command actually enables four different options, all of which are single
1131letters. Note that <a href="#cl::Grouping">cl::Grouping</a> options cannot have
1132values.<p>
1133
1134</ul>
1135
1136The CommandLine library does not restrict how you use the <a
1137href="#cl::Prefix">cl::Prefix</a> or <a href="#cl::Grouping">cl::Grouping</a>
1138modifiers, but it is possible to specify ambiguous argument settings. Thus, it
1139is possible to have multiple letter options that are prefix or grouping options,
1140and they will still work as designed.<p>
1141
1142To do this, the CommandLine library uses a greedy algorithm to parse the input
1143option into (potentially multiple) prefix and grouping options. The strategy
1144basically looks like this:<p>
1145
1146<tt>parse(string OrigInput) {</tt>
1147<ol>
1148<li><tt>string input = OrigInput;</tt>
1149<li><tt>if (isOption(input)) return getOption(input).parse();</tt>&nbsp;&nbsp;&nbsp;&nbsp;<i>// Normal option</i>
1150<li><tt>while (!isOption(input) &amp;&amp; !input.empty()) input.pop_back();</tt>&nbsp;&nbsp;&nbsp;&nbsp;<i>// Remove the last letter</i>
1151<li><tt>if (input.empty()) return error();</tt>&nbsp;&nbsp;&nbsp;&nbsp;<i>// No matching option</i>
1152<li><tt>if (getOption(input).isPrefix())<br>
1153&nbsp;&nbsp;return getOption(input).parse(input);</tt>
1154<li><tt>while (!input.empty()) {&nbsp;&nbsp;&nbsp;&nbsp;<i>// Must be grouping options</i><br>
1155&nbsp;&nbsp;getOption(input).parse();<br>
1156&nbsp;&nbsp;OrigInput.erase(OrigInput.begin(), OrigInput.begin()+input.length());<br>
1157&nbsp;&nbsp;input = OrigInput;<br>
1158&nbsp;&nbsp;while (!isOption(input) &amp;&amp; !input.empty()) input.pop_back();<br>
1159}</tt>
1160<li><tt>if (!OrigInput.empty()) error();</tt>
1161</tt>
1162
1163</ol>
1164<tt>}</tt><p>
1165
1166
Chris Lattner32a32842003-05-22 20:36:06 +00001167
1168<!-- _______________________________________________________________________ -->
1169</ul><a name="misc"><h4><hr size=0>Miscellaneous option modifiers</h4><ul>
1170
1171The miscellaneous option modifiers are the only flags where you can specify more
1172than one flag from the set: they are not mutually exclusive. These flags
1173specify boolean properties that modify the option.<p>
1174
1175<ul>
1176
1177<a name="cl::CommaSeparated">The <b><tt>cl::CommaSeparated</tt></b> modifier
1178indicates that any commas specified for an option's value should be used to
1179split the value up into multiple values for the option. For example, these two
1180options are equivalent when <tt>cl::CommaSeparated</tt> is specified:
1181"<tt>-foo=a -foo=b -foo=c</tt>" and "<tt>-foo=a,b,c</tt>". This option only
1182makes sense to be used in a case where the option is allowed to accept one or
1183more values (i.e. it is a <a href="#cl::list">cl::list</a> option).<p>
1184</ul>
1185
1186So far, the only miscellaneous option modifier is the
1187<tt>cl::CommaSeparated</tt> modifier.<p>
1188
1189
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001190<!-- ======================================================================= -->
1191</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 +00001192<a name="toplevel">Top-Level Classes and Functions
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001193</b></font></td></tr></table><ul>
1194
1195Despite all of the builtin flexibility, the CommandLine option library really
Chris Lattnerc1ae40c2002-08-07 18:27:04 +00001196only consists of one function (<a
1197href="#cl::ParseCommandLineOptions"><tt>cl::ParseCommandLineOptions</tt></a>)
1198and three main classes: <a href="#cl::opt"><tt>cl::opt</tt></a>, <a
1199href="#cl::list"><tt>cl::list</tt></a>, and <a
1200href="#cl::alias"><tt>cl::alias</tt></a>. This section describes these three
1201classes in detail.<p>
1202
1203<!-- _______________________________________________________________________ -->
1204</ul><a name="cl::ParseCommandLineOptions"><h4><hr size=0>The
1205<tt>cl::ParseCommandLineOptions</tt> function</h4><ul>
1206
1207The <tt>cl::ParseCommandLineOptions</tt> function is designed to be called
1208directly from <tt>main</tt>, and is used to fill in the values of all of the
1209command line option variables once <tt>argc</tt> and <tt>argv</tt> are
1210available.<p>
1211
1212The <tt>cl::ParseCommandLineOptions</tt> function requires two parameters
1213(<tt>argc</tt> and <tt>argv</tt>), but may also take an optional third parameter
1214which holds <a href="#description">additional extra text</a> to emit when the
1215<tt>--help</tt> option is invoked.<p>
1216
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001217
1218<!-- _______________________________________________________________________ -->
Brian Gaekee5842852003-08-19 23:11:43 +00001219</ul><a name="cl::ParseEnvironmentOptions"><h4><hr size=0>The
1220<tt>cl::ParseEnvironmentOptions</tt> function</h4><ul>
1221
1222The <tt>cl::ParseEnvironmentOptions</tt>
1223function has mostly the same effects as
1224<a href="#cl::ParseCommandLineOptions"><tt>cl::ParseCommandLineOptions</tt></a>,
1225except that it is designed to take values for options from an
1226environment variable, for those cases in which reading the
1227command line is not convenient or not desired. It fills in
1228the values of all the command line option variables just like
1229<a href="#cl::ParseCommandLineOptions"><tt>cl::ParseCommandLineOptions</tt></a>
1230does.<p>
1231
1232It takes three parameters: first, the name of the program (since <tt>argv</tt>
1233may not be available, it can't just look in <tt>argv[0]</tt>), second,
1234the name of the environment variable to examine, and third, the optional
1235<a href="#description">additional extra text</a> to emit when the
1236<tt>--help</tt> option is invoked.<p>
1237
1238<tt>cl::ParseEnvironmentOptions</tt> will break the environment
1239variable's value up into words and then process them using
1240<a href="#cl::ParseCommandLineOptions"><tt>cl::ParseCommandLineOptions</tt></a>.
1241<b>Note:</b> Currently <tt>cl::ParseEnvironmentOptions</tt> does not support
1242quoting, so an environment variable containing <tt>-option "foo bar"</tt> will
1243be parsed as three words, <tt>-option</tt>, <tt>"foo</tt>, and <tt>bar"</tt>,
1244which is different from what you would get from the shell with the same
1245input.<p>
1246
1247<!-- _______________________________________________________________________ -->
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001248</ul><a name="cl::opt"><h4><hr size=0>The <tt>cl::opt</tt> class</h4><ul>
1249
1250The <tt>cl::opt</tt> class is the class used to represent scalar command line
1251options, and is the one used most of the time. It is a templated class which
1252can take up to three arguments (all except for the first have default values
1253though):<p>
1254
1255<pre>
Chris Lattnerc1ae40c2002-08-07 18:27:04 +00001256<b>namespace</b> cl {
1257 <b>template</b> &lt;<b>class</b> DataType, <b>bool</b> ExternalStorage = <b>false</b>,
1258 <b>class</b> ParserClass = parser&lt;DataType&gt; &gt;
1259 <b>class</b> opt;
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001260}
1261</pre><p>
1262
1263The first template argument specifies what underlying data type the command line
1264argument is, and is used to select a default parser implementation. The second
1265template argument is used to specify whether the option should contain the
1266storage for the option (the default) or whether external storage should be used
1267to contain the value parsed for the option (see <a href="#storage">Internal vs
1268External Storage</a> for more information).<p>
1269
1270The third template argument specifies which parser to use. The default value
1271selects an instantiation of the <tt>parser</tt> class based on the underlying
1272data type of the option. In general, this default works well for most
1273applications, so this option is only used when using a <a
1274href="#customparser">custom parser</a>.<p>
1275
1276
1277<!-- _______________________________________________________________________ -->
1278</ul><a name="cl::list"><h4><hr size=0>The <tt>cl::list</tt> class</h4><ul>
1279
1280The <tt>cl::list</tt> class is the class used to represent a list of command
1281line options. It too is a templated class which can take up to three
1282arguments:<p>
1283
1284<pre>
Chris Lattnerc1ae40c2002-08-07 18:27:04 +00001285<b>namespace</b> cl {
1286 <b>template</b> &lt;<b>class</b> DataType, <b>class</b> Storage = <b>bool</b>,
1287 <b>class</b> ParserClass = parser&lt;DataType&gt; &gt;
1288 <b>class</b> list;
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001289}
1290</pre><p>
1291
1292This class works the exact same as the <a href="#cl::opt"><tt>cl::opt</tt></a>
1293class, except that the second argument is the <b>type</b> of the external
1294storage, not a boolean value. For this class, the marker type '<tt>bool</tt>'
1295is used to indicate that internal storage should be used.<p>
1296
1297
1298<!-- _______________________________________________________________________ -->
1299</ul><a name="cl::alias"><h4><hr size=0>The <tt>cl::alias</tt> class</h4><ul>
1300
1301The <tt>cl::alias</tt> class is a nontemplated class that is used to form
1302aliases for other arguments.<p>
1303
1304<pre>
Chris Lattnerc1ae40c2002-08-07 18:27:04 +00001305<b>namespace</b> cl {
1306 <b>class</b> alias;
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001307}
1308</pre></p>
1309
1310The <a href="#cl::aliasopt"><tt>cl::aliasopt</tt></a> attribute should be used
1311to specify which option this is an alias for. Alias arguments default to being
1312<a href="#cl::Hidden">Hidden</a>, and use the aliased options parser to do the
1313conversion from string to data.<p>
1314
1315
1316<!-- ======================================================================= -->
1317</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>
1318<a name="builtinparsers">Builtin parsers
1319</b></font></td></tr></table><ul>
1320
1321Parsers control how the string value taken from the command line is translated
1322into a typed value, suitable for use in a C++ program. By default, the
1323CommandLine library uses an instance of <tt>parser&lt;type&gt;</tt> if the
1324command line option specifies that it uses values of type '<tt>type</tt>'.
1325Because of this, custom option processing is specified with specializations of
1326the '<tt>parser</tt>' class.<p>
1327
1328The CommandLine library provides the following builtin parser specializations,
1329which are sufficient for most applications. It can, however, also be extended to
1330work with new data types and new ways of interpreting the same data. See the <a
1331href="#customparser">Writing a Custom Parser</a> for more details on this type
1332of library extension.<p>
1333
1334<li><a name="genericparser">The <b>generic <tt>parser&lt;t&gt;</tt> parser</b>
1335can be used to map strings values to any data type, through the use of the <a
1336href="#cl::values">cl::values</a> property, which specifies the mapping
1337information. The most common use of this parser is for parsing enum values,
1338which allows you to use the CommandLine library for all of the error checking to
1339make sure that only valid enum values are specified (as opposed to accepting
1340arbitrary strings). Despite this, however, the generic parser class can be used
1341for any data type.<p>
1342
1343<li><a name="boolparser">The <b><tt>parser&lt;bool&gt;</tt> specialization</b>
1344is used to convert boolean strings to a boolean value. Currently accepted
1345strings are "<tt>true</tt>", "<tt>TRUE</tt>", "<tt>True</tt>", "<tt>1</tt>",
1346"<tt>false</tt>", "<tt>FALSE</tt>", "<tt>False</tt>", and "<tt>0</tt>".<p>
1347
1348<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>
1349
1350<li><a name="intparser">The <b><tt>parser&lt;int&gt;</tt> specialization</b>
1351uses the C <tt>strtol</tt> function to parse the string input. As such, it will
1352accept a decimal number (with an optional '+' or '-' prefix) which must start
1353with a non-zero digit. It accepts octal numbers, which are identified with a
1354'<tt>0</tt>' prefix digit, and hexadecimal numbers with a prefix of
1355'<tt>0x</tt>' or '<tt>0X</tt>'.<p>
1356
1357<li><a name="doubleparser">The <b><tt>parser&lt;double&gt;</tt></b> and
1358<b><tt>parser&lt;float&gt;</tt> specializations</b> use the standard C
1359<tt>strtod</tt> function to convert floating point strings into floating point
1360values. As such, a broad range of string formats is supported, including
1361exponential notation (ex: <tt>1.7e15</tt>) and properly supports locales.
1362<p>
1363
Chris Lattner209c7f42001-07-23 23:03:12 +00001364
1365
1366<!-- *********************************************************************** -->
1367</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>
1368<a name="extensionguide">Extension Guide
1369</b></font></td></tr></table><ul>
1370<!-- *********************************************************************** -->
1371
Chris Lattnerc1ae40c2002-08-07 18:27:04 +00001372Although the CommandLine library has a lot of functionality built into it
1373already (as discussed previously), one of its true strengths lie in its
1374extensibility. This section discusses how the CommandLine library works under
1375the covers and illustrates how to do some simple, common, extensions.<p>
1376
Chris Lattner209c7f42001-07-23 23:03:12 +00001377
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001378<!-- ======================================================================= -->
Chris Lattnerc1ae40c2002-08-07 18:27:04 +00001379</ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0>
1380<tr><td>&nbsp;</td><td width="100%">&nbsp; <font color="#EEEEFF"
1381face="Georgia,Palatino"><b> <a name="customparser">Writing a custom parser
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001382</b></font></td></tr></table><ul>
1383
Chris Lattnerc1ae40c2002-08-07 18:27:04 +00001384One of the simplest and most common extensions is the use of a custom parser.
1385As <a href="#builtinparsers">discussed previously</a>, parsers are the portion
1386of the CommandLine library that turns string input from the user into a
1387particular parsed data type, validating the input in the process.<p>
1388
1389There are two ways to use a new parser:<p>
1390
1391<ol>
1392<li>Specialize the <a href="#genericparser"><tt>cl::parser</tt></a> template for
1393 your custom data type.<p>
1394
1395 This approach has the advantage that users of your custom data type will
1396 automatically use your custom parser whenever they define an option with a
1397 value type of your data type. The disadvantage of this approach is that it
1398 doesn't work if your fundemental data type is something that is already
1399 supported.<p>
1400
1401<li>Write an independant class, using it explicitly from options that need
1402 it.<p>
1403
1404 This approach works well in situations where you would line to parse an
1405 option using special syntax for a not-very-special data-type. The drawback
1406 of this approach is that users of your parser have to be aware that they are
1407 using your parser, instead of the builtin ones.<p>
1408
1409</ol><p>
1410
1411To guide the discussion, we will discuss a custom parser that accepts file
1412sizes, specified with an optional unit after the numeric size. For example, we
1413would like to parse "102kb", "41M", "1G" into the appropriate integer value. In
1414this case, the underlying data type we want to parse into is
1415'<tt>unsigned</tt>'. We choose approach #2 above because we don't want to make
1416this the default for all <tt>unsigned</tt> options.<p>
1417
1418To start out, we declare our new <tt>FileSizeParser</tt> class:<p>
1419
1420<pre>
1421<b>struct</b> FileSizeParser : <b>public</b> cl::basic_parser&lt;<b>unsigned</b>&gt; {
1422 <i>// parse - Return true on error.</i>
1423 <b>bool</b> parse(cl::Option &amp;O, <b>const char</b> *ArgName, <b>const</b> std::string &amp;ArgValue,
1424 <b>unsigned</b> &amp;Val);
1425};
1426</pre><p>
1427
1428Our new class inherits from the <tt>cl::basic_parser</tt> template class to fill
1429in the default, boiler plate, code for us. We give it the data type that we
1430parse into (the last argument to the <tt>parse</tt> method so that clients of
1431our custom parser know what object type to pass in to the parse method (here we
1432declare that we parse into '<tt>unsigned</tt>' variables.<p>
1433
1434For most purposes, the only method that must be implemented in a custom parser
1435is the <tt>parse</tt> method. The <tt>parse</tt> method is called whenever the
1436option is invoked, passing in the option itself, the option name, the string to
1437parse, 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>
1438
1439<pre>
1440<b>bool</b> FileSizeParser::parse(cl::Option &amp;O, <b>const char</b> *ArgName,
1441 <b>const</b> std::string &amp;Arg, <b>unsigned</b> &amp;Val) {
1442 <b>const char</b> *ArgStart = Arg.c_str();
1443 <b>char</b> *End;
1444
1445 <i>// Parse integer part, leaving 'End' pointing to the first non-integer char</i>
1446 Val = (unsigned)strtol(ArgStart, &amp;End, 0);
1447
1448 <b>while</b> (1) {
1449 <b>switch</b> (*End++) {
1450 <b>case</b> 0: <b>return</b> false; <i>// No error</i>
1451 <b>case</b> 'i': <i>// Ignore the 'i' in KiB if people use that</i>
1452 <b>case</b> 'b': <b>case</b> 'B': <i>// Ignore B suffix</i>
1453 <b>break</b>;
1454
1455 <b>case</b> 'g': <b>case</b> 'G': Val *= 1024*1024*1024; <b>break</b>;
1456 <b>case</b> 'm': <b>case</b> 'M': Val *= 1024*1024; <b>break</b>;
1457 <b>case</b> 'k': <b>case</b> 'K': Val *= 1024; <b>break</b>;
1458
1459 default:
1460 <i>// Print an error message if unrecognized character!</i>
1461 <b>return</b> O.error(": '" + Arg + "' value invalid for file size argument!");
1462 }
1463 }
1464}
1465</pre><p>
1466
1467This function implements a very simple parser for the kinds of strings we are
1468interested in. Although it has some holes (it allows "<tt>123KKK</tt>" for
1469example), it is good enough for this example. Note that we use the option
1470itself to print out the error message (the <tt>error</tt> method always returns
1471true) in order to get a nice error message (shown below). Now that we have our
1472parser class, we can use it like this:<p>
1473
1474<pre>
1475<b>static</b> <a href="#cl::opt">cl::opt</a>&lt;<b>unsigned</b>, <b>false</b>, FileSizeParser&gt;
1476MFS(<i>"max-file-size"</i>, <a href="#cl::desc">cl::desc</a>(<i>"Maximum file size to accept"</i>),
1477 <a href="#cl::value_desc">cl::value_desc</a>("<i>size</i>"));
1478</pre><p>
1479
1480Which adds this to the output of our program:<p>
1481
1482<pre>
1483OPTIONS:
1484 -help - display available options (--help-hidden for more)
1485 ...
1486 <b>-max-file-size=&lt;size&gt; - Maximum file size to accept</b>
1487</pre><p>
1488
1489And we can test that our parse works correctly now (the test program just prints
1490out the max-file-size argument value):<p>
1491
1492<pre>
1493$ ./test
1494MFS: 0
1495$ ./test -max-file-size=123MB
1496MFS: 128974848
1497$ ./test -max-file-size=3G
1498MFS: 3221225472
1499$ ./test -max-file-size=dog
1500-max-file-size option: 'dog' value invalid for file size argument!
1501</pre><p>
1502
1503It looks like it works. The error message that we get is nice and helpful, and
1504we seem to accept reasonable file sizes. This wraps up the "custom parser"
1505tutorial.<p>
1506
1507
1508<!-- ======================================================================= -->
1509</ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0>
1510<tr><td>&nbsp;</td><td width="100%">&nbsp; <font color="#EEEEFF"
1511face="Georgia,Palatino"><b> <a name="explotingexternal">Exploiting external
1512storage </b></font></td></tr></table><ul>
1513
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001514
1515
1516<!-- ======================================================================= -->
Chris Lattnerc1ae40c2002-08-07 18:27:04 +00001517</ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0>
1518<tr><td>&nbsp;</td><td width="100%">&nbsp; <font color="#EEEEFF"
1519face="Georgia,Palatino"><b> <a name="dynamicopts">Dynamically adding command
1520line options </b></font></td></tr></table><ul>
Chris Lattnere76d4ab2002-08-06 19:36:06 +00001521
1522
Chris Lattner209c7f42001-07-23 23:03:12 +00001523
1524
1525
1526<!-- *********************************************************************** -->
1527</ul>
1528<!-- *********************************************************************** -->
1529
1530<hr>
1531<font size=-1>
1532<address><a href="mailto:sabre@nondot.org">Chris Lattner</a></address>
1533<!-- Created: Tue Jan 23 15:19:28 CST 2001 -->
1534<!-- hhmts start -->
Chris Lattner589a4cc2003-08-01 21:30:37 +00001535Last modified: Fri Aug 1 16:30:11 CDT 2003
Chris Lattner209c7f42001-07-23 23:03:12 +00001536<!-- hhmts end -->
1537</font>
1538</body></html>