CommandLine Library Manual |
Introduction |
Although there are a lot 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:
Quick Start Guide |
To start out, you need to include the CommandLine header file into your program:
#include "Support/CommandLine.h"
Additionally, you need to add this as the first line of your main program:
int main(int argc, char **argv) { cl::ParseCommandLineOptions(argc, argv); ... }
... which actually parses the arguments and fills in the variable declarations.
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 '-o <filename>' option to specify where to put the output. With the CommandLine library, this is represented like this:
cl::String OutputFilename("o", "Specify output filename");
or more verbosely, like this:
cl::String OutputFilename("o", "Specify output filename", cl::NoFlags, "");
This declares a variable "OutputFilename" that is used to capture the result of the "o" 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 "cl::String", 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:
... ofstream Output(OutputFilename.c_str()); if (Out.good()) ... ...
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).
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: "opt [flags] sourcefilename.c"). 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:
cl::String InputFilename("", "Source file to optimize", cl::NoFlags, "-");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 "cl::Required" flag:
cl::String InputFilename("", "Source file to optimize", cl::Required, "-");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.
Flag Arguments |
cl::Flag Force ("f", "Overwrite output files", cl::NoFlags, false); cl::Flag Quiet ("q", "Don't print informational messages", cl::Hidden); cl::Flag Quiet2("quiet", "Don't print informational messages", cl::NoFlags);
This does what you would expect: it declares three boolean variables ("Force", "Quiet", and "Quiet2") to recognize these options. Note that the "-q" option is specified with the "cl::Hidden" flag. This prevents it from being shown by the standard "--help" command line argument provided. With these declarations, "opt --help" emits this:
USAGE: opt [options] OPTIONS: -f - Overwrite output files -o - Override output filename -quiet - Don't print informational messages -help - display available options (--help-hidden for more)
and "opt --help-hidden" emits this:
USAGE: opt [options] OPTIONS: -f - Overwrite output files -o - Override output filename -q - Don't print informational messages -quiet - Don't print informational messages -help - display available options (--help-hidden for more)
This brief example has shown you how to use simple scalar command line arguments, by using the "cl::String" and "cl::Flag" classes. In addition to these classes, there are also "cl::Int" and "cl::Double" classes that work analagously.
Argument Aliases |
... if (!Quiet && !Quiet2) printInformationalMessage(...); ...
... which is a real pain! Instead of defining two values for the same condition, we can use the "cl::Alias" to make the "-q" option an alias for "-quiet" instead of a value itself:
cl::Flag Force ("f", "Overwrite output files", cl::NoFlags, false); cl::Flag Quiet ("quiet", "Don't print informational messages", cl::NoFlags, false); cl::Alias QuietA("q", "Alias for -quiet", cl::NoFlags, Quiet);
Which does exactly what we want... and the alias is automatically hidden from the "--help" output. Note how the alias specifies the variable that it wants to alias to, the alias argument name, and the help description (shown by "--help-hidden") of the alias. Now your user code can simply use:
... if (!Quiet) printInformationalMessage(...); ...
... which is much nicer! The "cl::Alias" can be used to specify an alternative name for any variable type, and has many uses.
Selecting one alternative from a set |
Lets say that we would like to add four optimizations levels to our optimizer, using the standard flags "-g", "-O0", "-O1", and "-O2". We could easily implement this with the "cl::Flag" class above, but there are several problems with this strategy:
To cope with these problems, the CommandLine library provides the "cl::EnumFlags class, which is used like this:
enum OptLevel { g, O1, O2, O3 }; cl::EnumFlags<enum OptLevel> OptimizationLevel(cl::NoFlags, clEnumVal(g , "No optimizations, enable debugging"), clEnumVal(O1, "Enable trivial optimizations"), clEnumVal(O2, "Enable default optimizations"), clEnumVal(O3, "Enable expensive optimizations"), 0); ... if (OptimizationLevel >= O2) doGCSE(...); ...
This declaration defines a variable "OptimizationLevel" of the "OptLevel" 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 "0" 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. In addition to all of this, the CommandLine library automatically names the flag values the same as the enum values.
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 "g" in our program. We could alternatively write this example like this:
enum OptLevel { Debug, O1, O2, O3 }; cl::EnumFlags<enum OptLevel> OptimizationLevel(cl::NoFlags, clEnumValN(Debug, "g", "No optimizations, enable debugging"), clEnumVal(O1 , "Enable trivial optimizations"), clEnumVal(O2 , "Enable default optimizations"), clEnumVal(O3 , "Enable expensive optimizations"), 0); ... if (OptimizationLevel == Debug) outputDebugInfo(...); ...
By using the "clEnumValN" token instead of "clEnumVal", we can directly specify the name that the flag should get.
Named Alternatives |
enum DebugLev { nodebuginfo, quick, detailed }; // Enable Debug Options to be specified on the command line cl::Enum<enum DebugLev> DebugLevel("debug_level", cl::NoFlags, "select debugging level", clEnumValN(nodebuginfo, "none", "disable debug information"), clEnumVal(quick, "enable quick debug information"), clEnumVal(detailed, "enable detailed debug information"), 0);This definition defines an enumerated command line variable of type "enum DebugLev", with the same semantics as the "EnumFlags" definition does. The difference here is just the interface exposed to the user of your program and the help output by the "--help" option:
... OPTIONS: -debug_level - select debugging level =none - disable debug information =quick - enable quick debug information =detailed - enable detailed debug information -g - No optimizations, enable debugging -O1 - Enable trivial optimizations -O2 - Enable default optimizations -O3 - Enable expensive optimizations ...
By providing both of these forms of command line argument, the CommandLine library lets the application developer choose the appropriate interface for the job.
Parsing a list of options |
enum Opts { // 'inline' is a C++ reserved word, so name it 'inlining' dce, constprop, inlining, strip }
Then define your "cl::EnumList" variable:
cl::EnumListOptimizationList(cl::NoFlags, clEnumVal(dce , "Dead Code Elimination"), clEnumVal(constprop , "Constant Propogation"), clEnumValN(inlining, "inline", "Procedure Integration"), clEnumVal(strip , "Strip Symbols"), 0);
This defines a variable that is conceptually of the type "vector<enum Opts>". Thus, you can do operations like this:
for (unsigned i = 0; i < OptimizationList.size(); ++i) switch (OptimizationList[i]) ...... to iterate through the list of options specified.
Reference Guide |
Extension Guide |