kudu-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From t...@apache.org
Subject kudu git commit: tool: print usage information when arguments fail to parse
Date Tue, 01 Aug 2017 23:04:20 GMT
Repository: kudu
Updated Branches:
  refs/heads/master 9f7fed68c -> 7e0a56b2a


tool: print usage information when arguments fail to parse

This changes the output when the user forgets to pass a required
argument such that the usage string is now returned.

Example:

  todd@va1022:~/kudu$ build/latest/bin/kudu fs dump block
  Invalid argument: must provide positional argument block_id

  Usage: build/latest/bin/kudu fs dump block <block_id>
  [-fs_wal_dir=<dir>] [-fs_data_dirs=<dirs>]

Change-Id: I4695aca5cb3f848f525784476403a9f577fdba13
Reviewed-on: http://gerrit.cloudera.org:8080/7301
Reviewed-by: Adar Dembo <adar@cloudera.com>
Tested-by: Kudu Jenkins


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

Branch: refs/heads/master
Commit: 7e0a56b2a7ac80d345da660fdc619b7fcff84512
Parents: 9f7fed6
Author: Todd Lipcon <todd@cloudera.com>
Authored: Mon Jun 26 16:23:00 2017 -0700
Committer: Todd Lipcon <todd@apache.org>
Committed: Tue Aug 1 23:03:48 2017 +0000

----------------------------------------------------------------------
 src/kudu/tools/kudu-tool-test.cc | 24 ++++++++++++++++--------
 src/kudu/tools/tool_action.cc    |  6 +++++-
 src/kudu/tools/tool_action.h     | 10 +++++++++-
 src/kudu/tools/tool_main.cc      | 13 ++++++++-----
 4 files changed, 38 insertions(+), 15 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/kudu/blob/7e0a56b2/src/kudu/tools/kudu-tool-test.cc
----------------------------------------------------------------------
diff --git a/src/kudu/tools/kudu-tool-test.cc b/src/kudu/tools/kudu-tool-test.cc
index 1375ddd..ba1e085 100644
--- a/src/kudu/tools/kudu-tool-test.cc
+++ b/src/kudu/tools/kudu-tool-test.cc
@@ -159,10 +159,16 @@ class ToolTest : public KuduTest {
       StripTrailingNewline(stderr);
     }
     if (stdout_lines) {
-      *stdout_lines = strings::Split(out, "\n", strings::SkipEmpty());
+      *stdout_lines = strings::Split(out, "\n");
+      while (!stdout_lines->empty() && stdout_lines->back() == "") {
+        stdout_lines->pop_back();
+      }
     }
     if (stderr_lines) {
-      *stderr_lines = strings::Split(err, "\n", strings::SkipEmpty());
+      *stderr_lines = strings::Split(err, "\n");
+      while (!stderr_lines->empty() && stderr_lines->back() == "") {
+        stderr_lines->pop_back();
+      }
     }
     return s;
   }
@@ -230,11 +236,13 @@ class ToolTest : public KuduTest {
     const string kPositionalArgumentMessage = "must provide positional argument";
     const string kVariadicArgumentMessage = "must provide variadic positional argument";
     const string& message = variadic ? kVariadicArgumentMessage : kPositionalArgumentMessage;
-    string err;
-    RunTool(arg_str, nullptr, &err, nullptr, nullptr);
-
     Status expected_status = Status::InvalidArgument(Substitute("$0 $1", message, required_arg));
-    ASSERT_EQ(expected_status.ToString(), err);
+
+    vector<string> err_lines;
+    RunTool(arg_str, nullptr, nullptr, nullptr, /* stderr_lines = */ &err_lines);
+    ASSERT_GE(err_lines.size(), 3) << err_lines;
+    ASSERT_EQ(expected_status.ToString(), err_lines[0]);
+    ASSERT_STR_MATCHES(err_lines[2], "Usage: kudu.*");
   }
 
   void RunFsCheck(const string& arg_str,
@@ -800,7 +808,7 @@ TEST_F(ToolTest, TestFsDumpCFile) {
     SCOPED_TRACE(stdout);
     ASSERT_GE(stdout.size(), 4);
     ASSERT_EQ(stdout[0], "Header:");
-    ASSERT_EQ(stdout[1], "Footer:");
+    ASSERT_EQ(stdout[2], "Footer:");
   }
   {
     NO_FATALS(RunActionStdoutLines(Substitute(
@@ -816,7 +824,7 @@ TEST_F(ToolTest, TestFsDumpCFile) {
     SCOPED_TRACE(stdout);
     ASSERT_GT(stdout.size(), kNumEntries);
     ASSERT_EQ(stdout[0], "Header:");
-    ASSERT_EQ(stdout[1], "Footer:");
+    ASSERT_EQ(stdout[2], "Footer:");
   }
 }
 

http://git-wip-us.apache.org/repos/asf/kudu/blob/7e0a56b2/src/kudu/tools/tool_action.cc
----------------------------------------------------------------------
diff --git a/src/kudu/tools/tool_action.cc b/src/kudu/tools/tool_action.cc
index 8e7ced2..9b7a580 100644
--- a/src/kudu/tools/tool_action.cc
+++ b/src/kudu/tools/tool_action.cc
@@ -252,7 +252,8 @@ Status Action::Run(const vector<Mode*>& chain,
   return runner_({ chain, this, required_args, variadic_args });
 }
 
-string Action::BuildHelp(const vector<Mode*>& chain) const {
+string Action::BuildHelp(const vector<Mode*>& chain,
+                         Action::HelpMode mode) const {
   SetOptionalParameterDefaultValues();
   string usage_msg = Substitute("Usage: $0 $1", BuildUsageString(chain), name());
   string desc_msg;
@@ -295,6 +296,9 @@ string Action::BuildHelp(const vector<Mode*>& chain) const {
     desc_msg += google::DescribeOneFlag(gflag_info);
     desc_msg += "\n";
   }
+  if (mode == USAGE_ONLY) {
+    return usage_msg;
+  }
   string msg;
   AppendHardWrapped(usage_msg, 8, &msg);
   msg += "\n\n";

http://git-wip-us.apache.org/repos/asf/kudu/blob/7e0a56b2/src/kudu/tools/tool_action.h
----------------------------------------------------------------------
diff --git a/src/kudu/tools/tool_action.h b/src/kudu/tools/tool_action.h
index 16aeb76..f8c8209 100644
--- a/src/kudu/tools/tool_action.h
+++ b/src/kudu/tools/tool_action.h
@@ -254,9 +254,17 @@ class ActionBuilder {
 // A leaf node in the tree, representing a logical operation taken by the tool.
 class Action {
  public:
+  enum HelpMode {
+    // Return the full help text, including descriptions for each
+    // of the arguments.
+    FULL_HELP,
+    // Return only a single-line usage statement.
+    USAGE_ONLY
+  };
 
   // Returns the help for this action given its parent mode chain.
-  std::string BuildHelp(const std::vector<Mode*>& chain) const;
+  std::string BuildHelp(const std::vector<Mode*>& chain,
+                        HelpMode mode = FULL_HELP) const;
 
   // Returns the help xml for this action
   std::string BuildHelpXML(const std::vector<Mode*>& chain) const;

http://git-wip-us.apache.org/repos/asf/kudu/blob/7e0a56b2/src/kudu/tools/tool_main.cc
----------------------------------------------------------------------
diff --git a/src/kudu/tools/tool_main.cc b/src/kudu/tools/tool_main.cc
index 4cc10c7..0227cd2 100644
--- a/src/kudu/tools/tool_main.cc
+++ b/src/kudu/tools/tool_main.cc
@@ -117,15 +117,18 @@ int DispatchCommand(const vector<Mode*>& chain,
   vector<string> variadic_args;
   Status s = MarshalArgs(chain, action, remaining_args,
                          &required_args, &variadic_args);
-  if (s.ok()) {
-    s = action->Run(chain, required_args, variadic_args);
+  if (!s.ok()) {
+    cerr << s.ToString() << endl;
+    cerr << endl;
+    cerr << action->BuildHelp(chain, Action::USAGE_ONLY) << endl;
+    return 1;
   }
+  s = action->Run(chain, required_args, variadic_args);
   if (s.ok()) {
     return 0;
-  } else {
-    cerr << s.ToString() << endl;
-    return 1;
   }
+  cerr << s.ToString() << endl;
+  return 1;
 }
 
 // Replace hyphens with underscores in a string and return a copy.


Mime
View raw message