1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2 <html><head><title>CommandLine Library Manual</title></head>
5 <table width="100%" bgcolor="#330077" border=0 cellpadding=4 cellspacing=0>
6 <tr><td> <font size=+5 color="#EEEEFF" face="Georgia,Palatino,Times,Roman"><b>CommandLine Library Manual</b></font></td>
10 <li><a href="#introduction">Introduction</a>
11 <li><a href="#quickstart">Quick Start Guide</a>
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>
20 <li><a href="#referenceguide">Reference Guide</a>
21 <li><a href="#extensionguide">Extension Guide</a>
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 <!-- *********************************************************************** -->
32 This document describes the CommandLine argument processing library. It will show you how to use it, and what it can do.<p>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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 <!-- *********************************************************************** -->
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>
65 To start out, you need to include the CommandLine header file into your program:<p>
68 #include "CommandLine.h"
71 Additionally, you need to add this as the first line of your main program:<p>
74 int main(int argc, char **argv) {
75 cl::ParseCommandLineOptions(argc, argv);
80 ... which actually parses the arguments and fills in the variable declarations.<p>
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 <filename></tt>' option to specify where to put the output. With the CommandLine library, this is represented like this:<p>
85 cl::String OutputFilename("<i>o</i>", "<i>Specify output filename</i>");
88 or more verbosely, like this:<p>
91 cl::String OutputFilename("<i>o</i>", "<i>Specify output filename</i>", cl::NoFlags, "");
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>
98 ofstream Output(OutputFilename.c_str());
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>
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>
108 cl::String InputFilename("", "<i>Source file to optimize</i>", cl::NoFlags, "<i>-</i>");
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>
114 cl::String InputFilename("", "<i>Source file to optimize</i>", <b>cl::Required</b>, "<i>-</i>");
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>
119 <!-- ======================================================================= -->
120 </ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0><tr><td> </td><td width="100%"> <font color="#EEEEFF" face="Georgia,Palatino"><b>
121 <a name="flags">Flag Arguments
122 </b></font></td></tr></table><ul>
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>
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);
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>
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)
144 and "<tt>opt --help-hidden</tt>" emits this:<p>
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)
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>
160 <!-- ======================================================================= -->
161 </ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0><tr><td> </td><td width="100%"> <font color="#EEEEFF" face="Georgia,Palatino"><b>
162 <a name="aliases">Argument Aliases
163 </b></font></td></tr></table><ul>
165 This works well, except for the fact that we need to check the quiet condition like this now:<p>
169 if (!Quiet && !Quiet2) printInformationalMessage(...);
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>
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);
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>
185 if (!Quiet) printInformationalMessage(...);
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>
192 <!-- ======================================================================= -->
193 </ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0><tr><td> </td><td width="100%"> <font color="#EEEEFF" face="Georgia,Palatino"><b>
194 <a name="onealternative">Selecting one alternative from a set
195 </b></font></td></tr></table><ul>
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>
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>
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 >= "<tt>-O1</tt>" is enabled.
207 To cope with these problems, the CommandLine library provides the "<tt>cl::EnumFlags</tt> class, which is used like this:<p>
214 cl::EnumFlags<enum OptLevel> 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>"),
222 if (OptimizationLevel >= O2) doGCSE(...);
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.
229 In addition to all of this, the CommandLine library automatically names the flag values the same as the enum values.<p>
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>
238 cl::EnumFlags<enum OptLevel> 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>"),
246 if (OptimizationLevel == Debug) outputDebugInfo(...);
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>
252 <!-- ======================================================================= -->
253 </ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0><tr><td> </td><td width="100%"> <font color="#EEEEFF" face="Georgia,Palatino"><b>
254 <a name="namedalternatives">Named Alternatives
255 </b></font></td></tr></table><ul>
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>
261 nodebuginfo, quick, detailed
264 // Enable Debug Options to be specified on the command line
265 cl::Enum<enum DebugLev> 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>"),
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>
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
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>
292 <!-- ======================================================================= -->
293 </ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0><tr><td> </td><td width="100%"> <font color="#EEEEFF" face="Georgia,Palatino"><b>
294 <a name="enumlist">Parsing a list of options
295 </b></font></td></tr></table><ul>
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>
301 // 'inline' is a C++ reserved word, so name it 'inlining'
302 dce, constprop, inlining, strip
306 Then define your "<tt>cl::EnumList</tt>" variable:<p>
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>"),
317 This defines a variable that is conceptually of the type "<tt>vector<enum Opts></tt>". Thus, you can do operations like this:<p>
320 for (unsigned i = 0; i < OptimizationList.size(); ++i)
321 switch (OptimizationList[i])
325 ... to iterate through the list of options specified.
331 <!-- ======================================================================= -->
332 </ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0><tr><td> </td><td width="100%"> <font color="#EEEEFF" face="Georgia,Palatino"><b>
333 <a name="stringlist">Parsing a list of non-options
334 </b></font></td></tr></table><ul>
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>
340 cl::StringList InputFilenames("", "Load <arg> files, linking them together",
345 This variable works just like a "<tt>vector<string></tt>" object. As such, iteration is simple:<p>
348 for (unsigned i = 0; i < InputFilenames.size(); ++i)
349 cout << "Found an argument: " << InputFilenames[i] << endl;
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 <!-- *********************************************************************** -->
359 Reference Guide: TODO
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 <!-- *********************************************************************** -->
369 Look at the examples classes provided. This section is a TODO.
373 <!-- *********************************************************************** -->
375 <!-- *********************************************************************** -->
379 <address><a href="mailto:sabre@nondot.org">Chris Lattner</a></address>
380 <!-- Created: Tue Jan 23 15:19:28 CST 2001 -->
382 Last modified: Mon Jul 23 17:33:57 CDT 2001