Merged Anton Batenev's report of Nicolas Alvarez' unicode-in-be-new bug
[be.git] / libbe / command / help.py
1 # Copyright (C) 2006-2010 Aaron Bentley and Panometrics, Inc.
2 #                         Gianluca Montecchi <gian@grys.it>
3 #                         Thomas Gerigk <tgerigk@gmx.de>
4 #                         W. Trevor King <wking@drexel.edu>
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with this program; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 import libbe
21 import libbe.command
22 import libbe.command.util
23
24 TOPICS = {}
25
26 class Help (libbe.command.Command):
27     """Print help for given command or topic
28
29     >>> import sys
30     >>> import libbe.bugdir
31     >>> io = libbe.command.StringInputOutput()
32     >>> io.stdout = sys.stdout
33     >>> ui = libbe.command.UserInterface(io=io)
34     >>> cmd = Help()
35
36     >>> ret = ui.run(cmd, args=['help'])
37     usage: be help [options] [TOPIC]
38     <BLANKLINE>
39     Options:
40       -h, --help  Print a help message.
41     <BLANKLINE>
42       --complete  Print a list of possible completions.
43     <BLANKLINE>
44     <BLANKLINE>
45     Print help for specified command/topic or list of all commands.
46     """
47     name = 'help'
48
49     def __init__(self, *args, **kwargs):
50         libbe.command.Command.__init__(self, *args, **kwargs)
51         self.args.extend([
52                 libbe.command.Argument(
53                     name='topic', metavar='TOPIC', default=None,
54                     optional=True,
55                     completion_callback=self.complete_topic)
56                 ])
57
58     def _run(self, **params):
59         if params['topic'] == None:
60             if hasattr(self.ui, 'help'):
61                 print >> self.stdout, self.ui.help().rstrip('\n')
62         elif params['topic'] in libbe.command.commands():
63             module = libbe.command.get_command(params['topic'])
64             Class = libbe.command.get_command_class(module,params['topic'])
65             c = Class(ui=self.ui)
66             print >> self.stdout, c.help().rstrip('\n')
67         elif params['topic'] in TOPICS:
68             print >> self.stdout, TOPICS[params['topic']].rstrip('\n')
69         else:
70             raise libbe.command.UserError(
71                 '"%s" is neither a command nor topic' % params['topic'])
72         return 0
73
74     def _long_help(self):
75         return """
76 Print help for specified command/topic or list of all commands.
77 """
78
79     def complete_topic(self, command, argument, fragment=None):
80         commands = libbe.command.util.complete_command()
81         topics = sorted(TOPICS.keys())
82         return commands + topics