Mercurial > piecrust2
comparison piecrust/main.py @ 57:c8c522dacfea
Add `help` function, cleanup argument handling.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Tue, 26 Aug 2014 23:18:32 -0700 |
parents | a62452ab5080 |
children | 64f37c4cce68 |
comparison
equal
deleted
inserted
replaced
56:2d617b889b00 | 57:c8c522dacfea |
---|---|
4 import logging | 4 import logging |
5 import argparse | 5 import argparse |
6 import colorama | 6 import colorama |
7 from piecrust.app import PieCrust, PieCrustConfiguration, APP_VERSION | 7 from piecrust.app import PieCrust, PieCrustConfiguration, APP_VERSION |
8 from piecrust.chefutil import format_timed, log_friendly_exception | 8 from piecrust.chefutil import format_timed, log_friendly_exception |
9 from piecrust.commands.base import CommandContext | |
9 from piecrust.environment import StandardEnvironment | 10 from piecrust.environment import StandardEnvironment |
10 from piecrust.pathutil import SiteNotFoundError, find_app_root | 11 from piecrust.pathutil import SiteNotFoundError, find_app_root |
11 from piecrust.plugins.base import PluginLoader | 12 from piecrust.plugins.base import PluginLoader |
12 | 13 |
13 | 14 |
149 raise SiteNotFoundError("Can't apply any variant.") | 150 raise SiteNotFoundError("Can't apply any variant.") |
150 app.config.applyVariant('variants/' + pre_args.config_variant) | 151 app.config.applyVariant('variants/' + pre_args.config_variant) |
151 | 152 |
152 # Setup the arg parser. | 153 # Setup the arg parser. |
153 parser = argparse.ArgumentParser( | 154 parser = argparse.ArgumentParser( |
155 prog='chef', | |
154 description="The PieCrust chef manages your website.") | 156 description="The PieCrust chef manages your website.") |
155 parser.add_argument('--version', action='version', version=('%(prog)s ' + APP_VERSION)) | 157 parser.add_argument('--version', action='version', version=('%(prog)s ' + APP_VERSION)) |
156 parser.add_argument('--root', help="The root directory of the website.") | 158 parser.add_argument('--root', help="The root directory of the website.") |
157 parser.add_argument('--config', help="The configuration variant to use for this command.") | 159 parser.add_argument('--config', help="The configuration variant to use for this command.") |
158 parser.add_argument('--debug', help="Show debug information.", action='store_true') | 160 parser.add_argument('--debug', help="Show debug information.", action='store_true') |
164 key=lambda c: c.name) | 166 key=lambda c: c.name) |
165 subparsers = parser.add_subparsers() | 167 subparsers = parser.add_subparsers() |
166 for c in commands: | 168 for c in commands: |
167 p = subparsers.add_parser(c.name, help=c.description) | 169 p = subparsers.add_parser(c.name, help=c.description) |
168 c.setupParser(p, app) | 170 c.setupParser(p, app) |
169 p.set_defaults(func=c._runFromChef) | 171 p.set_defaults(func=c.checkedRun) |
172 help_cmd = next(filter(lambda c: c.name == 'help', commands), None) | |
173 if help_cmd and help_cmd.has_topics: | |
174 parser.epilog = ("Additional help topics: " + | |
175 ', '.join(help_cmd.getTopicNames())) | |
176 | |
170 | 177 |
171 # Parse the command line. | 178 # Parse the command line. |
172 result = parser.parse_args() | 179 result = parser.parse_args() |
173 logger.debug(format_timed(start_time, 'initialized PieCrust', colored=False)) | 180 logger.debug(format_timed(start_time, 'initialized PieCrust', colored=False)) |
174 | 181 |
175 # Run the command! | 182 # Run the command! |
176 exit_code = result.func(app, result) | 183 ctx = CommandContext(app, parser, result) |
184 exit_code = result.func(ctx) | |
177 return exit_code | 185 return exit_code |
178 | 186 |