New testcase for problem effecting mst
[oota-llvm.git] / docs / CommandLine.html
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2 <html><head><title>CommandLine Library Manual</title></head>
3 <body bgcolor=white>
4
5 <table width="100%" bgcolor="#330077" border=0 cellpadding=4 cellspacing=0>
6 <tr><td>&nbsp; <font size=+5 color="#EEEEFF" face="Georgia,Palatino,Times,Roman"><b>CommandLine Library Manual</b></font></td>
7 </tr></table>
8
9 <ol>
10   <li><a href="#introduction">Introduction</a>
11   <li><a href="#quickstart">Quick Start Guide</a>
12     <ol>
13       <li><a href="#flags">Flag Arguments</a>
14       <li><a href="#aliases">Argument Aliases</a>
15       <li><a href="#onealternative">Selecting one alternative from a set</a>
16       <li><a href="#namedalternatives">Named alternatives</a>
17       <li><a href="#enumlist">Parsing a list of options</a>
18       <li><a href="#stringlist">Parsing a list of non-options</a>
19     </ol>
20   <li><a href="#referenceguide">Reference Guide</a>
21   <li><a href="#extensionguide">Extension Guide</a>
22 </ol><p>
23
24
25 <!-- *********************************************************************** -->
26 </ul><table width="100%" bgcolor="#330077" border=0 cellpadding=4 cellspacing=0>
27 <tr><td align=center><font color="#EEEEFF" size=+2 face="Georgia,Palatino"><b>
28 <a name="introduction">Introduction
29 </b></font></td></tr></table><ul>
30 <!-- *********************************************************************** -->
31
32 This document describes the CommandLine argument processing library.  It will show you how to use it, and what it can do.<p>
33
34 Although there are a <b>lot</b> of command line argument parsing libraries out there in many different languages, none of them fit well with what I needed.  By looking at the features and problems of other libraries, I designed the CommandLine library to have the following features:<p>
35
36 <ol>
37 <li>Speed: The CommandLine library is very quick and uses little resources.  The parsing time of the library is directly proportional to the number of arguments parsed, not the the number of options recognized.  Additionally, command line argument values are captured transparently into user defined variables, which can be accessed like any other variable (and with the same performance).<p>
38
39 <li>Type Safe: As a user of CommandLine, you don't have to worry about remembering the type of arguments that you want (is it an int?  a string? a bool? an enum?) and keep casting it around.  Not only does this help prevent error prone constructs, it also leads to dramatically cleaner source code.<p>
40
41 <li>No subclasses required: To use CommandLine, you instantiate variables that correspond to the arguments that you would like to capture, you don't subclass a parser.  This leads to much less boilerplate code.<p>
42
43 <li>Globally accessible: Libraries can specify command line arguments that are automatically enabled in any tool that links to the library.  This is possible because the application doesn't have to keep a "list" of arguments to pass to the parser.<p>
44
45 <li>More Clean: CommandLine supports enum types directly, meaning that there is less error and more security built into the library.  You don't have to worry about whether your integral command line argument accidentally got assigned a value that is not valid for your enum type.<p>
46
47 <li>Powerful: The CommandLine library supports many different types of arguments, from simple boolean flags to scalars arguments (strings, integers, enums, doubles), to lists of arguments.  This is possible because CommandLine is...<p>
48
49 <li>Extensible: It is very simple to add a new argument type to CommandLine.  Simply subclass the <tt>cl::Option</tt> and customize its behaviour however you would like.<p>
50
51 <li>Labor Saving: The CommandLine library cuts down on the amount of grunt work that you, the user, have to do.  For example, it automatically provides a --help option that shows the available command line options for your tool.<p>
52 </ol>
53
54 This document will hopefully let you jump in and start using CommandLine in your utility quickly and painlessly.  Additionally it should be a simple reference manual to figure out how stuff works.  If it is failing in some area, nag the author, <a href="mailto:sabre@nondot.org">Chris Lattner</a>.<p>
55
56
57 <!-- *********************************************************************** -->
58 </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>
59 <a name="quickstart">Quick Start Guide
60 </b></font></td></tr></table><ul>
61 <!-- *********************************************************************** -->
62
63 This section of the manual runs through a simple CommandLine'ification of a utility, a program optimizer.  This is intended to show you how to jump into using the CommandLine library in your own program, and show you some of the cool things it can do.<p>
64
65 To start out, you need to include the CommandLine header file into your program:<p>
66
67 <pre>
68   #include "CommandLine.h"
69 </pre><p>
70
71 Additionally, you need to add this as the first line of your main program:<p>
72
73 <pre>
74 int main(int argc, char **argv) {
75   cl::ParseCommandLineOptions(argc, argv);
76   ...
77 }
78 </pre><p>
79
80 ... which actually parses the arguments and fills in the variable declarations.<p>
81
82 Now that you are ready to support command line arguments, we need to tell the system which ones we want, and what type of argument they are.  The CommandLine library uses the model of variable declarations to capture command line arguments.  This means that for every command line option that you would like to support, there should be a variable declaration to capture the result.  For example, in our optimizer, we would like to support the unix standard '<tt>-o &lt;filename&gt;</tt>' option to specify where to put the output.  With the CommandLine library, this is represented like this:<p>
83
84 <pre>
85 cl::String OutputFilename("<i>o</i>", "<i>Specify output filename</i>");
86 </pre><p>
87
88 or more verbosely, like this:<p>
89
90 <pre>
91 cl::String OutputFilename("<i>o</i>", "<i>Specify output filename</i>", cl::NoFlags, "");
92 </pre><p>
93
94 This declares a variable "<tt>OutputFilename</tt>" that is used to capture the result of the "<tt>o</tt>" argument (first parameter).  The help text that is associated with the option is specified as the second argument to the constructor.  The type of the variable is "<tt>cl::String</tt>", which stands for CommandLine string argument.  This variable may be used in any context that a normal C++ string object may be used.  For example:<p>
95
96 <pre>
97   ...
98   ofstream Output(OutputFilename.c_str());
99   if (Out.good()) ...
100   ...
101 </pre><p>
102
103 The two optional arguments (shown in the verbose example) show that you can pass "flags" to control the behavior of the argument (discussed later), and a default value for the argument (which is normally just an empty string, but you can override it if you would like).<p>
104
105 In addition, we would like to specify an input filename as well, but without an associated flag (i.e. we would like for the optimizer to be run like this: "<tt>opt [flags] sourcefilename.c</tt>").  To support this style of argument, the CommandLine library allows one "unnamed" argument to be specified for the program.  In our case it would look like this:<p>
106
107 <pre>
108 cl::String InputFilename("", "<i>Source file to optimize</i>", cl::NoFlags, "<i>-</i>");
109 </pre>
110
111 This declaration indicates that an unbound option should be treated as the input filename... and if one is not specified, a default value of "-" is desired (which is commonly used to refer to standard input).  If you would like to require that the user of your tool specify an input filename, you can mark the argument as such with the "<tt>cl::Required</tt>" flag:<p>
112
113 <pre>
114 cl::String InputFilename("", "<i>Source file to optimize</i>", <b>cl::Required</b>, "<i>-</i>");
115 </pre>
116
117 The CommandLine library will then issue an error if the argument is not specified (this flag can, of course, be applied to any argument type).  This is one example of how using flags can alter the default behaviour of the library, on a per-option basis.<p>
118
119 <!-- ======================================================================= -->
120 </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>
121 <a name="flags">Flag Arguments
122 </b></font></td></tr></table><ul>
123
124 In addition to input and output filenames, we would like the optimizer to support three boolean flags: "<tt>-f</tt>" to force overwriting of the output file, "<tt>--quiet</tt>" to enable quiet mode, and "<tt>-q</tt>" for backwards compatibility with some of our users.  We can support these with the "<tt>cl::Flag</tt>" declaration like this:<p>
125
126 <pre>
127 cl::Flag Force ("<i>f</i>", "<i>Overwrite output files</i>", cl::NoFlags, false);
128 cl::Flag Quiet ("<i>q</i>", "<i>Don't print informational messages</i>", cl::Hidden);
129 cl::Flag Quiet2("<i>quiet</i>", "<i>Don't print informational messages</i>", cl::NoFlags);
130 </pre><p>
131
132 This does what you would expect: it declares three boolean variables ("<tt>Force</tt>", "<tt>Quiet</tt>", and "<tt>Quiet2</tt>") to recognize these options.  Note that the "<tt>-q</tt>" option is specified with the "<tt>cl::Hidden</tt>" flag.  This prevents it from being shown by the standard "<tt>--help</tt>" command line argument provided.  With these declarations, "<tt>opt --help</tt>" emits this:<p>
133
134 <pre>
135 USAGE: opt [options]
136
137 OPTIONS:
138   -f     - Overwrite output files
139   -o     - Override output filename
140   -quiet - Don't print informational messages
141   -help  - display available options (--help-hidden for more)
142 </pre><p>
143
144 and "<tt>opt --help-hidden</tt>" emits this:<p>
145
146 <pre>
147 USAGE: opt [options]
148
149 OPTIONS:
150   -f     - Overwrite output files
151   -o     - Override output filename
152   -q     - Don't print informational messages
153   -quiet - Don't print informational messages
154   -help  - display available options (--help-hidden for more)
155 </pre><p>
156
157 This brief example has shown you how to use simple scalar command line arguments, by using the "<tt>cl::String</tt>" and "<tt>cl::Flag</tt>" classes.  In addition to these classes, there are also "<tt>cl::Int</tt>" and "<tt>cl::Double</tt>" classes that work analagously.<p>
158
159
160 <!-- ======================================================================= -->
161 </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>
162 <a name="aliases">Argument Aliases
163 </b></font></td></tr></table><ul>
164
165 This works well, except for the fact that we need to check the quiet condition like this now:<p>
166
167 <pre>
168 ...
169   if (!Quiet &amp;&amp; !Quiet2) printInformationalMessage(...);
170 ...
171 </pre><p>
172
173 ... which is a real pain!  Instead of defining two values for the same condition, we can use the "<tt>cl::Alias</tt>" to make the "<tt>-q</tt>" option an <b>alias</b> for "<tt>-quiet</tt>" instead of a value itself:<p>
174
175 <pre>
176 cl::Flag  Force ("<i>f</i>", "<i>Overwrite output files</i>", cl::NoFlags, false);
177 cl::Flag  Quiet ("<i>quiet</i>", "<i>Don't print informational messages</i>", cl::NoFlags, false);
178 cl::Alias QuietA("<i>q</i>", "<i>Alias for -quiet</i>", cl::NoFlags, Quiet);
179 </pre><p>
180
181 Which does exactly what we want... and the alias is automatically hidden from the "<tt>--help</tt>" output.  Note how the alias specifies the variable that it wants to alias to, the alias argument name, and the help description (shown by "<tt>--help-hidden</tt>") of the alias.  Now your user code can simply use:<p>
182
183 <pre>
184 ...
185   if (!Quiet) printInformationalMessage(...);
186 ...
187 </pre><p>
188
189 ... which is much nicer!  The "<tt>cl::Alias</tt>" can be used to specify an alternative name for any variable type, and has many uses.<p>
190
191
192 <!-- ======================================================================= -->
193 </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>
194 <a name="onealternative">Selecting one alternative from a set
195 </b></font></td></tr></table><ul>
196
197 Often we would like to be able to enable one option from a set of options.  The CommandLine library has a couple of different ways to do this... the first is with the "<tt>cl::EnumFlags</tt> class.<p>
198
199 Lets say that we would like to add four optimizations levels to our optimizer, using the standard flags "<tt>-g</tt>", "<tt>-O0</tt>", "<tt>-O1</tt>", and "<tt>-O2</tt>".  We could easily implement this with the "<tt>cl::Flag</tt>" class above, but there are several problems with this strategy:<p>
200
201 <ol>
202 <li>A user could specify more than one of the options at a time, for example, "<tt>opt -O3 -O2</tt>".  The CommandLine library would not catch this erroneous input for us.
203 <li>We would have to test 4 different variables to see which ones are set.
204 <li>This doesn't map to the numeric levels that we want... so we cannot easily see if some level &gt;= "<tt>-O1</tt>" is enabled.
205 </ol><p>
206
207 To cope with these problems, the CommandLine library provides the "<tt>cl::EnumFlags</tt> class, which is used like this:<p>
208
209 <pre>
210 enum OptLevel {
211   g, O1, O2, O3
212 };
213
214 cl::EnumFlags&lt;enum OptLevel&gt; OptimizationLevel(cl::NoFlags,
215   clEnumVal(g , "<i>No optimizations, enable debugging</i>"),
216   clEnumVal(O1, "<i>Enable trivial optimizations</i>"),
217   clEnumVal(O2, "<i>Enable default optimizations</i>"),
218   clEnumVal(O3, "<i>Enable expensive optimizations</i>"),
219  0);
220
221 ...
222   if (OptimizationLevel &gt;= O2) doGCSE(...);
223 ...
224 </pre><p>
225
226 This declaration defines a variable "<tt>OptimizationLevel</tt>" of the "<tt>OptLevel</tt>" enum type.  This variable can be assigned any of the values that are listed in the declaration (Note that the declaration list must be terminated with the "<tt>0</tt>" argument!).  The CommandLine library enforces that the user can only specify one of the options, and it ensure that only valid enum values can be specified.  The default value of the flag is the first value listed.
227
228
229 In addition to all of this, the CommandLine library automatically names the flag values the same as the enum values.<p>
230
231 In this case, it is sort of awkward that flag names correspond directly to enum names, because we probably don't want a enum definition named "<tt>g</tt>" in our program.  We could alternatively write this example like this:<p>
232
233 <pre>
234 enum OptLevel {
235   Debug, O1, O2, O3
236 };
237
238 cl::EnumFlags&lt;enum OptLevel&gt; OptimizationLevel(cl::NoFlags,
239  clEnumValN(Debug, "g", "<i>No optimizations, enable debugging</i>"),
240   clEnumVal(O1        , "<i>Enable trivial optimizations</i>"),
241   clEnumVal(O2        , "<i>Enable default optimizations</i>"),
242   clEnumVal(O3        , "<i>Enable expensive optimizations</i>"),
243  0);
244
245 ...
246   if (OptimizationLevel == Debug) outputDebugInfo(...);
247 ...
248 </pre><p>
249
250 By using the "<tt>clEnumValN</tt>" token instead of "<tt>clEnumVal</tt>", we can directly specify the name that the flag should get.<p>
251
252 <!-- ======================================================================= -->
253 </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>
254 <a name="namedalternatives">Named Alternatives
255 </b></font></td></tr></table><ul>
256
257 Another useful argument form is a named alternative style.  We shall use this style in our optimizer to specify different debug levels that can be used.  Instead of each debug level being its own switch, we want to support the following options, of which only one can be specified at a time: "<tt>--debug-level=none</tt>", "<tt>--debug-level=quick</tt>", "<tt>--debug-level=detailed</tt>".  To do this, we use the CommandLine "<tt>cl::Enum</tt>" class:<p>
258
259 <pre>
260 enum DebugLev {
261   nodebuginfo, quick, detailed
262 };
263
264 // Enable Debug Options to be specified on the command line
265 cl::Enum&lt;enum DebugLev&gt; DebugLevel("<i>debug_level</i>", cl::NoFlags,
266    "select debugging level",
267   clEnumValN(nodebuginfo, "none", "<i>disable debug information</i>"),
268    clEnumVal(quick,               "<i>enable quick debug information</i>"),
269    clEnumVal(detailed,            "<i>enable detailed debug information</i>"),
270  0);
271 </pre>
272
273 This definition defines an enumerated command line variable of type "<tt>enum DebugLev</tt>", with the same semantics as the "<tt>EnumFlags</tt>" definition does.  The difference here is just the interface exposed to the user of your program and the help output by the "<tt>--help</tt>" option:<p>
274
275 <pre>
276 ...
277 OPTIONS:
278   -debug_level - select debugging level
279     =none      - disable debug information
280     =quick     - enable quick debug information
281     =detailed  - enable detailed debug information
282   -g           - No optimizations, enable debugging
283   -O1          - Enable trivial optimizations
284   -O2          - Enable default optimizations
285   -O3          - Enable expensive optimizations
286   ...
287 </pre><p>
288
289 By providing both of these forms of command line argument, the CommandLine library lets the application developer choose the appropriate interface for the job.<p>
290
291
292 <!-- ======================================================================= -->
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>
294 <a name="enumlist">Parsing a list of options
295 </b></font></td></tr></table><ul>
296
297 Now that we have the standard run of the mill argument types out of the way, lets get a little wild and crazy.  Lets say that we want our optimizer to accept a <b>list</b> of optimizations to perform, allowing duplicates.  For example, we might want to run: "<tt>opt -dce -constprop -inline -dce -strip</tt>".  For this case, the order of the arguments and the number of appearances is very important.  This is what the "<tt>cl::EnumList</tt>" definition is for.  First, start by defining an enum of the optimizations that you would like to perform:<p>
298
299 <pre>
300 enum Opts {
301   // 'inline' is a C++ reserved word, so name it 'inlining'
302   dce, constprop, inlining, strip
303 }
304 </pre><p>
305
306 Then define your "<tt>cl::EnumList</tt>" variable:<p>
307
308 <pre>
309 cl::EnumList<enum Opts> OptimizationList(cl::NoFlags,
310   clEnumVal(dce               , "<i>Dead Code Elimination</i>"),
311   clEnumVal(constprop         , "<i>Constant Propogation</i>"),
312  clEnumValN(inlining, "<i>inline</i>", "<i>Procedure Integration</i>"),
313   clEnumVal(strip             , "<i>Strip Symbols</i>"),
314 0);
315 </pre><p>
316
317 This defines a variable that is conceptually of the type "<tt>vector&lt;enum Opts&gt;</tt>".  Thus, you can do operations like this:<p>
318
319 <pre>
320   for (unsigned i = 0; i &lt; OptimizationList.size(); ++i)
321     switch (OptimizationList[i])
322        ...
323 </pre>
324
325 ... to iterate through the list of options specified.
326
327
328
329
330
331 <!-- ======================================================================= -->
332 </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>
333 <a name="stringlist">Parsing a list of non-options
334 </b></font></td></tr></table><ul>
335
336 Often times it is convenient to have a "left over bin", that collects arguments that couldn't be parsed any other way.  For me, this typically occurs when I am writing a utility that takes a list of filenames to work on... a linker for example.  Each of these filenames isn't exactly a command line option, but we'd like for them to be parsed in a useful way.  To do this, we use the "<tt>cl::StringList</tt>" class.<p>
337
338 <pre>
339 ...
340 cl::StringList InputFilenames("", "Load <arg> files, linking them together", 
341                               cl::OneOrMore);
342 ...
343 </pre><p>
344
345 This variable works just like a "<tt>vector&lt;string&gt;</tt>" object.  As such, iteration is simple:<p>
346
347 <pre>
348   for (unsigned i = 0; i < InputFilenames.size(); ++i)
349         cout << "Found an argument: " << InputFilenames[i] << endl;
350 </pre><p>
351
352
353 <!-- *********************************************************************** -->
354 </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>
355 <a name="referenceguide">Reference Guide
356 </b></font></td></tr></table><ul>
357 <!-- *********************************************************************** -->
358
359 Reference Guide: TODO
360
361
362 <!-- *********************************************************************** -->
363 </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>
364 <a name="extensionguide">Extension Guide
365 </b></font></td></tr></table><ul>
366 <!-- *********************************************************************** -->
367
368
369 Look at the examples classes provided.  This section is a TODO.
370
371
372
373 <!-- *********************************************************************** -->
374 </ul>
375 <!-- *********************************************************************** -->
376
377 <hr>
378 <font size=-1>
379 <address><a href="mailto:sabre@nondot.org">Chris Lattner</a></address>
380 <!-- Created: Tue Jan 23 15:19:28 CST 2001 -->
381 <!-- hhmts start -->
382 Last modified: Mon Jul 23 17:33:57 CDT 2001
383 <!-- hhmts end -->
384 </font>
385 </body></html>