Mercurial > piecrust2
comparison 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 |
comparison
equal
deleted
inserted
replaced
98:5959a117a943 | 99:8703be118430 |
---|---|
1 import io | |
1 import sys | 2 import sys |
2 import time | 3 import time |
3 import os.path | 4 import os.path |
4 import logging | 5 import logging |
5 import argparse | 6 import argparse |
6 import colorama | 7 import colorama |
7 from piecrust import APP_VERSION | 8 from piecrust import APP_VERSION |
8 from piecrust.app import PieCrust, PieCrustConfiguration | 9 from piecrust.app import PieCrust, PieCrustConfiguration |
9 from piecrust.chefutil import format_timed, log_friendly_exception | 10 from piecrust.chefutil import (format_timed, log_friendly_exception, |
11 print_help_item) | |
10 from piecrust.commands.base import CommandContext | 12 from piecrust.commands.base import CommandContext |
11 from piecrust.environment import StandardEnvironment | 13 from piecrust.environment import StandardEnvironment |
12 from piecrust.pathutil import SiteNotFoundError, find_app_root | 14 from piecrust.pathutil import SiteNotFoundError, find_app_root |
13 from piecrust.plugins.base import PluginLoader | 15 from piecrust.plugins.base import PluginLoader |
14 | 16 |
165 app.config.applyVariant('variants/' + pre_args.config_variant) | 167 app.config.applyVariant('variants/' + pre_args.config_variant) |
166 | 168 |
167 # Setup the arg parser. | 169 # Setup the arg parser. |
168 parser = argparse.ArgumentParser( | 170 parser = argparse.ArgumentParser( |
169 prog='chef', | 171 prog='chef', |
170 description="The PieCrust chef manages your website.") | 172 description="The PieCrust chef manages your website.", |
173 formatter_class=argparse.RawDescriptionHelpFormatter) | |
171 parser.add_argument('--version', action='version', version=('%(prog)s ' + APP_VERSION)) | 174 parser.add_argument('--version', action='version', version=('%(prog)s ' + APP_VERSION)) |
172 parser.add_argument('--root', help="The root directory of the website.") | 175 parser.add_argument('--root', help="The root directory of the website.") |
173 parser.add_argument('--config', help="The configuration variant to use for this command.") | 176 parser.add_argument('--config', help="The configuration variant to use for this command.") |
174 parser.add_argument('--debug', help="Show debug information.", action='store_true') | 177 parser.add_argument('--debug', help="Show debug information.", action='store_true') |
175 parser.add_argument('--no-cache', help="When applicable, disable caching.", action='store_true') | 178 parser.add_argument('--no-cache', help="When applicable, disable caching.", action='store_true') |
177 parser.add_argument('--log', help="Send log messages to the specified file.") | 180 parser.add_argument('--log', help="Send log messages to the specified file.") |
178 parser.add_argument('--log-debug', help="Log debug messages to the log file.", action='store_true') | 181 parser.add_argument('--log-debug', help="Log debug messages to the log file.", action='store_true') |
179 | 182 |
180 commands = sorted(app.plugin_loader.getCommands(), | 183 commands = sorted(app.plugin_loader.getCommands(), |
181 key=lambda c: c.name) | 184 key=lambda c: c.name) |
182 subparsers = parser.add_subparsers() | 185 subparsers = parser.add_subparsers(title='list of commands') |
183 for c in commands: | 186 for c in commands: |
184 p = subparsers.add_parser(c.name, help=c.description) | 187 p = subparsers.add_parser(c.name, help=c.description) |
185 c.setupParser(p, app) | 188 c.setupParser(p, app) |
186 p.set_defaults(func=c.checkedRun) | 189 p.set_defaults(func=c.checkedRun) |
190 | |
187 help_cmd = next(filter(lambda c: c.name == 'help', commands), None) | 191 help_cmd = next(filter(lambda c: c.name == 'help', commands), None) |
188 if help_cmd and help_cmd.has_topics: | 192 if help_cmd and help_cmd.has_topics: |
189 parser.epilog = ("Additional help topics: " + | 193 with io.StringIO() as epilog: |
190 ', '.join(help_cmd.getTopicNames())) | 194 epilog.write("additional help topics:\n") |
195 for name, desc in help_cmd.getTopics(): | |
196 print_help_item(epilog, name, desc) | |
197 parser.epilog = epilog.getvalue() | |
191 | 198 |
192 | 199 |
193 # Parse the command line. | 200 # Parse the command line. |
194 result = parser.parse_args() | 201 result = parser.parse_args() |
195 logger.debug(format_timed(start_time, 'initialized PieCrust', colored=False)) | 202 logger.debug(format_timed(start_time, 'initialized PieCrust', colored=False)) |