This is an automated email from the ASF dual-hosted git repository.

bcall pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficserver.git


The following commit(s) were added to refs/heads/master by this push:
     new 121948b  ArgParser: Standardize version message and other updates
121948b is described below

commit 121948b6320e2d668b9c0dbafdfce321c5062043
Author: Xavier Chi <chitianha...@gmail.com>
AuthorDate: Thu Nov 8 15:08:46 2018 -0600

    ArgParser: Standardize version message and other updates
---
 .../internal-libraries/ArgParser.en.rst            |  8 +++++-
 include/tscore/ArgParser.h                         |  1 +
 src/traffic_cache_tool/Makefile.inc                |  5 ++++
 src/traffic_layout/traffic_layout.cc               |  5 ++--
 src/tscore/ArgParser.cc                            | 33 ++++++++++++++++++++--
 5 files changed, 46 insertions(+), 6 deletions(-)

diff --git a/doc/developer-guide/internal-libraries/ArgParser.en.rst 
b/doc/developer-guide/internal-libraries/ArgParser.en.rst
index 6e16197..1448a90 100644
--- a/doc/developer-guide/internal-libraries/ArgParser.en.rst
+++ b/doc/developer-guide/internal-libraries/ArgParser.en.rst
@@ -144,11 +144,13 @@ from the :class:`Arguments` object returned from the 
parsing. The function can b
 
     args.invoke();
 
-Help message
+Help and Version messages
 -------------------------
 
 - Help message will be outputted when a wrong usage of the program is detected 
or `--help` option found.
 
+- Version message is defined unified in :code:`ArgParser::version_message()`.
+
 Classes
 +++++++
 
@@ -175,6 +177,10 @@ Classes
 
       Output usage to the console.
 
+   .. function:: void version_message() const
+
+      Output version string to the console.
+
    .. function:: void add_global_usage(std::string const &usage)
 
       Add a global_usage for :code:`help_message()`. Example: `traffic_blabla 
[--SWITCH [ARG]]`.
diff --git a/include/tscore/ArgParser.h b/include/tscore/ArgParser.h
index 927882d..95a5a2d 100644
--- a/include/tscore/ArgParser.h
+++ b/include/tscore/ArgParser.h
@@ -175,6 +175,7 @@ public:
     bool parse(Arguments &ret, AP_StrVec &args);
     // The help & version messages
     void help_message(std::string_view err = "") const;
+    void version_message() const;
     // Helpr method for parse()
     void append_option_data(Arguments &ret, AP_StrVec &args, int index);
     // The command name and help message
diff --git a/src/traffic_cache_tool/Makefile.inc 
b/src/traffic_cache_tool/Makefile.inc
index 8206380..6cb9430 100644
--- a/src/traffic_cache_tool/Makefile.inc
+++ b/src/traffic_cache_tool/Makefile.inc
@@ -48,4 +48,9 @@ traffic_cache_tool_traffic_cache_tool_LDADD = \
     $(top_builddir)/src/tscore/.libs/Regex.o \
     $(top_builddir)/src/tscore/.libs/CryptoHash.o \
     $(top_builddir)/src/tscore/.libs/MMH.o \
+    $(top_builddir)/src/tscore/.libs/Version.o \
+    $(top_builddir)/src/tscore/.libs/Regression.o \
+    $(top_builddir)/src/tscore/.libs/ink_args.o \
+    $(top_builddir)/src/tscore/.libs/ParseRules.o \
+    $(top_builddir)/src/tscore/.libs/SourceLocation.o \
     @OPENSSL_LIBS@ @LIBPCRE@ @LIBTCL@
diff --git a/src/traffic_layout/traffic_layout.cc 
b/src/traffic_layout/traffic_layout.cc
index a0095d1..a7bd66d 100644
--- a/src/traffic_layout/traffic_layout.cc
+++ b/src/traffic_layout/traffic_layout.cc
@@ -40,8 +40,9 @@ main(int argc, const char **argv)
   engine.parser.add_global_usage("traffic_layout CMD [OPTIONS]");
 
   // global options
-  engine.parser.add_option("--help", "-h", "Print usage information");
-  engine.parser.add_option("--run-root", "", "using TS_RUNROOT as sandbox", 
"", 1);
+  engine.parser.add_option("--help", "-h", "Print usage information")
+    .add_option("--run-root", "", "using TS_RUNROOT as sandbox", "", 1)
+    .add_option("--version", "-V", "Print version string");
 
   // info command
   engine.parser.add_command("info", "Show the layout as default", [&]() { 
engine.info(); })
diff --git a/src/tscore/ArgParser.cc b/src/tscore/ArgParser.cc
index 349eb65..6f3f477 100644
--- a/src/tscore/ArgParser.cc
+++ b/src/tscore/ArgParser.cc
@@ -22,10 +22,13 @@
  */
 
 #include "tscore/ArgParser.h"
+#include "tscore/ink_file.h"
+#include "tscore/I_Version.h"
 
 #include <iostream>
 #include <set>
 #include <sstream>
+#include <sysexits.h>
 
 std::string global_usage;
 std::string parser_program_name;
@@ -76,7 +79,7 @@ ArgParser::add_global_usage(std::string const &usage)
 void
 ArgParser::help_message(std::string_view err) const
 {
-  return _top_level_command.help_message(err);
+  _top_level_command.help_message(err);
 }
 
 // a graceful way to output help message
@@ -103,6 +106,17 @@ ArgParser::Command::help_message(std::string_view err) 
const
   if (!_example_usage.empty()) {
     std::cout << "\nExample Usage: " << _example_usage << std::endl;
   }
+  // standard return code
+  exit(EX_USAGE);
+}
+
+void
+ArgParser::Command::version_message() const
+{
+  // unified version message of ATS
+  AppVersionInfo appVersionInfo;
+  appVersionInfo.setup(PACKAGE_NAME, _name.c_str(), PACKAGE_VERSION, __DATE__, 
__TIME__, BUILD_MACHINE, BUILD_PERSON, "");
+  ink_fputln(stdout, appVersionInfo.FullVersionInfoStr);
   exit(0);
 }
 
@@ -158,7 +172,16 @@ ArgParser::parse(const char **argv)
     for (const auto &it : args) {
       msg = msg + " '" + it + "'";
     }
-    _top_level_command.help_message(msg);
+    // find the correct level to output help message
+    ArgParser::Command *command = &_top_level_command;
+    for (unsigned i = 1; i < _argv.size(); i++) {
+      auto it = command->_subcommand_list.find(_argv[i]);
+      if (it == command->_subcommand_list.end()) {
+        break;
+      }
+      command = &it->second;
+    }
+    command->help_message(msg);
   }
   return ret;
 }
@@ -398,8 +421,12 @@ ArgParser::Command::append_option_data(Arguments &ret, 
AP_StrVec &args, int inde
         i -= 1;
       }
     } else {
+      // output version message
+      if ((args[i] == "--version" || args[i] == "-V") && 
_option_list.find("--version") != _option_list.end()) {
+        version_message();
+      }
       // output help message
-      if (args[i] == "--help" || args[i] == "-h") {
+      if ((args[i] == "--help" || args[i] == "-h") && 
_option_list.find("--help") != _option_list.end()) {
         ArgParser::Command *command = this;
         // find the correct level to output help messsage
         for (unsigned i = 1; i < args.size(); i++) {

Reply via email to