tool: Allow hyphens to be used instead of underscores

This works for both modes and actions.

Change-Id: Ie8bf958ee8841e9624687fb5ae50cc908e3e41c3
Reviewed-on: http://gerrit.cloudera.org:8080/5311
Tested-by: Kudu Jenkins
Reviewed-by: Adar Dembo <a...@cloudera.com>


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

Branch: refs/heads/master
Commit: 06569f218675893948e61b427d7ffe9155c7fffa
Parents: 648d69e
Author: Mike Percy <mpe...@apache.org>
Authored: Thu Dec 1 19:45:14 2016 +0000
Committer: Mike Percy <mpe...@apache.org>
Committed: Thu Dec 1 23:51:13 2016 +0000

----------------------------------------------------------------------
 src/kudu/tools/kudu-tool-test.cc | 13 +++++++++++++
 src/kudu/tools/tool_main.cc      | 15 +++++++++++++--
 2 files changed, 26 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/kudu/blob/06569f21/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 5aa34ee..7d2e62a 100644
--- a/src/kudu/tools/kudu-tool-test.cc
+++ b/src/kudu/tools/kudu-tool-test.cc
@@ -185,6 +185,7 @@ class ToolTest : public KuduTest {
     ASSERT_OK(s);
   }
 
+  // Run tool with specified arguments, expecting help output.
   void RunTestHelp(const string& arg_str,
                    const vector<string>& regexes,
                    const Status& expected_status = Status::OK()) const {
@@ -318,6 +319,18 @@ TEST_F(ToolTest, TestModeHelp) {
         "rewrite_raft_config.*Rewrite a tablet replica"
     };
     NO_FATALS(RunTestHelp("local_replica cmeta", kLocalReplicaCMetaRegexes));
+    // Try with a hyphen instead of an underscore.
+    NO_FATALS(RunTestHelp("local-replica cmeta", kLocalReplicaCMetaRegexes));
+  }
+  {
+    const vector<string> kLocalReplicaCopyFromRemoteRegexes = {
+        "Copy a tablet replica from a remote server"
+    };
+    NO_FATALS(RunTestHelp("local_replica copy_from_remote --help",
+                          kLocalReplicaCopyFromRemoteRegexes));
+    // Try with hyphens instead of underscores.
+    NO_FATALS(RunTestHelp("local-replica copy-from-remote --help",
+                          kLocalReplicaCopyFromRemoteRegexes));
   }
   {
     const vector<string> kClusterModeRegexes = {

http://git-wip-us.apache.org/repos/asf/kudu/blob/06569f21/src/kudu/tools/tool_main.cc
----------------------------------------------------------------------
diff --git a/src/kudu/tools/tool_main.cc b/src/kudu/tools/tool_main.cc
index 8ea1dac..59d7f11 100644
--- a/src/kudu/tools/tool_main.cc
+++ b/src/kudu/tools/tool_main.cc
@@ -15,6 +15,7 @@
 // specific language governing permissions and limitations
 // under the License.
 
+#include <algorithm>
 #include <deque>
 #include <iostream>
 #include <memory>
@@ -108,6 +109,12 @@ int DispatchCommand(const vector<Mode*>& chain,
   }
 }
 
+// Replace hyphens with underscores in a string and return a copy.
+string HyphensToUnderscores(string str) {
+  std::replace(str.begin(), str.end(), '-', '_');
+  return str;
+}
+
 int RunTool(int argc, char** argv, bool show_help) {
   unique_ptr<Mode> root = ModeBuilder(argv[0])
     .Description("doesn't matter") // root mode description isn't printed
@@ -135,7 +142,9 @@ int RunTool(int argc, char** argv, bool show_help) {
 
     // Match argument with a mode.
     for (const auto& m : cur->modes()) {
-      if (m->name() == argv[i]) {
+      if (m->name() == argv[i] ||
+          // Allow hyphens in addition to underscores in mode names.
+          m->name() == HyphensToUnderscores(argv[i])) {
         next_mode = m.get();
         break;
       }
@@ -143,7 +152,9 @@ int RunTool(int argc, char** argv, bool show_help) {
 
     // Match argument with an action.
     for (const auto& a : cur->actions()) {
-      if (a->name() == argv[i]) {
+      if (a->name() == argv[i] ||
+          // Allow hyphens in addition to underscores in action names.
+          a->name() == HyphensToUnderscores(argv[i])) {
         next_action = a.get();
         break;
       }

Reply via email to