You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kudu.apache.org by to...@apache.org on 2016/08/23 23:24:38 UTC

[2/3] kudu git commit: tool: improve help output

tool: improve help output

- adds blank line in between usage summary and more detail
- better formatting for required parameters in usage
- better formatting for optional boolean parameters
- justifies the list of subcommands

Example output with subcommands:

Usage: ./build/latest/bin/kudu <command> [<args>]

<command> can be one of the following:
      fs   Operate on a local Kudu filesystem
     pbc   Operate on a PBC (protobuf container) files
  tablet   Operate on a local Kudu replica

Example output of a leaf node:

Usage: ./build/latest/bin/kudu pbc dump <path> [-oneline]

Dump a PBC (protobuf container) file
    path (path to PBC file) type: string default: ""
    -oneline (print each protobuf on a single line) type: bool default: false

Change-Id: I0b4fc2da2b03edf8ce6b9998eeabd06a4fcd216d
Reviewed-on: http://gerrit.cloudera.org:8080/4038
Reviewed-by: Adar Dembo <ad...@cloudera.com>
Tested-by: Todd Lipcon <to...@apache.org>


Project: http://git-wip-us.apache.org/repos/asf/kudu/repo
Commit: http://git-wip-us.apache.org/repos/asf/kudu/commit/8c996243
Tree: http://git-wip-us.apache.org/repos/asf/kudu/tree/8c996243
Diff: http://git-wip-us.apache.org/repos/asf/kudu/diff/8c996243

Branch: refs/heads/master
Commit: 8c9962438eacd0d49caf662c29c1e8d191ba7b9c
Parents: e216aff
Author: Todd Lipcon <to...@apache.org>
Authored: Wed Aug 17 23:32:21 2016 -0700
Committer: Todd Lipcon <to...@apache.org>
Committed: Tue Aug 23 22:52:31 2016 +0000

----------------------------------------------------------------------
 src/kudu/tools/tool_action.cc | 51 ++++++++++++++++++++++++++++----------
 1 file changed, 38 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/kudu/blob/8c996243/src/kudu/tools/tool_action.cc
----------------------------------------------------------------------
diff --git a/src/kudu/tools/tool_action.cc b/src/kudu/tools/tool_action.cc
index 447c392..616e6a6 100644
--- a/src/kudu/tools/tool_action.cc
+++ b/src/kudu/tools/tool_action.cc
@@ -17,9 +17,13 @@
 
 #include "kudu/tools/tool_action.h"
 
+#include <algorithm>
+#include <iomanip>
 #include <memory>
 #include <string>
+#include <strstream>
 #include <unordered_map>
+#include <utility>
 #include <vector>
 
 #include "kudu/gutil/strings/join.h"
@@ -87,15 +91,27 @@ unique_ptr<Mode> ModeBuilder::Build() {
 
 // Get help for this mode, passing in its parent mode chain.
 string Mode::BuildHelp(const vector<Mode*>& chain) const {
-  string msg = Substitute("$0 <action>\n", BuildUsageString(chain));
-  msg += "Action can be one of the following:\n";
+  std::ostringstream msg;
+  msg << Substitute("$0 <command> [<args>]\n\n", BuildUsageString(chain));
+  msg << "<command> can be one of the following:\n";
+
+  vector<pair<string, string>> line_pairs;
+  int max_command_len = 0;
   for (const auto& m : modes()) {
-    msg += Substitute("  $0 : $1\n", m->name(), m->description());
+    line_pairs.emplace_back(m->name(), m->description());
+    max_command_len = std::max<int>(max_command_len, m->name().size());
   }
   for (const auto& a : actions()) {
-    msg += Substitute("  $0 : $1\n", a->name(), a->description());
+    line_pairs.emplace_back(a->name(), a->description());
+    max_command_len = std::max<int>(max_command_len, a->name().size());
   }
-  return msg;
+
+  for (const auto& lp : line_pairs) {
+    msg << "  " << std::setw(max_command_len) << lp.first;
+    msg << "   " << lp.second << "\n";
+  }
+
+  return msg.str();
 }
 
 Mode::Mode() {
@@ -161,19 +177,28 @@ string Action::BuildHelp(const vector<Mode*>& chain) const {
   for (const auto& param : args_.optional) {
     google::CommandLineFlagInfo gflag_info =
         google::GetCommandLineFlagInfoOrDie(param.c_str());
-    string noun;
-    string::size_type last_underscore_idx = param.rfind('_');
-    if (last_underscore_idx != string::npos &&
-        last_underscore_idx != param.size() - 1) {
-      noun = param.substr(last_underscore_idx + 1);
+
+    if (gflag_info.type == "bool") {
+      if (gflag_info.default_value == "false") {
+        usage_msg += Substitute(" [-$0]", param);
+      } else {
+        usage_msg += Substitute(" [-no$0]", param);
+      }
     } else {
-      noun = param;
+      string noun;
+      string::size_type last_underscore_idx = param.rfind('_');
+      if (last_underscore_idx != string::npos &&
+          last_underscore_idx != param.size() - 1) {
+        noun = param.substr(last_underscore_idx + 1);
+      } else {
+        noun = param;
+      }
+      usage_msg += Substitute(" [-$0=<$1>]", param, noun);
     }
-    usage_msg += Substitute(" [-$0=<$1>]", param, noun);
     desc_msg += google::DescribeOneFlag(gflag_info);
   }
   string msg = usage_msg;
-  msg += "\n";
+  msg += "\n\n";
   msg += Substitute("$0\n", label_.description);
   msg += desc_msg;
   return msg;