samba-tool: Redefined samba-tool as a SuperCommand
authorGiampaolo Lauria <lauria2@yahoo.com>
Fri, 29 Jul 2011 01:44:06 +0000 (21:44 -0400)
committerAndrew Bartlett <abartlet@samba.org>
Fri, 9 Sep 2011 05:24:01 +0000 (15:24 +1000)
Removed MainCommand class as samba-tool is a SuperCommand
Redefined samba-tool as a SuperCommand
Fixed error handling in SuperCommand _run

Signed-off-by: Amitay Isaacs <amitay@gmail.com>
Signed-off-by: Andrew Bartlett <abartlet@samba.org>
source4/scripting/bin/samba-tool
source4/scripting/python/samba/netcmd/__init__.py

index 7d662308a2efe423f39c051c7840869da36eed44..ba792fb886a659375e6231d1da5a926ca419dd65 100755 (executable)
@@ -23,41 +23,15 @@ import sys
 sys.path.insert(0, "bin/python")
 
 from samba import netcmd
-from samba.netcmd import Command, CommandError
+from samba.netcmd import SuperCommand
 
 
-class MainCommand(Command):
-    """Main class for samba tool commands"""
 
-    commands = {}
+class cmd_sambatool(SuperCommand):
+    """samba-tool SuperCommand"""
+    
+    subcommands = netcmd.commands
 
-    def _run(self, myname, command=None, *args):
-        if command in self.commands:
-            return self.commands[command]._run(command, *args)
-        print "Usage: samba-tool <command> [options]"
-        print "Available commands:"
-        cmds = self.commands.keys()
-        cmds.sort()
-        for cmd in cmds:
-            print "    %-16s - %s" % (cmd, self.commands[cmd].description)
-        if command in [None]:
-            raise CommandError("You must specify a command")
-        if command in ['help', '-h', '--help']:
-            print "For more help on a specific command, please type: samba-tool <command> (-h|--help)"
-            return 0
-        raise CommandError("No such command '%s'" % command)
-
-    def usage(self, myname, command=None, *args):
-        if command is None or not command in self.commands:
-            print "Usage: samba-tool %s (%s) [options]" % (myname,
-                    " | ".join(self.commands.keys()))
-        else:
-            return self.commands[command].usage(*args)
-
-
-class cmd_sambatool(MainCommand):
-    """Samba Tool Commands"""
-    commands = netcmd.commands
 
 
 if __name__ == '__main__':
index 12e6a99b4d3cedb6a7fcc602ac27e81919580d5b..360134644bd7cb4b2c65abf50b04a92a45544788 100644 (file)
@@ -148,7 +148,12 @@ class SuperCommand(Command):
     def _run(self, myname, subcommand=None, *args):
         if subcommand in self.subcommands:
             return self.subcommands[subcommand]._run(subcommand, *args)
-        print "Usage: samba-tool %s <subcommand> [options]" % myname
+        
+        if (myname == "samba-tool"):
+            usage = "samba-tool <subcommand>"
+        else:
+            usage = "samba-tool %s <subcommand>" % myname
+        print "Usage: %s [options]" %usage        
         print "Available subcommands:"
         subcmds = self.subcommands.keys()
         subcmds.sort()
@@ -157,7 +162,7 @@ class SuperCommand(Command):
         if subcommand in [None]:
             raise CommandError("You must specify a subcommand")
         if subcommand in ['help', '-h', '--help']:
-            print "For more help on a specific subcommand, please type: samba-tool %s <subcommand> (-h|--help)" % myname
+            print "For more help on a specific subcommand, please type: %s (-h|--help)" % usage
             return 0
         raise CommandError("No such subcommand '%s'" % subcommand)