be --dir DIR COMMAND now roots the bugdir in DIR without changing directories.
[be.git] / becommands / help.py
1 # Copyright (C) 2006-2009 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 """Print help for given subcommand"""
20 from libbe import cmdutil, utility
21 __desc__ = __doc__
22
23 def execute(args, manipulate_encodings=True, restrict_file_access=False,
24             dir="."):
25     """
26     Print help of specified command (the manipulate_encodings argument
27     is ignored).
28
29     >>> execute(["help"])
30     Usage: be help [COMMAND]
31     <BLANKLINE>
32     Options:
33       -h, --help  Print a help message
34       --complete  Print a list of available completions
35     <BLANKLINE>
36     Print help for specified command or list of all commands.
37     <BLANKLINE>
38     """
39     parser = get_parser()
40     options, args = parser.parse_args(args)
41     complete(options, args, parser)
42     if len(args) > 1:
43         raise cmdutil.UsageError("Too many arguments.")
44     if len(args) == 0:
45         print cmdutil.help()
46     else:
47         try:
48             print cmdutil.help(args[0])
49         except AttributeError:
50             print "No help available"    
51
52 def get_parser():
53     parser = cmdutil.CmdOptionParser("be help [COMMAND]")
54     return parser
55
56 longhelp="""
57 Print help for specified command or list of all commands.
58 """
59
60 def help():
61     return get_parser().help_str() + longhelp
62
63 def complete(options, args, parser):
64     for option, value in cmdutil.option_value_pairs(options, parser):
65         if value == "--complete":
66             # no argument-options at the moment, so this is future-proofing
67             raise cmdutil.GetCompletions()
68     if "--complete" in args:
69         cmds = [command for command,module in cmdutil.iter_commands()]
70         raise cmdutil.GetCompletions(cmds)