blob: 0df3c14b396b35a512677c1b32471706eca08903 [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
Shawn O. Pearce43c3d9e2009-03-04 14:26:50 -080016import re
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070017import sys
18from formatter import AbstractFormatter, DumbWriter
19
20from color import Coloring
Shawn O. Pearcec95583b2009-03-03 17:47:06 -080021from command import PagedCommand, MirrorSafeCommand
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070022
Shawn O. Pearcec95583b2009-03-03 17:47:06 -080023class Help(PagedCommand, MirrorSafeCommand):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070024 common = False
25 helpSummary = "Display detailed help on a command"
26 helpUsage = """
27%prog [--all|command]
28"""
29 helpDescription = """
30Displays detailed usage information about a command.
31"""
32
33 def _PrintAllCommands(self):
34 print 'usage: repo COMMAND [ARGS]'
35 print """
36The complete list of recognized repo commands are:
37"""
38 commandNames = self.commands.keys()
39 commandNames.sort()
40
41 maxlen = 0
42 for name in commandNames:
43 maxlen = max(maxlen, len(name))
44 fmt = ' %%-%ds %%s' % maxlen
45
46 for name in commandNames:
47 command = self.commands[name]
48 try:
49 summary = command.helpSummary.strip()
50 except AttributeError:
51 summary = ''
52 print fmt % (name, summary)
53 print """
54See 'repo help <command>' for more information on a specific command.
55"""
56
57 def _PrintCommonCommands(self):
58 print 'usage: repo COMMAND [ARGS]'
59 print """
60The most commonly used repo commands are:
61"""
62 commandNames = [name
63 for name in self.commands.keys()
64 if self.commands[name].common]
65 commandNames.sort()
66
67 maxlen = 0
68 for name in commandNames:
69 maxlen = max(maxlen, len(name))
70 fmt = ' %%-%ds %%s' % maxlen
71
72 for name in commandNames:
73 command = self.commands[name]
74 try:
75 summary = command.helpSummary.strip()
76 except AttributeError:
77 summary = ''
78 print fmt % (name, summary)
79 print """
80See 'repo help <command>' for more information on a specific command.
Shawn O. Pearce4259b8a2009-03-04 14:03:16 -080081See 'repo help --all' for a complete list of recognized commands.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070082"""
83
84 def _PrintCommandHelp(self, cmd):
85 class _Out(Coloring):
86 def __init__(self, gc):
87 Coloring.__init__(self, gc, 'help')
88 self.heading = self.printer('heading', attr='bold')
89
90 self.wrap = AbstractFormatter(DumbWriter())
91
92 def _PrintSection(self, heading, bodyAttr):
93 try:
94 body = getattr(cmd, bodyAttr)
95 except AttributeError:
96 return
Shawn O. Pearcec7c57e32009-06-03 17:43:16 -070097 if body == '' or body is None:
98 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070099
100 self.nl()
101
102 self.heading('%s', heading)
103 self.nl()
104
105 self.heading('%s', ''.ljust(len(heading), '-'))
106 self.nl()
107
108 me = 'repo %s' % cmd.NAME
109 body = body.strip()
110 body = body.replace('%prog', me)
111
Shawn O. Pearce938d6082009-04-21 08:01:17 -0700112 asciidoc_hdr = re.compile(r'^\n?([^\n]{1,})\n([=~-]{2,})$')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700113 for para in body.split("\n\n"):
114 if para.startswith(' '):
115 self.write('%s', para)
116 self.nl()
117 self.nl()
Shawn O. Pearce43c3d9e2009-03-04 14:26:50 -0800118 continue
119
120 m = asciidoc_hdr.match(para)
121 if m:
Shawn O. Pearce938d6082009-04-21 08:01:17 -0700122 title = m.group(1)
123 type = m.group(2)
124 if type[0] in ('=', '-'):
125 p = self.heading
126 else:
127 def _p(fmt, *args):
128 self.write(' ')
129 self.heading(fmt, *args)
130 p = _p
131
132 p('%s', title)
Shawn O. Pearce43c3d9e2009-03-04 14:26:50 -0800133 self.nl()
Shawn O. Pearce938d6082009-04-21 08:01:17 -0700134 p('%s', ''.ljust(len(title),type[0]))
Shawn O. Pearce43c3d9e2009-03-04 14:26:50 -0800135 self.nl()
136 continue
137
138 self.wrap.add_flowing_data(para)
139 self.wrap.end_paragraph(1)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700140 self.wrap.end_paragraph(0)
141
142 out = _Out(self.manifest.globalConfig)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700143 out._PrintSection('Summary', 'helpSummary')
Shawn O. Pearce5da554f2009-04-18 11:44:00 -0700144 cmd.OptionParser.print_help()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700145 out._PrintSection('Description', 'helpDescription')
146
147 def _Options(self, p):
148 p.add_option('-a', '--all',
149 dest='show_all', action='store_true',
150 help='show the complete list of commands')
151
152 def Execute(self, opt, args):
153 if len(args) == 0:
154 if opt.show_all:
155 self._PrintAllCommands()
156 else:
157 self._PrintCommonCommands()
158
159 elif len(args) == 1:
160 name = args[0]
161
162 try:
163 cmd = self.commands[name]
164 except KeyError:
165 print >>sys.stderr, "repo: '%s' is not a repo command." % name
166 sys.exit(1)
167
Shawn O. Pearce752371d2011-10-11 15:23:37 -0700168 cmd.manifest = self.manifest
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700169 self._PrintCommandHelp(cmd)
170
171 else:
172 self._PrintCommandHelp(self)