aurora-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mchucarr...@apache.org
Subject git commit: Implement help for v2 bridge
Date Fri, 21 Feb 2014 14:01:15 GMT
Repository: incubator-aurora
Updated Branches:
  refs/heads/master f806aa046 -> 445ecd778


Implement help for v2 bridge

Implement help message generation for the noun/verb framework.

Bugs closed: aurora-203

Reviewed at https://reviews.apache.org/r/18267/


Project: http://git-wip-us.apache.org/repos/asf/incubator-aurora/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-aurora/commit/445ecd77
Tree: http://git-wip-us.apache.org/repos/asf/incubator-aurora/tree/445ecd77
Diff: http://git-wip-us.apache.org/repos/asf/incubator-aurora/diff/445ecd77

Branch: refs/heads/master
Commit: 445ecd778e77b8b8e45892b7d888241aea6671fb
Parents: f806aa0
Author: Mark Chu-Carroll <mchucarroll@twopensource.com>
Authored: Fri Feb 21 08:57:49 2014 -0500
Committer: Mark Chu-Carroll <mchucarroll@twitter.com>
Committed: Fri Feb 21 08:57:49 2014 -0500

----------------------------------------------------------------------
 .../python/apache/aurora/client/cli/bridge.py   | 26 ++++++++++++++++++++
 .../python/apache/aurora/client/cli/client.py   |  9 +++++--
 .../python/apache/aurora/client/cli/options.py  | 24 ++++++++++++------
 3 files changed, 49 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/445ecd77/src/main/python/apache/aurora/client/cli/bridge.py
----------------------------------------------------------------------
diff --git a/src/main/python/apache/aurora/client/cli/bridge.py b/src/main/python/apache/aurora/client/cli/bridge.py
index 27079da..801facc 100644
--- a/src/main/python/apache/aurora/client/cli/bridge.py
+++ b/src/main/python/apache/aurora/client/cli/bridge.py
@@ -12,6 +12,10 @@ class CommandProcessor(object):
   "arg1" and "arg2" would be the parameters to that command.
   """
 
+  @property
+  def name(self):
+    """Get the name of this command processor"""
+
   def execute(self, args):
     """Execute the command-line tool wrapped by this processor.
 
@@ -40,9 +44,31 @@ class Bridge(object):
     self.command_processors = command_processors
     self.default = default
 
+  def show_help(self, args):
+    """Dispatch a help request to the appropriate sub-command"""
+    if len(args) == 2:  # command was just "help":
+      print("This is a merged command line, consisting of %s" %
+          [cp.name for cp in self.command_processors])
+      for cp in self.command_processors:
+        print("========== help for %s ==========" % cp.name)
+        cp.execute(args)
+      return
+    elif len(args) >= 3:
+      discriminator = args[2]
+      for cp in self.command_processors:
+        if discriminator in cp.get_commands():
+          cp.execute(args)
+          return
+      if self.default is not None:
+        self.default.execute(args)
+
+
   def execute(self, args):
     """Dispatch a command line to the appropriate CommandProcessor"""
     for cl in self.command_processors:
+      if args[1] == 'help':
+        self.show_help(args)
+        return 0
       if args[1] in cl.get_commands():
         return cl.execute(args)
     if self.default is not None:

http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/445ecd77/src/main/python/apache/aurora/client/cli/client.py
----------------------------------------------------------------------
diff --git a/src/main/python/apache/aurora/client/cli/client.py b/src/main/python/apache/aurora/client/cli/client.py
index 56d6366..86c2615 100644
--- a/src/main/python/apache/aurora/client/cli/client.py
+++ b/src/main/python/apache/aurora/client/cli/client.py
@@ -29,10 +29,10 @@ class AuroraClientV2CommandProcessor(CommandProcessor):
 
   @property
   def name(self):
-    return "aurora"
+    return "Aurora Client v2"
 
   def get_commands(self):
-    return self.commandline.registered_nouns + ["help"]
+    return self.commandline.registered_nouns
 
   def execute(self, args):
     return self.commandline.execute(args[1:])
@@ -40,6 +40,11 @@ class AuroraClientV2CommandProcessor(CommandProcessor):
 
 class AuroraClientV1CommandProcessor(CommandProcessor):
   # TODO(mchucarroll): deprecate client v1. (AURORA-131)
+
+  @property
+  def name(self):
+    return "Aurora Client v1"
+
   def get_commands(self):
     return ["cancel_update", "create", "diff", "get_quota", "inspect", "kill", "list_jobs",
         "open", "restart", "run", "ssh", "start_cron", "status", "update", "version" ]

http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/445ecd77/src/main/python/apache/aurora/client/cli/options.py
----------------------------------------------------------------------
diff --git a/src/main/python/apache/aurora/client/cli/options.py b/src/main/python/apache/aurora/client/cli/options.py
index 2f37372..0e0bd45 100644
--- a/src/main/python/apache/aurora/client/cli/options.py
+++ b/src/main/python/apache/aurora/client/cli/options.py
@@ -15,10 +15,10 @@
 #
 
 from collections import namedtuple
-from types import IntType, StringType
 
 from apache.aurora.common.aurora_job_key import AuroraJobKey
 
+from twitter.common.lang import Compatibility
 from twitter.common.quantity.parse_simple import parse_time
 from argparse import ArgumentTypeError
 
@@ -27,24 +27,32 @@ class CommandOption(object):
   """A lightweight encapsulation of an argparse option specification"""
 
   def __init__(self, *args, **kwargs):
-    self.name = args[0]
+    # Search for the long-name: it's either the first argument that
+    # is a double-dashed option name, or the first argument that doesn't
+    # have a dash in front of it.
+    for arg in args:
+      if arg.startswith('--') or not arg.startswith('-'):
+        self.name = arg
+        break
+    else:
+      raise ValueError('CommandOption had no valid name.')
     self.args = args
     self.kwargs = kwargs
-    self.type = kwargs['type'] if 'type' in kwargs else None
-    self.help = kwargs['help'] if 'help' in kwargs else ""
+    self.type = kwargs.get('type')
+    self.help = kwargs.get('help', '')
 
   def is_mandatory(self):
-    return self.kwargs["required"] if "required" in self.kwargs else not self.name.startswith('--')
+    return self.kwargs.get('required', not self.name.startswith('--'))
 
   def get_displayname(self):
     """Get a display name for a the expected format of a parameter value"""
     if 'metavar' in self.kwargs:
       displayname = self.kwargs['metavar']
-    elif self.type == str:
+    elif self.type is str:
       displayname = "str"
-    elif type(self.type) is StringType:
+    elif isinstance(self.type, Compatibility.string):
       displayname = self.type
-    elif type(self.type) is IntType:
+    elif isinstance(self.type, Compatibility.integer):
       displayname = "int",
     else:
       displayname = "value"


Mime
View raw message