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