blob: c5979fd6d5dc3a05357dd7712bf6f45017a8e6b8 [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
97
98 self.nl()
99
100 self.heading('%s', heading)
101 self.nl()
102
103 self.heading('%s', ''.ljust(len(heading), '-'))
104 self.nl()
105
106 me = 'repo %s' % cmd.NAME
107 body = body.strip()
108 body = body.replace('%prog', me)
109
Shawn O. Pearce938d6082009-04-21 08:01:17 -0700110 asciidoc_hdr = re.compile(r'^\n?([^\n]{1,})\n([=~-]{2,})$')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700111 for para in body.split("\n\n"):
112 if para.startswith(' '):
113 self.write('%s', para)
114 self.nl()
115 self.nl()
Shawn O. Pearce43c3d9e2009-03-04 14:26:50 -0800116 continue
117
118 m = asciidoc_hdr.match(para)
119 if m:
Shawn O. Pearce938d6082009-04-21 08:01:17 -0700120 title = m.group(1)
121 type = m.group(2)
122 if type[0] in ('=', '-'):
123 p = self.heading
124 else:
125 def _p(fmt, *args):
126 self.write(' ')
127 self.heading(fmt, *args)
128 p = _p
129
130 p('%s', title)
Shawn O. Pearce43c3d9e2009-03-04 14:26:50 -0800131 self.nl()
Shawn O. Pearce938d6082009-04-21 08:01:17 -0700132 p('%s', ''.ljust(len(title),type[0]))
Shawn O. Pearce43c3d9e2009-03-04 14:26:50 -0800133 self.nl()
134 continue
135
136 self.wrap.add_flowing_data(para)
137 self.wrap.end_paragraph(1)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700138 self.wrap.end_paragraph(0)
139
140 out = _Out(self.manifest.globalConfig)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700141 out._PrintSection('Summary', 'helpSummary')
Shawn O. Pearce5da554f2009-04-18 11:44:00 -0700142 cmd.OptionParser.print_help()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700143 out._PrintSection('Description', 'helpDescription')
144
145 def _Options(self, p):
146 p.add_option('-a', '--all',
147 dest='show_all', action='store_true',
148 help='show the complete list of commands')
149
150 def Execute(self, opt, args):
151 if len(args) == 0:
152 if opt.show_all:
153 self._PrintAllCommands()
154 else:
155 self._PrintCommonCommands()
156
157 elif len(args) == 1:
158 name = args[0]
159
160 try:
161 cmd = self.commands[name]
162 except KeyError:
163 print >>sys.stderr, "repo: '%s' is not a repo command." % name
164 sys.exit(1)
165
166 self._PrintCommandHelp(cmd)
167
168 else:
169 self._PrintCommandHelp(self)