[jhbuild] [commands] simplify command line help (GNOME bug 606360)



commit ee4bf954709024d04527eff4b9c33ea0ee4b3745
Author: Marc-André Lureau <marcandre lureau gmail com>
Date:   Tue Feb 2 23:11:26 2010 +0100

    [commands] simplify command line help (GNOME bug 606360)
    
    This removes the distinction between --help and --help-commands, and adds a
    "help" command (equivalent to --help).

 jhbuild/commands/__init__.py |   47 +++++++++++++++++++++++++++++++++++++----
 jhbuild/commands/base.py     |    4 +-
 jhbuild/main.py              |   32 +++++++++-------------------
 3 files changed, 54 insertions(+), 29 deletions(-)
---
diff --git a/jhbuild/commands/__init__.py b/jhbuild/commands/__init__.py
index f19f216..53d64d4 100644
--- a/jhbuild/commands/__init__.py
+++ b/jhbuild/commands/__init__.py
@@ -47,9 +47,9 @@ class Command:
     def __init__(self, options=[]):
         self.options = options
 
-    def execute(self, config, args):
+    def execute(self, config, args, help):
         options, args = self.parse_args(args)
-        return self.run(config, options, args)
+        return self.run(config, options, args, help)
 
     def parse_args(self, args):
         self.parser = OptionParser(
@@ -58,20 +58,56 @@ class Command:
         self.parser.add_options(self.options)
         return self.parser.parse_args(args)
 
-    def run(self, config, options, args):
+    def run(self, config, options, args, help=None):
         """The body of the command"""
         raise NotImplementedError
 
 
+def print_help():
+    import os
+    thisdir = os.path.abspath(os.path.dirname(__file__))
+
+    # import all available commands
+    for fname in os.listdir(os.path.join(thisdir)):
+        name, ext = os.path.splitext(fname)
+        if not ext == '.py':
+            continue
+        try:
+            __import__('jhbuild.commands.%s' % name)
+        except ImportError:
+            pass
+
+    uprint(_('JHBuild commands are:'))
+    commands = [(x.name, x.doc) for x in get_commands().values()]
+    commands.sort()
+    for name, description in commands:
+        uprint('  %-15s %s' % (name, description))
+    print
+    uprint(_('For more information run "jhbuild <command> --help"'))
+
 # handle registration of new commands
 _commands = {}
 def register_command(command_class):
     _commands[command_class.name] = command_class
 
+# special help command, never run
+class cmd_help(Command):
+    doc = N_('Information about available jhbuild commands')
+
+    name = 'help'
+    usage_args = ''
+
+    def run(self, config, options, args, help=None):
+        if help:
+            return help()
+
+register_command(cmd_help)
+
+
 def get_commands():
     return _commands
 
-def run(command, config, args):
+def run(command, config, args, help):
     # if the command hasn't been registered, load a module by the same name
     if command not in _commands:
         try:
@@ -82,8 +118,9 @@ def run(command, config, args):
         raise FatalError(_('command not found'))
 
     command_class = _commands[command]
+
     cmd = command_class()
-    return cmd.execute(config, args)
+    return cmd.execute(config, args, help)
 
 
 from jhbuild.commands import base
diff --git a/jhbuild/commands/base.py b/jhbuild/commands/base.py
index 607eaf4..1f53cbb 100644
--- a/jhbuild/commands/base.py
+++ b/jhbuild/commands/base.py
@@ -376,7 +376,7 @@ class cmd_run(Command):
                         help=_('run command in checkout dir of the given module')),
             ])
 
-    def execute(self, config, args):
+    def execute(self, config, args, help=None):
         # Do a shallow check of the arguments list
         # so that '--' isn't always required when command has arguments, 
         # only if some of them look like they might be for us
@@ -442,7 +442,7 @@ class cmd_shell(Command):
     name = 'shell'
     usage_args = ''
 
-    def execute(self, config, args):
+    def execute(self, config, args, help=None):
         if "--help" in args:
             self.parse_args(args) # This doesn't return
         user_shell = os.environ.get('SHELL', '/bin/sh')
diff --git a/jhbuild/main.py b/jhbuild/main.py
index 55a7ee0..147ec23 100644
--- a/jhbuild/main.py
+++ b/jhbuild/main.py
@@ -75,26 +75,10 @@ class LoggingFormatter(logging.Formatter):
         record.level_name_initial = record.levelname[0]
         return logging.Formatter.format(self, record)
 
-def help_commands(option, opt_str, value, parser):
-    thisdir = os.path.abspath(os.path.dirname(__file__))
-    
-    # import all available commands
-    for fname in os.listdir(os.path.join(thisdir, 'commands')):
-        name, ext = os.path.splitext(fname)
-        if not ext == '.py':
-            continue
-        try:
-            __import__('jhbuild.commands.%s' % name)
-        except ImportError:
-            pass
-    
-    uprint(_('JHBuild commands are:'))
-    commands = [(x.name, x.doc) for x in jhbuild.commands.get_commands().values()]
-    commands.sort()
-    for name, description in commands:
-        uprint('  %-15s %s' % (name, description))
+def print_help(parser):
+    parser.print_help()
     print
-    uprint(_('For more information run "jhbuild <command> --help"'))
+    jhbuild.commands.print_help()
     parser.exit()
 
 def main(args):
@@ -109,11 +93,15 @@ def main(args):
     logging.getLogger().addHandler(logging_handler)
     parser = optparse.OptionParser(
         usage=_('%prog [ -f config ] command [ options ... ]'),
+        add_help_option=False,
         description=_('Build a set of modules from diverse repositories in correct dependency order (such as GNOME).'))
     parser.disable_interspersed_args()
+    parser.add_option('-h', '--help', action='callback',
+                      callback=lambda *args: print_help(parser),
+                      help=_("Display this help and exit"))
     parser.add_option('--help-commands', action='callback',
-                      callback=help_commands,
-                      help=_('Information about available jhbuild commands'))
+                      callback=lambda *args: print_help(parser),
+                      help=optparse.SUPPRESS_HELP)
     parser.add_option('-f', '--file', action='store', metavar='CONFIG',
                       type='string', dest='configfile',
                       default=os.environ.get("JHBUILDRC", os.path.join(os.environ['HOME'], '.jhbuildrc')),
@@ -145,7 +133,7 @@ def main(args):
     warn_local_modulesets(config)
 
     try:
-        rc = jhbuild.commands.run(command, config, args)
+        rc = jhbuild.commands.run(command, config, args, help=lambda: print_help(parser))
     except UsageError, exc:
         sys.stderr.write('jhbuild %s: %s\n' % (command, exc.args[0].encode(_encoding, 'replace')))
         parser.print_usage()



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]