samba-tool: Fix error handling in SuperCommand class
authorGiampaolo Lauria <lauria2@yahoo.com>
Mon, 18 Jul 2011 15:30:23 +0000 (11:30 -0400)
committerAndrew Tridgell <tridge@samba.org>
Thu, 21 Jul 2011 01:44:30 +0000 (11:44 +1000)
Created show_command_error method to handle errors in SuperCommand
Removed statement in SuperCommand to raise exception

Signed-off-by: Andrew Tridgell <tridge@samba.org>
source4/scripting/python/samba/netcmd/__init__.py

index e5c5badb182b6fbbc6fb9d6796524fe423f86287..5e71a1c535b40fb359c310cc4a6383059d7f155f 100644 (file)
@@ -29,6 +29,7 @@ class Option(optparse.Option):
     pass
 
 
+
 class Command(object):
     """A net command."""
 
@@ -88,7 +89,6 @@ class Command(object):
         if force_traceback or samba.get_debug_level() >= 3:
             traceback.print_tb(etraceback)
 
-
     synopsis = property(_get_synopsis)
 
     outf = sys.stdout
@@ -146,6 +146,7 @@ class Command(object):
         raise NotImplementedError(self.run)
 
 
+
 class SuperCommand(Command):
     """A command with subcommands."""
 
@@ -159,7 +160,13 @@ class SuperCommand(Command):
             print "\t%-20s - %s" % (cmd, self.subcommands[cmd].description)
         if subcommand in [None, 'help', '-h', '--help' ]:
             return 0
-        raise CommandError("No such subcommand '%s'" % subcommand)
+        self.show_command_error("No such subcommand '%s'" % (subcommand))
+
+    def show_command_error(self, msg):
+        '''display a command error'''
+
+        print >>sys.stderr, "ERROR: %s" % (msg)
+        return -1
 
     def usage(self, myname, subcommand=None, *args):
         if subcommand is None or not subcommand in self.subcommands:
@@ -169,6 +176,7 @@ class SuperCommand(Command):
             return self.subcommands[subcommand].usage(*args)
 
 
+
 class CommandError(Exception):
     '''an exception class for netcmd errors'''
     def __init__(self, message, inner_exception=None):
@@ -177,6 +185,7 @@ class CommandError(Exception):
         self.exception_info = sys.exc_info()
 
 
+
 commands = {}
 from samba.netcmd.newuser import cmd_newuser
 commands["newuser"] = cmd_newuser()