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-04 16:02:59 +0000 |
commit | 249592e84e875a891ee2f4510753a160d06a7249 (patch) | |
tree | 7f25693792e46d13fe9b71fffb903361a5e60b47 | |
parent | 55482b2c6f01f6586750a8d0071416611934475c (diff) | |
download | morph-baserock/markdoffman/s10382/add-help-option-v3.tar.gz |
Add write and configuration extensions to help.baserock/markdoffman/s10382/add-help-option-v3
Add a command 'help-extensions' to list all extensions.
Add the ability to find help on an extension by calling
'morph help [extension name]'. This will then call
the extension with the '--help' option to obtain help text.
-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 9ceca9a8..e5256f29 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() @@ -473,41 +475,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) + with extensions.get_extension_filename(build_ref_prefix, + name, + kind) as fname: + status, output, error = \ + self.runcmd_unchecked([fname, '--help']) + if status == 0 and output: + self.output.write(output) + else: + 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)) |