blob: 4aa3f863140db3e0aa4cb883dccdead13d381fe5 [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
Sarah Owenscecd1d82012-11-01 22:59:27 -070016from __future__ import print_function
Shawn O. Pearce43c3d9e2009-03-04 14:26:50 -080017import re
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070018import sys
19from formatter import AbstractFormatter, DumbWriter
20
21from color import Coloring
Shawn O. Pearcec95583b2009-03-03 17:47:06 -080022from command import PagedCommand, MirrorSafeCommand
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070023
Shawn O. Pearcec95583b2009-03-03 17:47:06 -080024class Help(PagedCommand, MirrorSafeCommand):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070025 common = False
26 helpSummary = "Display detailed help on a command"
27 helpUsage = """
28%prog [--all|command]
29"""
30 helpDescription = """
31Displays detailed usage information about a command.
32"""
33
34 def _PrintAllCommands(self):
Sarah Owenscecd1d82012-11-01 22:59:27 -070035 print('usage: repo COMMAND [ARGS]')
36 print('The complete list of recognized repo commands are:')
Chirayu Desai217ea7d2013-03-01 19:14:38 +053037 commandNames = list(sorted(self.commands))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070038
39 maxlen = 0
40 for name in commandNames:
41 maxlen = max(maxlen, len(name))
42 fmt = ' %%-%ds %%s' % maxlen
43
44 for name in commandNames:
45 command = self.commands[name]
46 try:
47 summary = command.helpSummary.strip()
48 except AttributeError:
49 summary = ''
Sarah Owenscecd1d82012-11-01 22:59:27 -070050 print(fmt % (name, summary))
David Pursehouse2f9e7e42013-03-05 17:26:46 +090051 print("See 'repo help <command>' for more information on a "
Sarah Owenscecd1d82012-11-01 22:59:27 -070052 'specific command.')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070053
54 def _PrintCommonCommands(self):
Sarah Owenscecd1d82012-11-01 22:59:27 -070055 print('usage: repo COMMAND [ARGS]')
56 print('The most commonly used repo commands are:')
Chirayu Desai217ea7d2013-03-01 19:14:38 +053057 commandNames = list(sorted([name
58 for name, command in self.commands.items()
59 if command.common]))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070060
61 maxlen = 0
62 for name in commandNames:
63 maxlen = max(maxlen, len(name))
64 fmt = ' %%-%ds %%s' % maxlen
65
66 for name in commandNames:
67 command = self.commands[name]
68 try:
69 summary = command.helpSummary.strip()
70 except AttributeError:
71 summary = ''
Sarah Owenscecd1d82012-11-01 22:59:27 -070072 print(fmt % (name, summary))
73 print(
74"See 'repo help <command>' for more information on a specific command.\n"
75"See 'repo help --all' for a complete list of recognized commands.")
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070076
77 def _PrintCommandHelp(self, cmd):
78 class _Out(Coloring):
79 def __init__(self, gc):
80 Coloring.__init__(self, gc, 'help')
81 self.heading = self.printer('heading', attr='bold')
82
83 self.wrap = AbstractFormatter(DumbWriter())
84
85 def _PrintSection(self, heading, bodyAttr):
86 try:
87 body = getattr(cmd, bodyAttr)
88 except AttributeError:
89 return
Shawn O. Pearcec7c57e32009-06-03 17:43:16 -070090 if body == '' or body is None:
91 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070092
93 self.nl()
94
95 self.heading('%s', heading)
96 self.nl()
97
98 self.heading('%s', ''.ljust(len(heading), '-'))
99 self.nl()
100
101 me = 'repo %s' % cmd.NAME
102 body = body.strip()
103 body = body.replace('%prog', me)
104
Shawn O. Pearce938d6082009-04-21 08:01:17 -0700105 asciidoc_hdr = re.compile(r'^\n?([^\n]{1,})\n([=~-]{2,})$')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700106 for para in body.split("\n\n"):
107 if para.startswith(' '):
108 self.write('%s', para)
109 self.nl()
110 self.nl()
Shawn O. Pearce43c3d9e2009-03-04 14:26:50 -0800111 continue
112
113 m = asciidoc_hdr.match(para)
114 if m:
Shawn O. Pearce938d6082009-04-21 08:01:17 -0700115 title = m.group(1)
David Pursehouse8a68ff92012-09-24 12:15:13 +0900116 section_type = m.group(2)
117 if section_type[0] in ('=', '-'):
Shawn O. Pearce938d6082009-04-21 08:01:17 -0700118 p = self.heading
119 else:
120 def _p(fmt, *args):
121 self.write(' ')
122 self.heading(fmt, *args)
123 p = _p
124
125 p('%s', title)
Shawn O. Pearce43c3d9e2009-03-04 14:26:50 -0800126 self.nl()
David Pursehouse8f62fb72012-11-14 12:09:38 +0900127 p('%s', ''.ljust(len(title), section_type[0]))
Shawn O. Pearce43c3d9e2009-03-04 14:26:50 -0800128 self.nl()
129 continue
130
131 self.wrap.add_flowing_data(para)
132 self.wrap.end_paragraph(1)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700133 self.wrap.end_paragraph(0)
134
135 out = _Out(self.manifest.globalConfig)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700136 out._PrintSection('Summary', 'helpSummary')
Shawn O. Pearce5da554f2009-04-18 11:44:00 -0700137 cmd.OptionParser.print_help()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700138 out._PrintSection('Description', 'helpDescription')
139
140 def _Options(self, p):
141 p.add_option('-a', '--all',
142 dest='show_all', action='store_true',
143 help='show the complete list of commands')
144
145 def Execute(self, opt, args):
146 if len(args) == 0:
147 if opt.show_all:
148 self._PrintAllCommands()
149 else:
150 self._PrintCommonCommands()
151
152 elif len(args) == 1:
153 name = args[0]
154
155 try:
156 cmd = self.commands[name]
157 except KeyError:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700158 print("repo: '%s' is not a repo command." % name, file=sys.stderr)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700159 sys.exit(1)
160
Shawn O. Pearce752371d2011-10-11 15:23:37 -0700161 cmd.manifest = self.manifest
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700162 self._PrintCommandHelp(cmd)
163
164 else:
165 self._PrintCommandHelp(self)