blob: a68e37d259b9af9d4bf4b5f3e508b099956f715b [file] [log] [blame]
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001#
2# Copyright (C) 2008 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16import sys
17from formatter import AbstractFormatter, DumbWriter
18
19from color import Coloring
Shawn O. Pearcec95583b2009-03-03 17:47:06 -080020from command import PagedCommand, MirrorSafeCommand
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070021
Shawn O. Pearcec95583b2009-03-03 17:47:06 -080022class Help(PagedCommand, MirrorSafeCommand):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070023 common = False
24 helpSummary = "Display detailed help on a command"
25 helpUsage = """
26%prog [--all|command]
27"""
28 helpDescription = """
29Displays detailed usage information about a command.
30"""
31
32 def _PrintAllCommands(self):
33 print 'usage: repo COMMAND [ARGS]'
34 print """
35The complete list of recognized repo commands are:
36"""
37 commandNames = self.commands.keys()
38 commandNames.sort()
39
40 maxlen = 0
41 for name in commandNames:
42 maxlen = max(maxlen, len(name))
43 fmt = ' %%-%ds %%s' % maxlen
44
45 for name in commandNames:
46 command = self.commands[name]
47 try:
48 summary = command.helpSummary.strip()
49 except AttributeError:
50 summary = ''
51 print fmt % (name, summary)
52 print """
53See 'repo help <command>' for more information on a specific command.
54"""
55
56 def _PrintCommonCommands(self):
57 print 'usage: repo COMMAND [ARGS]'
58 print """
59The most commonly used repo commands are:
60"""
61 commandNames = [name
62 for name in self.commands.keys()
63 if self.commands[name].common]
64 commandNames.sort()
65
66 maxlen = 0
67 for name in commandNames:
68 maxlen = max(maxlen, len(name))
69 fmt = ' %%-%ds %%s' % maxlen
70
71 for name in commandNames:
72 command = self.commands[name]
73 try:
74 summary = command.helpSummary.strip()
75 except AttributeError:
76 summary = ''
77 print fmt % (name, summary)
78 print """
79See 'repo help <command>' for more information on a specific command.
Shawn O. Pearce4259b8a2009-03-04 14:03:16 -080080See 'repo help --all' for a complete list of recognized commands.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070081"""
82
83 def _PrintCommandHelp(self, cmd):
84 class _Out(Coloring):
85 def __init__(self, gc):
86 Coloring.__init__(self, gc, 'help')
87 self.heading = self.printer('heading', attr='bold')
88
89 self.wrap = AbstractFormatter(DumbWriter())
90
91 def _PrintSection(self, heading, bodyAttr):
92 try:
93 body = getattr(cmd, bodyAttr)
94 except AttributeError:
95 return
96
97 self.nl()
98
99 self.heading('%s', heading)
100 self.nl()
101
102 self.heading('%s', ''.ljust(len(heading), '-'))
103 self.nl()
104
105 me = 'repo %s' % cmd.NAME
106 body = body.strip()
107 body = body.replace('%prog', me)
108
109 for para in body.split("\n\n"):
110 if para.startswith(' '):
111 self.write('%s', para)
112 self.nl()
113 self.nl()
114 else:
115 self.wrap.add_flowing_data(para)
116 self.wrap.end_paragraph(1)
117 self.wrap.end_paragraph(0)
118
119 out = _Out(self.manifest.globalConfig)
120 cmd.OptionParser.print_help()
121 out._PrintSection('Summary', 'helpSummary')
122 out._PrintSection('Description', 'helpDescription')
123
124 def _Options(self, p):
125 p.add_option('-a', '--all',
126 dest='show_all', action='store_true',
127 help='show the complete list of commands')
128
129 def Execute(self, opt, args):
130 if len(args) == 0:
131 if opt.show_all:
132 self._PrintAllCommands()
133 else:
134 self._PrintCommonCommands()
135
136 elif len(args) == 1:
137 name = args[0]
138
139 try:
140 cmd = self.commands[name]
141 except KeyError:
142 print >>sys.stderr, "repo: '%s' is not a repo command." % name
143 sys.exit(1)
144
145 self._PrintCommandHelp(cmd)
146
147 else:
148 self._PrintCommandHelp(self)