Commit b4bf72b0 authored by Craig Silverstein's avatar Craig Silverstein

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
parent 71e1be97
...@@ -517,8 +517,8 @@ access parts of <code>argv</code> outside main, including the program ...@@ -517,8 +517,8 @@ access parts of <code>argv</code> outside main, including the program
name (<code>argv[0]</code>).</p> name (<code>argv[0]</code>).</p>
<p>For more information about these routines, and other useful helper <p>For more information about these routines, and other useful helper
methods such as <code>google::SetUsageMessage</code>, see methods such as <code>google::SetUsageMessage()</code> and
<code>gflags.h</code>.</p> <code>google::SetVersionString</code>, see <code>gflags.h</code>.</p>
<h2> <A name="misc">Miscellaneous Notes</code> </h2> <h2> <A name="misc">Miscellaneous Notes</code> </h2>
......
...@@ -1467,9 +1467,12 @@ void GetAllFlags(vector<CommandLineFlagInfo>* OUTPUT) { ...@@ -1467,9 +1467,12 @@ void GetAllFlags(vector<CommandLineFlagInfo>* OUTPUT) {
// ProgramInvocationShortName() // ProgramInvocationShortName()
// SetUsageMessage() // SetUsageMessage()
// ProgramUsage() // ProgramUsage()
// SetVersionString()
// VersionString()
// Functions to set and get argv. Typically the setter is called // Functions to set and get argv. Typically the setter is called
// by ParseCommandLineFlags. Also can get the ProgramUsage string, // 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 // These values are not protected by a Mutex because they are normally
...@@ -1479,6 +1482,7 @@ static const char* cmdline = ""; // the entire command-line ...@@ -1479,6 +1482,7 @@ static const char* cmdline = ""; // the entire command-line
static vector<string> argvs; static vector<string> argvs;
static uint32 argv_sum = 0; static uint32 argv_sum = 0;
static const char* program_usage = NULL; static const char* program_usage = NULL;
static const char* version_string = NULL;
void SetArgv(int argc, const char** argv) { void SetArgv(int argc, const char** argv) {
static bool called_set_argv = false; static bool called_set_argv = false;
...@@ -1528,6 +1532,12 @@ void SetUsageMessage(const string& usage) { ...@@ -1528,6 +1532,12 @@ void SetUsageMessage(const string& usage) {
program_usage = strdup(usage.c_str()); // small memory leak 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() { const char* ProgramUsage() {
if (program_usage) { if (program_usage) {
return program_usage; return program_usage;
...@@ -1535,6 +1545,10 @@ const char* ProgramUsage() { ...@@ -1535,6 +1545,10 @@ const char* ProgramUsage() {
return "Warning: SetUsageMessage() never called"; return "Warning: SetUsageMessage() never called";
} }
const char* VersionString() {
return version_string ? version_string : "";
}
// -------------------------------------------------------------------- // --------------------------------------------------------------------
// GetCommandLineOption() // GetCommandLineOption()
// GetCommandLineFlagInfo() // GetCommandLineFlagInfo()
......
...@@ -210,10 +210,11 @@ extern const char* GetArgv0(); // only argv0 ...@@ -210,10 +210,11 @@ extern const char* GetArgv0(); // only argv0
extern uint32 GetArgvSum(); // simple checksum of argv extern uint32 GetArgvSum(); // simple checksum of argv
extern const char* ProgramInvocationName(); // argv0, or "UNKNOWN" if not set extern const char* ProgramInvocationName(); // argv0, or "UNKNOWN" if not set
extern const char* ProgramInvocationShortName(); // basename(argv0) extern const char* ProgramInvocationShortName(); // basename(argv0)
// ProgramUsage() is thread-safe as long as SetUsageMessage() is only // ProgramUsage() and VersionString() are thread-safe as long as
// called before any threads start. // SetUsageMessage() and SetVersionString() are only called before any
// threads start.
extern const char* ProgramUsage(); // string set by SetUsageMessage() 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)" // Normally you access commandline flags by just saying "if (FLAGS_foo)"
...@@ -344,6 +345,11 @@ extern const char *StringFromEnv(const char *varname, const char *defval); ...@@ -344,6 +345,11 @@ extern const char *StringFromEnv(const char *varname, const char *defval);
// Thread-hostile; meant to be called before any threads are spawned. // Thread-hostile; meant to be called before any threads are spawned.
extern void SetUsageMessage(const std::string& usage); 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 // Looks for flags in argv and parses them. Rearranges argv to put
// flags first, or removes them entirely if remove_flags is true. // 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 // If a flag is defined more than once in the command line or flag
......
...@@ -347,9 +347,13 @@ static void ShowXMLOfFlags(const char *prog_name) { ...@@ -347,9 +347,13 @@ static void ShowXMLOfFlags(const char *prog_name) {
// -------------------------------------------------------------------- // --------------------------------------------------------------------
static void ShowVersion() { static void ShowVersion() {
fprintf(stdout, "%s\n", ProgramInvocationShortName()); const char* version_string = VersionString();
// TODO: add other stuff, like a timestamp, who built it, what if (version_string && *version_string) {
// target they built, etc. fprintf(stdout, "%s version %s\n",
ProgramInvocationShortName(), version_string);
} else {
fprintf(stdout, "%s\n", ProgramInvocationShortName());
}
# if !defined(NDEBUG) # if !defined(NDEBUG)
fprintf(stdout, "Debug build (NDEBUG not #defined)\n"); fprintf(stdout, "Debug build (NDEBUG not #defined)\n");
......
...@@ -1562,6 +1562,7 @@ static int Main(int argc, char **argv) { ...@@ -1562,6 +1562,7 @@ static int Main(int argc, char **argv) {
FLAGS_changed_bool2 = true; FLAGS_changed_bool2 = true;
SetUsageMessage(usage_message.c_str()); SetUsageMessage(usage_message.c_str());
SetVersionString("test_version");
ParseCommandLineFlags(&argc, &argv, true); ParseCommandLineFlags(&argc, &argv, true);
#if defined(__MINGW32__) #if defined(__MINGW32__)
......
...@@ -182,6 +182,7 @@ Expect $LINENO 1 "/gflags_unittest.cc</file>" \ ...@@ -182,6 +182,7 @@ Expect $LINENO 1 "/gflags_unittest.cc</file>" \
# just print the version info and exit # just print the version info and exit
Expect $LINENO 0 "gflags_unittest" "gflags_unittest.cc" --version Expect $LINENO 0 "gflags_unittest" "gflags_unittest.cc" --version
Expect $LINENO 0 "version test_version" "gflags_unittest.cc" --version
# --undefok is a fun flag... # --undefok is a fun flag...
Expect $LINENO 1 "unknown command line flag 'foo'" "" --undefok= --foo --unused_bool Expect $LINENO 1 "unknown command line flag 'foo'" "" --undefok= --foo --unused_bool
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment