Fixup reStructuredText in README.
[hooke.git] / hooke / ui / commandline.py
index efed63488b553f66856ee82c9f42e334c72b868e..c82d5c5e9a47c29acb7a1dddc43368e4a5bf6ed5 100644 (file)
@@ -24,6 +24,7 @@ import codecs
 import cmd
 import logging
 import optparse
+import pprint
 try:
     import readline # including readline makes cmd.Cmd.cmdloop() smarter
 except ImportError, e:
@@ -74,7 +75,8 @@ class CommandLineParser (optparse.OptionParser):
                         try:
                             self.add_option(
                                 '--disable-%s' % name, dest=name,
-                                default=Default, action='store_false')
+                                default=Default, action='store_false',
+                                help=self._argument_help(a))
                         except optparse.OptionConflictError, e:
                             logging.warn('error in %s: %s' % (command, e))
                             raise
@@ -84,7 +86,8 @@ class CommandLineParser (optparse.OptionParser):
                         try:
                             self.add_option(
                                 '--enable-%s' % name, dest=name,
-                                default=Default, action='store_true')
+                                default=Default, action='store_true',
+                                help=self._argument_help(a))
                         except optparse.OptionConflictError, e:
                             logging.warn('error in %s: %s' % (command, e))
                             raise
@@ -97,7 +100,8 @@ class CommandLineParser (optparse.OptionParser):
                     type = 'string'
                 try:
                     self.add_option(
-                        '--%s' % name, dest=name, type=type, default=Default)
+                        '--%s' % name, dest=name, type=type, default=Default,
+                        help=self._argument_help(a))
                 except optparse.OptionConflictError, e:
                     logging.warn('error in %s: %s' % (command, e))
                     raise
@@ -113,6 +117,10 @@ class CommandLineParser (optparse.OptionParser):
             self.command_args.remove(infinite_counter)
             self.command_args.append(infinite_counter)
 
+    def _argument_help(self, argument):
+        return '%s (%s)' % (argument._help, argument.default)
+        # default in the case of callbacks, config-backed values, etc.?
+
     def exit(self, status=0, msg=None):
         """Override :meth:`optparse.OptionParser.exit` which calls
         :func:`sys.exit`.
@@ -145,7 +153,7 @@ class DoCommand (CommandMethod):
         try:
             args = self._parse_args(args)
         except optparse.OptParseError, e:
-            self.cmd.stdout.write(str(e).lstrip()+'\n')
+            self.cmd.stdout.write(unicode(e).lstrip()+'\n')
             self.cmd.stdout.write('Failure\n')
             return
         cm = CommandMessage(self.command.name, args)
@@ -156,7 +164,7 @@ class DoCommand (CommandMethod):
                 return True
             elif isinstance(msg, CommandExit):
                 self.cmd.stdout.write(msg.__class__.__name__+'\n')
-                self.cmd.stdout.write(str(msg).rstrip()+'\n')
+                self.cmd.stdout.write(unicode(msg).rstrip()+'\n')
                 break
             elif isinstance(msg, ReloadUserInterfaceConfig):
                 self.cmd.ui.reload_config(msg.config)
@@ -167,7 +175,11 @@ class DoCommand (CommandMethod):
                 except EOF:
                     return True
                 continue
-            self.cmd.stdout.write(unicode(msg).rstrip()+'\n')
+            if isinstance(msg, dict):
+                text = pprint.pformat(msg)
+            else:
+                text = unicode(msg)
+            self.cmd.stdout.write(text.rstrip()+'\n')
 
     def _parse_args(self, args):
         options,args = self.parser.parse_args(args)
@@ -235,7 +247,7 @@ class DoCommand (CommandMethod):
         while True:
             if error != None:
                 self.cmd.stdout.write(''.join([
-                        error.__class__.__name__, ': ', str(error), '\n']))
+                        error.__class__.__name__, ': ', unicode(error), '\n']))
             self.cmd.stdout.write(prompt_string)
             stdin = sys.stdin
             try:
@@ -336,14 +348,15 @@ class HelpCommand (CommandMethod):
         self.parser = CommandLineParser(self.command, self.name_fn)
 
     def __call__(self):
-        blocks = [self.command.help(name_fn=self.name_fn),
+        blocks = [self.parser.format_help(),
+                  self._command_message(),
                   '----',
                   'Usage: ' + self._usage_string(),
                   '']
         self.cmd.stdout.write('\n'.join(blocks))
 
-    def _message(self):
-        return self.command.help(name_fn=self.name_fn)
+    def _command_message(self):
+        return self.command._help
 
     def _usage_string(self):
         if len(self.parser.command_opts) == 0: