Revert "samba-tool: moved takes_optiongroups definition to Command base class"
[tridge/samba.git] / source4 / scripting / python / samba / netcmd / __init__.py
index 831ac6ba57bb5c77a8dd23aa129633a0e9d26f59..f0cf7809c2994413260fb4c607249f7d029337cc 100644 (file)
@@ -3,7 +3,6 @@
 # Unix SMB/CIFS implementation.
 # Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2009-2011
 # Copyright (C) Theresa Halloran <theresahalloran@gmail.com> 2011
-# Copyright (C) Giampaolo Lauria <lauria2@yahoo.com> 2011
 #
 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
@@ -25,10 +24,23 @@ from ldb import LdbError
 import sys, traceback
 import textwrap
 
-
 class Option(optparse.Option):
     pass
 
+# This help formatter does text wrapping and preserves newlines
+class PlainHelpFormatter(optparse.IndentedHelpFormatter):
+    def format_description(self,description=""):
+            desc_width = self.width - self.current_indent
+            indent = " "*self.current_indent
+            paragraphs = description.split('\n')
+            wrapped_paragraphs = [
+                textwrap.fill(p,
+                        desc_width,
+                        initial_indent=indent,
+                        subsequent_indent=indent)
+                for p in paragraphs]
+            result = "\n".join(wrapped_paragraphs) + "\n"
+            return result
 
 
 class Command(object):
@@ -58,13 +70,11 @@ class Command(object):
     synopsis = None
     takes_args = []
     takes_options = []
-    takes_optiongroups = {
-        "sambaopts": options.SambaOptions,
-        "credopts": options.CredentialsOptions,
-        "versionopts": options.VersionOptions,
-        }
-    outf = sys.stdout
-    errf = sys.stderr
+    takes_optiongroups = {}
+
+    def __init__(self, outf=sys.stdout, errf=sys.stderr):
+        self.outf = outf
+        self.errf = errf
 
     def usage(self, prog, *args):
         parser, _ = self._create_parser(prog)
@@ -107,6 +117,7 @@ class Command(object):
         parser = optparse.OptionParser(
             usage=self.synopsis,
             description=self.full_description,
+            formatter=PlainHelpFormatter(),
             prog=prog)
         parser.add_options(self.takes_options)
         optiongroups = {}
@@ -168,6 +179,8 @@ class Command(object):
 class SuperCommand(Command):
     """A samba-tool command with subcommands."""
 
+    synopsis = "%prog <subcommand>"
+
     subcommands = {}
 
     def _run(self, myname, subcommand=None, *args):