<h1>How To Use gflags (formerly Google Commandline Flags)</h1>
<small>(as of
<script type=text/javascript>
varlm=newDate(document.lastModified);
document.write(lm.toDateString());
</script>)
</small>
<br>
<blockquote><dl>
<dt> Table of contents </dt>
<dd><ahref="#intro">Introduction</a></dd>
<dd><ahref="#cmake">Finding and Linking to gflags using CMake</a></dd>
<dd><ahref="#define">DEFINE: Defining Flags In Program</A></dd>
<dd><ahref="#using">Accessing the Flag</A></dd>
<dd><ahref="#declare">DECLARE: Using the Flag in a Different File</a></dd>
<dd><ahref="#validate">RegisterFlagValidator: Sanity-checking Flag Values</a></dd>
<dd><ahref="#together">Putting It Together: How to Set Up Flags</a></dd>
<dd><ahref="#commandline">Setting Flags on the Command Line</a></dd>
<dd><ahref="#varz">Setting Flags at Runtime</a></dd>
<dd><ahref="#default">Changing the Default Flag Value</a></dd>
<dd><ahref="#special">Special Flags</a></dd>
<dd><ahref="#api">The API</a></dd>
<dd><br/></dd>
</dl></blockquote>
<h2><ANAME=intro>Introduction, and Comparison to Other Commandline
Flags Libraries</A></h2>
<p><b>Commandline flags</b> are flags that users specify on the
command line when they run an executable. In the command</p>
<pre>
fgrep -l -f /var/tmp/foo johannes brahms
</pre>
<p><code>-l</code> and <code>-f /var/tmp/foo</code> are the two
commandline flags. (<code>johannes</code> and <code>brahms</code>,
which don't start with a dash, are <b>commandline arguments</b>.)</p>
<p>Typically, an application lists what flags the user is allowed to
pass in, and what arguments they take -- in this example,
<code>-l</code> takes no argument, and <code>-f</code> 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.</p>
<p>Gflags, the commandline flags library used within Google,
differs from other libraries,
such as <code>getopt()</code>, in that flag definitions can be
scattered around the source code, and not just listed in one place
such as <code>main()</code>. 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.</p>
<p>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.</p>
<p>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.</p>
<h2><Aname=cmake>Finding and Linking to gflags </A> using CMake</h2>
<p> Using gflags within a project which uses <Ahref="http://www.cmake.org">CMake</A> for its build system is easy. Therefore, simply add the following CMake code to your <code>CMakeLists.txt</code> file.
<pre>
find_package (gflags REQUIRED)
add_executable (foo main.cc)
target_link_libraries (foo gflags)
</pre>
<h2><Aname=define>DEFINE: Defining Flags In Program</A></h2>
<p> 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
<code>gflags/gflags.h</code>. Here's an example file,
<code>foo.cc</code>:</p>
<pre>
#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");
</pre>
<p><code>DEFINE_bool</code> defines a boolean flag. Here are the