diff piecrust/main.py @ 99:8703be118430

Changes to `help` command and extendable commands: - The `help` command has a prettier output, with descriptions of help topics. - `ExtendableChefCommand` is not assuming extensions are sub-commands anymore.
author Ludovic Chabant <ludovic@chabant.com>
date Sat, 13 Sep 2014 14:47:01 -0700
parents 5959a117a943
children 028df35a690e
line wrap: on
line diff
--- a/piecrust/main.py	Mon Sep 08 00:04:29 2014 -0700
+++ b/piecrust/main.py	Sat Sep 13 14:47:01 2014 -0700
@@ -1,3 +1,4 @@
+import io
 import sys
 import time
 import os.path
@@ -6,7 +7,8 @@
 import colorama
 from piecrust import APP_VERSION
 from piecrust.app import PieCrust, PieCrustConfiguration
-from piecrust.chefutil import format_timed, log_friendly_exception
+from piecrust.chefutil import (format_timed, log_friendly_exception,
+        print_help_item)
 from piecrust.commands.base import CommandContext
 from piecrust.environment import StandardEnvironment
 from piecrust.pathutil import SiteNotFoundError, find_app_root
@@ -167,7 +169,8 @@
     # Setup the arg parser.
     parser = argparse.ArgumentParser(
             prog='chef',
-            description="The PieCrust chef manages your website.")
+            description="The PieCrust chef manages your website.",
+            formatter_class=argparse.RawDescriptionHelpFormatter)
     parser.add_argument('--version', action='version', version=('%(prog)s ' + APP_VERSION))
     parser.add_argument('--root', help="The root directory of the website.")
     parser.add_argument('--config', help="The configuration variant to use for this command.")
@@ -179,15 +182,19 @@
 
     commands = sorted(app.plugin_loader.getCommands(),
             key=lambda c: c.name)
-    subparsers = parser.add_subparsers()
+    subparsers = parser.add_subparsers(title='list of commands')
     for c in commands:
         p = subparsers.add_parser(c.name, help=c.description)
         c.setupParser(p, app)
         p.set_defaults(func=c.checkedRun)
+
     help_cmd = next(filter(lambda c: c.name == 'help', commands), None)
     if help_cmd and help_cmd.has_topics:
-        parser.epilog = ("Additional help topics: " +
-                ', '.join(help_cmd.getTopicNames()))
+        with io.StringIO() as epilog:
+            epilog.write("additional help topics:\n")
+            for name, desc in help_cmd.getTopics():
+                print_help_item(epilog, name, desc)
+            parser.epilog = epilog.getvalue()
 
 
     # Parse the command line.