b0b182d2c0d0e5b0ed695b045904fa77a0d41dae
[be.git] / becommands / help.py
1 # Copyright (C) 2006-2009 Aaron Bentley and Panometrics, Inc.
2 #                         Thomas Gerigk <tgerigk@gmx.de>
3 #                         W. Trevor King <wking@drexel.edu>
4 # <tgerigk@gmx.de>
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
17 #    along with this program; if not, write to the Free Software
18 #    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 """Print help for given subcommand"""
20 from libbe import cmdutil, utility
21 __desc__ = __doc__
22
23 def execute(args):
24     """
25     Print help of specified command.
26     >>> execute(["help"])
27     Usage: be help [COMMAND]
28     <BLANKLINE>
29     Options:
30       -h, --help  Print a help message
31       --complete  Print a list of available completions
32     <BLANKLINE>
33     Print help for specified command or list of all commands.
34     <BLANKLINE>
35     """
36     parser = get_parser()
37     options, args = parser.parse_args(args)
38     complete(options, args, parser)
39     if len(args) > 1:
40         raise cmdutil.UsageError("Too many arguments.")
41     if len(args) == 0:
42         print cmdutil.help()
43     else:
44         try:
45             print cmdutil.help(args[0])
46         except AttributeError:
47             print "No help available"    
48
49 def get_parser():
50     parser = cmdutil.CmdOptionParser("be help [COMMAND]")
51     return parser
52
53 longhelp="""
54 Print help for specified command or list of all commands.
55 """
56
57 def help():
58     return get_parser().help_str() + longhelp
59
60 def complete(options, args, parser):
61     for option, value in cmdutil.option_value_pairs(options, parser):
62         if value == "--complete":
63             # no argument-options at the moment, so this is future-proofing
64             raise cmdutil.GetCompletions()
65     if "--complete" in args:
66         cmds = [command for command,module in cmdutil.iter_commands()]
67         raise cmdutil.GetCompletions(cmds)