doc/ 0000755 0000000 0000000 00000000000 13141651242 010320 5 ustar root root doc/designstyle.css 0000644 0000000 0000000 00000004000 13033702445 013360 0 ustar root root body {
background-color: #ffffff;
color: black;
margin-right: 1in;
margin-left: 1in;
}
h1, h2, h3, h4, h5, h6 {
color: #3366ff;
font-family: sans-serif;
}
@media print {
/* Darker version for printing */
h1, h2, h3, h4, h5, h6 {
color: #000080;
font-family: helvetica, sans-serif;
}
}
h1 {
text-align: center;
font-size: 18pt;
}
h2 {
margin-left: -0.5in;
}
h3 {
margin-left: -0.25in;
}
h4 {
margin-left: -0.125in;
}
hr {
margin-left: -1in;
}
/* Definition lists: definition term bold */
dt {
font-weight: bold;
}
address {
text-align: right;
}
/* Use the Commandline flags are flags that users specify on the
command line when they run an executable. In the command Typically, an application lists what flags the user is allowed to
pass in, and what arguments they take -- in this example,
Gflags, the commandline flags library used within Google,
differs from other libraries,
such as There's significant gain in flexibility, and ease of code reuse,
due to this technique. However, there is a danger that two files will
define the same flag, and then give an error when they're linked
together. The rest of this document describes how to use the commandlineflag
library. It's a C++ library, so examples are in C++. However, there
is a Python port with the same functionality, and this discussion
translates directly to Python. The gflags library can be downloaded from GitHub.
You can clone the project using the command: Build and installation instructions are provided in the
INSTALL file.
The installation of the gflags package includes configuration files for popular build systems
such as pkg-config,
CMake, and Bazel. Using gflags within a project which uses CMake for its build system is easy.
You can either require an external installation of the gflags package and find it using CMake's find_package
command, or include the gflags project as subtree or submodule within your project's source tree and add the directory
using CMake's add_subdirectory command.
To use an external gflags installation, add the following CMake code to your Find gflags installation. The To request a particular imported gflags library target to link against, use the Note that this will raise a fatal error when the installed gflags package does not contain the requested library.
It is therefore recommended to only specify the particular component to look for if a specific library must be used.
Otherwise, the gflags-config.cmake module will choose a suitable and available library for you. By default, the
multi-threaded gflags library with shared linkage is chosen if available. When the source tree of the gflags project is included as subtree or submodule in the "gflags" directory of your project,
replace the above find_package command by Finally, add your executable build target which uses gflags to parse the command arguments with dependency on the
imported gflags library target: To use gflags within a project which uses Bazel as build tool,
add the following lines to your You can then add For example, see the following Defining a flag is easy: just use the appropriate macro for the
type you want the flag to be, as defined at the bottom of
Note that there are no 'complex' types like lists: the "languages"
flag in our example is a list of strings, but is defined of type
"string", not "list_of_string" or similar. This is by design. We'd
rather use only simple types for the flags, and allow for complex,
arbitrary parsing routines to parse them, than to try to put the logic
inside the flags library proper. All DEFINE macros take the same three arguments: the name of the
flag, its default value, and a 'help' string that describes its use.
The 'help' string is displayed when the user runs the application with
the You can define a flag in any source-code file in your executable.
Only define a flag once! If you want to access a flag in more than
one source file, DEFINE it in one file, and DECLARE it in the others. Even better, DEFINE it
in
Defining flags in libraries rather than in main() is powerful, but
does have some costs. One is that a library might not have a good
default value for its flags, for example if the flag holds a
filename that might not exist in some environments. To mitigate such problems,
you can use flag validators to ensure prompt
notification (in the form of a crash) of an invalid flag value.
Note that while most functions in this library are defined in the
All defined flags are available to the program as just a normal
variable, with the prefix You can read and write to the flag just like any other
variable: You can also get and set flag values via special functions in
Accessing a flag in the manner of the previous section only works
if the flag was The This is functionally equivalent to saying Note that such an extern declaration introduces a dependency
between your file and the file that defines the You should go the do-not-DECLARE route when the flag is only needed
by If the flag does span multiple files, DECLARE it in the associated
After DEFINE-ing a flag, you may optionally register a validator
function with the flag. If you do this, after the flag is parsed from
the commandline, and whenever its value is changed via a call to
Here is an example use of this functionality: By doing the registration at global initialization time (right
after the DEFINE_int32), we ensure that the registration happens before
the commandline is parsed at the beginning of The above used The final piece is the one that tells the executable to process the
commandline flags, and set the Usually, this code is at the beginning of The last argument is called "remove_flags". If true, then
If, on the other hand, In either case, the The reason you make something a flag instead of a compile-time
constant, is so users can specify a non-default value on the
commandline. Here's how they might do it for an application that
links in This sets Note the atypical syntax for setting a boolean flag to false:
putting "no" in front of its name. There's a fair bit of flexibility
to how flags may be specified. Here's an example of all the ways to
specify the "languages" flag: For boolean flags, the possibilities are slightly different: (as well as the single-dash variant on all of these). Despite this flexibility, we recommend using only a single form:
It is a fatal error to specify a flag on the commandline that has
not been DEFINED somewhere in the executable. If you need that
functionality for some reason -- say you want to use the same set of
flags for several executables, but not all of them DEFINE every flag
in your list -- you can specify As in getopt(), If a flag is specified more than once, only the last specification
is used; the others are ignored. Note that flags do not have single-letter synonyms, like they do in
the getopt library, nor do we allow "combining" flags behind a
single dash, as in Sometimes a flag is defined in a library, and you want to change
its default value in one application but not others. It's simple to
do this: just assign a new value to the flag in For this application, users can still set the flag value on the
commandline, but if they do not, the flag's value will default to
true. There are a few flags defined by the commandlineflags module
itself, and are available to all applications that use
commandlineflags. These fall into
three categories. First are the 'reporting' flags that, when found, cause
the application to print some information about itself and exit. Second are the flags that affect how other flags are parsed. Third are the 'recursive' flags, that cause other flag values to be
set: This is equivalent to specifying Note it is a fatal error to say It is also a fatal error to say Note it is still an error to say In its simplest form, With this flagfile, the following two lines are equivalent:
Note that many errors are silently suppressed in flagfiles. In
particular, unrecognized flagnames are silently ignored, as are flags
that are missing a required value (e.g., a flagfile that just says
The general format of a flagfile is a bit more complicated than the
simple, common case above. It is: a sequence of filenames, one per
line, followed by a sequence of flags, one per line, repeated as many
times as desired. Filenames in a flagfile can use wildcards
( Lines that start with a It is possible for a flagfile to use the Flags are always processed in the expected order. That is,
processing begins by examining the flags specified directly on the
command line. If a flagfile is specified, its contents are processed,
and then processing continues with remaining flags from the command
line. In addition to accessing For more information about these routines, and other useful helper
methods such as tag for bits of code and for variables and objects. */
code,pre,samp,var {
color: #006000;
}
/* Use the
How To Use gflags (formerly Google Commandline Flags)
(as of
)
Introduction, and Comparison to Other Commandline
Flags Libraries
fgrep -l -f /var/tmp/foo johannes brahms
-l
and -f /var/tmp/foo
are the two
commandline flags. (johannes
and brahms
,
which don't start with a dash, are commandline arguments.)-l
takes no argument, and -f
takes a
string (in particular, a filename) as an argument. Users can use a
library to help parse the commandline and store the flags in some data
structure.getopt()
, in that flag definitions can be
scattered around the source code, and not just listed in one place
such as main()
. In practice, this means that a single
source-code file will define and use flags that are meaningful to that
file. Any application that links in that file will get the flags, and
the gflags library will automatically handle that
flag appropriately. Download and Installation
git clone https://github.com/gflags/gflags.git
Declare dependency on gflags with CMake
CMakeLists.txt
file.gflags_DIR
variable must be set to the <prefix>/lib/cmake/gflags directory
containing the gflags-config.cmake file if <prefix> is a non-standard location. Otherwise, CMake should find
the gflags installation automatically.
find_package(gflags REQUIRED)
COMPONENTS
option of
the find_package command. For example, to force the use of the single-threaded static library, use the command
find_package(gflags COMPONENTS nothreads_static)
add_subdirectory(gflags)
. See the top of the gflags/CMakeLists.txt
file for a listing of available CMake variables that can be set before this command to configure the build of the
gflags library. The default build settings are the build of a single-threaded static library which does not require
any installation of the gflags subproject products.
add_executable(foo main.cc)
target_link_libraries(foo gflags)
Declare dependency on gflags with Bazel
WORKSPACE
file
(see also Bazel documentation of git_repository):
git_repository(
name = "com_github_gflags_gflags",
commit = "<INSERT COMMIT SHA HERE>",
remote = "https://github.com/gflags/gflags.git",
)
bind(
name = "gflags",
actual = "@com_github_gflags_gflags//:gflags",
)
bind(
name = "gflags_nothreads",
actual = "@com_github_gflags_gflags//:gflags_nothreads",
)
//external:gflags
to the deps
section of a cc_binary
or cc_library
rule, and #include "gflags/gflags.h"
to include it in your source code.
This use the shared gflags library with multi-threading enabled. In order to use the single-threaded shared
gflags library, use the external dependency //external:gflags_nothreads
instead.BUILD
rule of the gflags/example project:
cc_binary(
name = "foo",
srcs = ["main.cc"],
deps = ["//external:gflags"],
)
DEFINE: Defining Flags In Program
gflags/gflags.h
. Here's an example file,
foo.cc
:
#include <gflags/gflags.h>
DEFINE_bool(big_menu, true, "Include 'advanced' options in the menu listing");
DEFINE_string(languages, "english,french,german",
"comma-separated list of languages to offer in the 'lang' menu");
DEFINE_bool
defines a boolean flag. Here are the
types supported:
DEFINE_bool
: boolean
DEFINE_int32
: 32-bit integer
DEFINE_int64
: 64-bit integer
DEFINE_uint64
: unsigned 64-bit integer
DEFINE_double
: double
DEFINE_string
: C++ string
--help
flag.foo.cc
and DECLARE it in foo.h
; then
everyone who #includes foo.h
can use the flag.google
namespace, DEFINE_foo
(and
DECLARE_foo
, below), should always
be in the global namespace. Accessing the Flag
FLAGS_
prepended. In the above
example, the macros define two variables, FLAGS_big_menu
(a bool), and FLAGS_languages
(a C++ string).
if (FLAGS_consider_made_up_languages)
FLAGS_languages += ",klingon"; // implied by --consider_made_up_languages
if (FLAGS_languages.find("finnish") != string::npos)
HandleFinnish();
gflags.h
. That's a rarer use case, though. DECLARE: Using the Flag in a Different File
DEFINE
-ed at the top of the file. If it
wasn't, you'll get an 'unknown variable' error.DECLARE_type
macro is available when you want to
use a flag that's defined in another file. For instance, if I were
writing bar.cc
but wanted to access the big_menu, flag, I
would put this near the top of bar.cc
:
DECLARE_bool(big_menu);
extern
FLAGS_big_menu
.big_menu
flag: foo.cc
, in this case. Such implicit dependencies
can be difficult to manage in large projects. For that reason we
recommend the following guideline:
If you DEFINE a flag in
foo.cc
, either don't DECLARE it
at all, only DECLARE it in tightly related tests, or only DECLARE
it in foo.h
.
foo.cc
, and not in any other file. If you want to
modify the value of the flag in the related test file to see if it is
functioning as expected, DECLARE it in the foo_test.cc
file.
.h
file, and make others #include
that
.h
file if they want to access the flag. The
#include
will make explicit the dependency between the
two files. This causes the flag to be a global variable. RegisterFlagValidator: Sanity-checking Flag Values
SetCommandLineOption()
, the validator function is called
with the new value as an argument. The validator function should
return 'true' if the flag value is valid, and false otherwise.
If the function returns false for the new setting of the
flag, the flag will retain its current value. If it returns false for the
default value, ParseCommandLineFlags will die.
static bool ValidatePort(const char* flagname, int32 value) {
if (value > 0 && value < 32768) // value is ok
return true;
printf("Invalid value for --%s: %d\n", flagname, (int)value);
return false;
}
DEFINE_int32(port, 0, "What port to listen on");
DEFINE_validator(port, &ValidatePort);
main()
.DEFINE_validator
macro calls the
RegisterFlagValidator()
function which returns true if the
registration is successful. It returns false if the registration fails
because a) the first argument does not refer to a commandline flag, or
b) a different validator has already been registered for this flag.
The return value is available as global static boolean variable named
<flag>_validator_registered
. Putting It Together: How to Set Up Flags
FLAGS_*
variables to the
appropriate, non-default value based on what is seen on the
commandline. This is equivalent to the getopt()
call in
the getopt library, but has much less overhead to use. In fact, it's
just a single function call:
gflags::ParseCommandLineFlags(&argc, &argv, true);
main()
.
argc
and argv
are exactly as passed in to
main()
. This routine might modify them, which is why
pointers to them are passed in.ParseCommandLineFlags
removes the flags and their
arguments from argv
, and modifies argc
appropriately. In this case, after the function call,
argv
will hold only commandline arguments, and not
commandline flags.remove_flags
is false, then
ParseCommandLineFlags
will leave argc unchanged, but will
rearrange the arguments in argv so that the flags are all at the
beginning. For example, if the input is "/bin/foo" "arg1" "-q"
"arg2"
(which is legal but weird), the function will rearrange
argv
so it reads "/bin/foo", "-q", "arg1",
"arg2"
. In this case, ParseCommandLineFlags
returns the index into argv that holds the first commandline argument:
that is, the index past the last flag. (In this example, it would
return 2, since argv[2]
points to arg1
.)FLAGS_*
variables are modified
based on what was passed in on the
commandline. Setting Flags on the Command Line
foo.cc
:
app_containing_foo --nobig_menu -languages="chinese,japanese,korean" ...
FLAGS_big_menu = false;
and
FLAGS_languages = "chinese,japanese,korean"
, when
ParseCommandLineFlags
is run.
app_containing_foo --languages="chinese,japanese,korean"
app_containing_foo -languages="chinese,japanese,korean"
app_containing_foo --languages "chinese,japanese,korean"
app_containing_foo -languages "chinese,japanese,korean"
app_containing_foo --big_menu
app_containing_foo --nobig_menu
app_containing_foo --big_menu=true
app_containing_foo --big_menu=false
--variable=value
for non-boolean flags, and
--variable/--novariable
for boolean flags. This
consistency will make your code more readable, and is also the format
required for certain special-use cases like flagfiles.--undefok
to suppress the error.--
by itself will terminate flags
processing. So in foo -f1 1 -- -f2 2
, f1
is
considered a flag, but -f2
is not.ls -la
. Changing the Default Flag Value
main()
,
before calling ParseCommandLineFlags()
:
DECLARE_bool(lib_verbose); // mylib has a lib_verbose flag, default is false
int main(int argc, char** argv) {
FLAGS_lib_verbose = true; // in my app, I want a verbose lib by default
ParseCommandLineFlags(...);
}
Special Flags
--help
shows all flags from all files, sorted by file and then by name;
shows the flagname, its default value, and its help string
--helpfull
same as -help, but unambiguously asks for all flags
(in case -help changes in the future)
--helpshort
shows only flags for the file with the same name as the executable
(usually the one containing
main()
)
--helpxml
like --help, but output is in xml for easier parsing
--helpon=FILE
shows only flags defined in FILE.*
--helpmatch=S
shows only flags defined in *S*.*
--helppackage
shows flags defined in files in same directory as
main()
--version
prints version info for the executable
--undefok=flagname,flagname,...
for those names listed as the argument to --undefok
,
suppress the normal error-exit that occurs when
--name
is seen on the commandline, but
name
has not been DEFINED anywhere in the
application
--fromenv
, --tryfromenv
,
--flagfile
. These are described below in more
detail.
--fromenv
--fromenv=foo,bar
says to read the values for the
foo
and bar
flags from the environment.
In concert with this flag, you must actually set the values in the
environment, via a line like one of the two below:
export FLAGS_foo=xxx; export FLAGS_bar=yyy # sh
setenv FLAGS_foo xxx; setenv FLAGS_bar yyy # tcsh
--foo=xxx
,
--bar=yyy
on the commandline.--fromenv=foo
if
foo
is not DEFINED somewhere in the application. (Though
you can suppress this error via --undefok=foo
, just like
for any other flag.)--fromenv=foo
if
FLAGS_foo
is not actually defined in the environment.
--tryfromenv
--tryfromenv
is exactly like --fromenv
,
except it is not a fatal error to say
--tryfromenv=foo
if FLAGS_foo
is not
actually defined in the environment. Instead, in such cases,
FLAGS_foo
just keeps its default value as specified in
the application.--tryfromenv=foo
if
foo
is not DEFINED somewhere in the application.
--flagfile
--flagfile=f
tells the commandlineflags module to read
the file f
, and to run all the flag-assignments found in
that file as if these flags had been specified on the commandline.f
should just be a list of flag
assignments, one per line. Unlike on the commandline, the equals sign
separating a flagname from its argument is required for
flagfiles. An example flagfile, /tmp/myflags
:
--nobig_menus
--languages=english,french
./myapp --foo --nobig_menus --languages=english,french --bar
./myapp --foo --flagfile=/tmp/myflags --bar
--languages
).*
and ?
), and the sequence of flags located
after a sequence of filenames is processed only if the current
executable's name matches one of the filenames. It is possible to
start the flagfile with a sequence of flags instead of a sequence of
filenames; if such a sequence of flags is present, these flags are
applied to the current executable no matter what it is.#
are ignored as comments.
Leading whitespace is also ignored in flagfiles, as are blank
lines.--flagfile
flag to include another flagfile. The API
FLAGS_foo
directly, it is
possible to access the flags programmatically, through an API. It is
also possible to access information about a flag, such as its default
value and help-string. A FlagSaver
makes it easy to
modify flags and then automatically undo the modifications later.
Finally, there are somewhat unrelated, but useful, routines to easily
access parts of argv
outside main, including the program
name (argv[0]
).gflags::SetUsageMessage()
and
gflags::SetVersionString
, see gflags.h
. Miscellaneous Notes
If your application has code like this:
#define STRIP_FLAG_HELP 1 // this must go before the #include! #include <gflags/gflags.h>
we will remove the help messages from the compiled source. This can reduce the size of the resulting binary somewhat, and may also be useful for security reasons.
Please report any issues or ideas for additional features on GitHub. We would also like to encourage pull requests for bug fixes and implementations of new features.