From f9ee7a537561be80b9c232dd4fc848ddb564f6b0 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Mon, 14 Dec 2009 20:33:35 -0500 Subject: Transitioned help to Command-format --- libbe/command/__init__.py | 5 ++- libbe/command/base.py | 1 + libbe/command/help.py | 93 ++++++++++++++++++++++++++--------------------- libbe/ui/command_line.py | 1 + 4 files changed, 57 insertions(+), 43 deletions(-) diff --git a/libbe/command/__init__.py b/libbe/command/__init__.py index 8558882..916b5ce 100644 --- a/libbe/command/__init__.py +++ b/libbe/command/__init__.py @@ -20,10 +20,11 @@ import base UserError = base.UserError UnknownCommand = base.UnknownCommand get_command = base.get_command +get_command_class = base.get_command_class commands = base.commands Option = base.Option Argument = base.Argument Command = base.Command -__all__ = [UserError, UnknownCommand, get_command, commands, - Option, Argument, Command] +__all__ = [UserError, UnknownCommand, get_command, get_command_class, + commands, Option, Argument, Command] diff --git a/libbe/command/base.py b/libbe/command/base.py index fe29908..54463c8 100644 --- a/libbe/command/base.py +++ b/libbe/command/base.py @@ -180,6 +180,7 @@ class Command (object): def __init__(self, input_encoding=None, output_encoding=None): self.status = None self.result = None + self.ui = None # calling user-interface, e.g. for Help() self.requires_bugdir = False self.requires_storage = False self.requires_unconnected_storage = False diff --git a/libbe/command/help.py b/libbe/command/help.py index 9e6d1aa..c8d700d 100644 --- a/libbe/command/help.py +++ b/libbe/command/help.py @@ -16,55 +16,66 @@ # You should have received a copy of the GNU General Public License along # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. -"""Print help for given subcommand""" -from libbe import cmdutil, utility -__desc__ = __doc__ -def execute(args, manipulate_encodings=True, restrict_file_access=False, - dir="."): - """ - Print help of specified command (the manipulate_encodings argument - is ignored). +import libbe +import libbe.command +import libbe.command.util + +TOPICS = {} + +class Help (libbe.command.Command): + """Print help for given command or topic + + >>> import sys + >>> import libbe.bugdir + >>> cmd = Help() + >>> cmd._setup_io = lambda i_enc,o_enc : None + >>> cmd.stdout = sys.stdout - >>> execute(["help"]) - Usage: be help [COMMAND] + >>> ret = cmd.run(args=['help']) + usage: be help [options] [TOPIC] Options: - -h, --help Print a help message - --complete Print a list of available completions + -h, --help Print a help message. - Print help for specified command or list of all commands. + --complete Print a list of possible completions. + + Print help for specified command/topic or list of all commands. """ - parser = get_parser() - options, args = parser.parse_args(args) - complete(options, args, parser) - if len(args) > 1: - raise cmdutil.UsageError("Too many arguments.") - if len(args) == 0: - print cmdutil.help() - else: - try: - print cmdutil.help(args[0]) - except AttributeError: - print "No help available" + name = 'help' -def get_parser(): - parser = cmdutil.CmdOptionParser("be help [COMMAND]") - return parser + def __init__(self, *args, **kwargs): + libbe.command.Command.__init__(self, *args, **kwargs) + self.args.extend([ + libbe.command.Argument( + name='topic', metavar='TOPIC', default=None, + optional=True, + completion_callback=self.complete_topic) + ]) -longhelp=""" -Print help for specified command or list of all commands. -""" + def _run(self, storage, bugdir, **params): + if params['topic'] == None: + if hasattr(self.ui, 'help'): + self.ui.help() + elif params['topic'] in libbe.command.commands(): + module = libbe.command.get_command(params['topic']) + Class = libbe.command.get_command_class(module,params['topic']) + c = Class() + print >> self.stdout, c.help().rstrip('\n') + elif params['topic'] in TOPICS: + print >> self.stdout, TOPICS[params['topic']].rstrip('\n') + else: + raise libbe.command.UserError( + '"%s" is neither a command nor topic' % params['topic']) + return 0 -def help(): - return get_parser().help_str() + longhelp + def _long_help(self): + return """ +Print help for specified command/topic or list of all commands. +""" -def complete(options, args, parser): - for option, value in cmdutil.option_value_pairs(options, parser): - if value == "--complete": - # no argument-options at the moment, so this is future-proofing - raise cmdutil.GetCompletions() - if "--complete" in args: - cmds = [command for command,module in cmdutil.iter_commands()] - raise cmdutil.GetCompletions(cmds) + def complete_topic(self, command, argument, fragment=None): + commands = libbe.command.util.complete_command() + topics = sorted(TOPICS.keys()) + return commands + topics diff --git a/libbe/ui/command_line.py b/libbe/ui/command_line.py index feeccd4..84f9450 100755 --- a/libbe/ui/command_line.py +++ b/libbe/ui/command_line.py @@ -265,6 +265,7 @@ def main(): return 1 Class = getattr(module, command_name.capitalize()) command = Class() + command.ui = self parser = CmdOptionParser(command) storage = None bugdir = None -- cgit