Add support for specifying a version number (SetVersionString()),
which is then displayed in --version.


git-svn-id: https://gflags.googlecode.com/svn/trunk@47 6586e3c6-dcc4-952a-343f-ff74eb82781d
diff --git a/doc/gflags.html b/doc/gflags.html
index 4e70917..75027e3 100644
--- a/doc/gflags.html
+++ b/doc/gflags.html
@@ -517,8 +517,8 @@
 name (<code>argv[0]</code>).</p>
 
 <p>For more information about these routines, and other useful helper
-methods such as <code>google::SetUsageMessage</code>, see
-<code>gflags.h</code>.</p>
+methods such as <code>google::SetUsageMessage()</code> and
+<code>google::SetVersionString</code>, see <code>gflags.h</code>.</p>
 
 
 <h2> <A name="misc">Miscellaneous Notes</code> </h2>
diff --git a/src/gflags.cc b/src/gflags.cc
index 298d514..460d427 100644
--- a/src/gflags.cc
+++ b/src/gflags.cc
@@ -1467,9 +1467,12 @@
 // ProgramInvocationShortName()
 // SetUsageMessage()
 // ProgramUsage()
+// SetVersionString()
+// VersionString()
 //    Functions to set and get argv.  Typically the setter is called
 //    by ParseCommandLineFlags.  Also can get the ProgramUsage string,
-//    set by SetUsageMessage.
+//    set by SetUsageMessage, and the version string, set by
+//    SetVersionString.
 // --------------------------------------------------------------------
 
 // These values are not protected by a Mutex because they are normally
@@ -1479,6 +1482,7 @@
 static vector<string> argvs;
 static uint32 argv_sum = 0;
 static const char* program_usage = NULL;
+static const char* version_string = NULL;
 
 void SetArgv(int argc, const char** argv) {
   static bool called_set_argv = false;
@@ -1528,6 +1532,12 @@
   program_usage = strdup(usage.c_str());      // small memory leak
 }
 
+void SetVersionString(const string& version) {
+  if (version_string != NULL)
+    ReportError(DIE, "ERROR: SetVersionString() called twice\n");
+  version_string = strdup(version.c_str());   // small memory leak
+}
+
 const char* ProgramUsage() {
   if (program_usage) {
     return program_usage;
@@ -1535,6 +1545,10 @@
   return "Warning: SetUsageMessage() never called";
 }
 
+const char* VersionString() {
+  return version_string ? version_string : "";
+}
+
 // --------------------------------------------------------------------
 // GetCommandLineOption()
 // GetCommandLineFlagInfo()
diff --git a/src/gflags/gflags.h.in b/src/gflags/gflags.h.in
index bade86d..1b82b29 100644
--- a/src/gflags/gflags.h.in
+++ b/src/gflags/gflags.h.in
@@ -210,10 +210,11 @@
 extern uint32 GetArgvSum();                  // simple checksum of argv
 extern const char* ProgramInvocationName();  // argv0, or "UNKNOWN" if not set
 extern const char* ProgramInvocationShortName();   // basename(argv0)
-// ProgramUsage() is thread-safe as long as SetUsageMessage() is only
-// called before any threads start.
+// ProgramUsage() and VersionString() are thread-safe as long as
+// SetUsageMessage() and SetVersionString() are only called before any
+// threads start.
 extern const char* ProgramUsage();           // string set by SetUsageMessage()
-
+extern const char* VersionString();          // string set by SetVersionString()
 
 // --------------------------------------------------------------------
 // Normally you access commandline flags by just saying "if (FLAGS_foo)"
@@ -344,6 +345,11 @@
 // Thread-hostile; meant to be called before any threads are spawned.
 extern void SetUsageMessage(const std::string& usage);
 
+// Sets the version string, which is emitted with --version.
+// For instance: SetVersionString("1.3");
+// Thread-hostile; meant to be called before any threads are spawned.
+extern void SetVersionString(const std::string& version);
+
 // Looks for flags in argv and parses them.  Rearranges argv to put
 // flags first, or removes them entirely if remove_flags is true.
 // If a flag is defined more than once in the command line or flag
diff --git a/src/gflags_reporting.cc b/src/gflags_reporting.cc
index 4ce2557..289bb6e 100644
--- a/src/gflags_reporting.cc
+++ b/src/gflags_reporting.cc
@@ -347,9 +347,13 @@
 // --------------------------------------------------------------------
 
 static void ShowVersion() {
-  fprintf(stdout, "%s\n", ProgramInvocationShortName());
-  // TODO: add other stuff, like a timestamp, who built it, what
-  //       target they built, etc.
+  const char* version_string = VersionString();
+  if (version_string && *version_string) {
+    fprintf(stdout, "%s version %s\n",
+            ProgramInvocationShortName(), version_string);
+  } else {
+    fprintf(stdout, "%s\n", ProgramInvocationShortName());
+  }
 
 # if !defined(NDEBUG)
   fprintf(stdout, "Debug build (NDEBUG not #defined)\n");
diff --git a/src/gflags_unittest.cc b/src/gflags_unittest.cc
index d10f2c5..3f82b45 100644
--- a/src/gflags_unittest.cc
+++ b/src/gflags_unittest.cc
@@ -1562,6 +1562,7 @@
   FLAGS_changed_bool2 = true;
 
   SetUsageMessage(usage_message.c_str());
+  SetVersionString("test_version");
   ParseCommandLineFlags(&argc, &argv, true);
 
 #if defined(__MINGW32__)
diff --git a/src/gflags_unittest.sh b/src/gflags_unittest.sh
index a9810a3..20c84a0 100755
--- a/src/gflags_unittest.sh
+++ b/src/gflags_unittest.sh
@@ -182,6 +182,7 @@
 
 # just print the version info and exit
 Expect $LINENO 0 "gflags_unittest" "gflags_unittest.cc" --version
+Expect $LINENO 0 "version test_version" "gflags_unittest.cc" --version
 
 # --undefok is a fun flag...
 Expect $LINENO 1 "unknown command line flag 'foo'" "" --undefok= --foo --unused_bool