diff options
author | Mark Doffman <mark.doffman@codethink.co.uk> | 2014-02-21 18:59:36 +0000 |
---|---|---|
committer | Mark Doffman <mark.doffman@codethink.co.uk> | 2014-03-31 21:38:44 +0000 |
commit | 764bb0acfb8744064d6081b00046c7f83808ced6 (patch) | |
tree | 845c66ea762607c47306152f1ba35ef39e16866a /morphlib | |
parent | 8fb1dac02b0be1502fd7b19de981e2a81468ffe2 (diff) | |
download | morph-764bb0acfb8744064d6081b00046c7f83808ced6.tar.gz |
Add write and configuration extensions to help.
Add a command 'help-extensions' to list all extensions.
Add the ability to find help on an extension by calling
'morph help [extension name]'.
Diffstat (limited to 'morphlib')
-rw-r--r-- | morphlib/app.py | 77 |
1 files changed, 45 insertions, 32 deletions
diff --git a/morphlib/app.py b/morphlib/app.py index 409e0a12..91647a32 100644 --- a/morphlib/app.py +++ b/morphlib/app.py @@ -22,6 +22,7 @@ import sys import time import urlparse import warnings +import extensions import morphlib @@ -60,10 +61,9 @@ class Morph(cliapp.Application): 'show no output unless there is an error') self.settings.boolean(['help', 'h'], - 'show this help message and exit') - + 'show this help message and exit') self.settings.boolean(['help-all'], - 'show help message including hidden subcommands') + 'show help message including hidden subcommands') self.settings.string(['build-ref-prefix'], 'Prefix to use for temporary build refs', @@ -198,6 +198,8 @@ class Morph(cliapp.Application): def setup(self): self.status_prefix = '' + self.add_subcommand('help-extensions', self.help_extensions) + def process_args(self, args): self.check_time() @@ -476,41 +478,52 @@ class Morph(cliapp.Application): compute_setting_values=self.compute_setting_values, add_help_option=False) - class IdentityFormat(): - def format(self, text): - return text - - def _help_helper(self, args, show_all): - try: - width = int(os.environ.get('COLUMNS', '78')) - except ValueError: - width = 78 - - if args: - cmd = args[0] - if cmd not in self.subcommands: - raise cliapp.AppException('Unknown subcommand %s' % cmd) - # TODO Search for other things we might want help on - # such as write or configuration extensions. - usage = self._format_usage_for(cmd) - fmt = self.IdentityFormat() - description = fmt.format(self._format_subcommand_help(cmd)) + def _help(self, show_all): + pp = self.settings.build_parser( + configs_only=True, + arg_synopsis=self.arg_synopsis, + cmd_synopsis=self.cmd_synopsis, + all_options=show_all, + add_help_option=False) + text = pp.format_help() + self.output.write(text) + + def _help_topic(self, topic): + build_ref_prefix = self.settings['build-ref-prefix'] + if topic in self.subcommands: + usage = self._format_usage_for(topic) + description = self._format_subcommand_help(topic) text = '%s\n\n%s' % (usage, description) self.output.write(text) + elif topic in extensions.list_extensions(build_ref_prefix): + name, kind = os.path.splitext(topic) + try: + with extensions.get_extension_filename(build_ref_prefix, + name, + kind + '.help', executable=False) as fname: + with open(fname, 'r') as f: + help_data = morphlib.yamlparse.load(f.read()) + print help_data['help'] + except extensions.ExtensionError: + raise cliapp.AppException( + 'Help not available for extension %s' % topic) else: - pp = self.settings.build_parser( - configs_only=True, - arg_synopsis=self.arg_synopsis, - cmd_synopsis=self.cmd_synopsis, - all_options=show_all, - add_help_option=False) - text = pp.format_help() - self.output.write(text) + raise cliapp.AppException( + 'Unknown subcommand or extension %s' % topic) def help(self, args): # pragma: no cover '''Print help.''' - self._help_helper(args, False) + if args: + self._help_topic(args[0]) + else: + self._help(False) def help_all(self, args): # pragma: no cover '''Print help, including hidden subcommands.''' - self._help_helper(args, True) + self._help(True) + + def help_extensions(self, args): + exts = extensions.list_extensions(self.settings['build-ref-prefix']) + template = "Extensions:\n %s\n" + ext_string = '\n '.join(exts) + self.output.write(template % (ext_string)) |