s4:samba-tool: use normal option parsing in SuperCommand
authorStefan Metzmacher <metze@samba.org>
Mon, 8 Oct 2012 10:47:47 +0000 (12:47 +0200)
committerStefan Metzmacher <metze@samba.org>
Mon, 8 Oct 2012 14:13:06 +0000 (16:13 +0200)
We use the epilog to print the subcommands.

metze

source4/scripting/python/samba/netcmd/__init__.py

index 677f4f0fc2a40a93a1dabcd45dca006cfd182e87..a3edf50516567527a6fb226357b463e9f97ca285 100644 (file)
@@ -201,22 +201,25 @@ class SuperCommand(Command):
             return self.subcommands[subcommand]._run(
                 "%s %s" % (myname, subcommand), *args)
 
-        self.usage(myname)
-        self.outf.write("Available subcommands:\n")
+        epilog = "\nAvailable subcommands:\n"
         subcmds = self.subcommands.keys()
         subcmds.sort()
         max_length = max([len(c) for c in subcmds])
         for cmd_name in subcmds:
             cmd = self.subcommands[cmd_name]
             if not cmd.hidden:
-                self.outf.write("  %*s  - %s\n" % (
-                    -max_length, cmd_name, cmd.short_description))
-        if subcommand in [None]:
-            raise CommandError("You must specify a subcommand")
-        if subcommand in ['help', '-h', '--help']:
-            self.outf.write("For more help on a specific subcommand, please type: %s <subcommand> (-h|--help)\n" % myname)
-            return 0
-        raise CommandError("No such subcommand '%s'" % subcommand)
+                epilog += "  %*s  - %s\n" % (
+                    -max_length, cmd_name, cmd.short_description)
+        epilog += "For more help on a specific subcommand, please type: %s <subcommand> (-h|--help)\n" % myname
+
+        parser, optiongroups = self._create_parser(myname, epilog=epilog)
+        args_list = list(args)
+        if subcommand:
+            args_list.insert(0, subcommand)
+        opts, args = parser.parse_args(args_list)
+
+        parser.print_help()
+        return -1
 
 
 class CommandError(Exception):