blob: 57fb3cc288d1755a5fe185f9059948e871d005ed [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:')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070037 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 = ''
Sarah Owenscecd1d82012-11-01 22:59:27 -070051 print(fmt % (name, summary))
52 print("See 'repo help <command>' for more information on a"
53 'specific command.')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070054
55 def _PrintCommonCommands(self):
Sarah Owenscecd1d82012-11-01 22:59:27 -070056 print('usage: repo COMMAND [ARGS]')
57 print('The most commonly used repo commands are:')
58 commandNames = [name
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070059 for name in self.commands.keys()
60 if self.commands[name].common]
61 commandNames.sort()
62
63 maxlen = 0
64 for name in commandNames:
65 maxlen = max(maxlen, len(name))
66 fmt = ' %%-%ds %%s' % maxlen
67
68 for name in commandNames:
69 command = self.commands[name]
70 try:
71 summary = command.helpSummary.strip()
72 except AttributeError:
73 summary = ''
Sarah Owenscecd1d82012-11-01 22:59:27 -070074 print(fmt % (name, summary))
75 print(
76"See 'repo help <command>' for more information on a specific command.\n"
77"See 'repo help --all' for a complete list of recognized commands.")
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070078
79 def _PrintCommandHelp(self, cmd):
80 class _Out(Coloring):
81 def __init__(self, gc):
82 Coloring.__init__(self, gc, 'help')
83 self.heading = self.printer('heading', attr='bold')
84
85 self.wrap = AbstractFormatter(DumbWriter())
86
87 def _PrintSection(self, heading, bodyAttr):
88 try:
89 body = getattr(cmd, bodyAttr)
90 except AttributeError:
91 return
Shawn O. Pearcec7c57e32009-06-03 17:43:16 -070092 if body == '' or body is None:
93 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070094
95 self.nl()
96
97 self.heading('%s', heading)
98 self.nl()
99
100 self.heading('%s', ''.ljust(len(heading), '-'))
101 self.nl()
102
103 me = 'repo %s' % cmd.NAME
104 body = body.strip()
105 body = body.replace('%prog', me)
106
Shawn O. Pearce938d6082009-04-21 08:01:17 -0700107 asciidoc_hdr = re.compile(r'^\n?([^\n]{1,})\n([=~-]{2,})$')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700108 for para in body.split("\n\n"):
109 if para.startswith(' '):
110 self.write('%s', para)
111 self.nl()
112 self.nl()
Shawn O. Pearce43c3d9e2009-03-04 14:26:50 -0800113 continue
114
115 m = asciidoc_hdr.match(para)
116 if m:
Shawn O. Pearce938d6082009-04-21 08:01:17 -0700117 title = m.group(1)
David Pursehouse8a68ff92012-09-24 12:15:13 +0900118 section_type = m.group(2)
119 if section_type[0] in ('=', '-'):
Shawn O. Pearce938d6082009-04-21 08:01:17 -0700120 p = self.heading
121 else:
122 def _p(fmt, *args):
123 self.write(' ')
124 self.heading(fmt, *args)
125 p = _p
126
127 p('%s', title)
Shawn O. Pearce43c3d9e2009-03-04 14:26:50 -0800128 self.nl()
David Pursehouse8a68ff92012-09-24 12:15:13 +0900129 p('%s', ''.ljust(len(title),section_type[0]))
Shawn O. Pearce43c3d9e2009-03-04 14:26:50 -0800130 self.nl()
131 continue
132
133 self.wrap.add_flowing_data(para)
134 self.wrap.end_paragraph(1)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700135 self.wrap.end_paragraph(0)
136
137 out = _Out(self.manifest.globalConfig)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700138 out._PrintSection('Summary', 'helpSummary')
Shawn O. Pearce5da554f2009-04-18 11:44:00 -0700139 cmd.OptionParser.print_help()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700140 out._PrintSection('Description', 'helpDescription')
141
142 def _Options(self, p):
143 p.add_option('-a', '--all',
144 dest='show_all', action='store_true',
145 help='show the complete list of commands')
146
147 def Execute(self, opt, args):
148 if len(args) == 0:
149 if opt.show_all:
150 self._PrintAllCommands()
151 else:
152 self._PrintCommonCommands()
153
154 elif len(args) == 1:
155 name = args[0]
156
157 try:
158 cmd = self.commands[name]
159 except KeyError:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700160 print("repo: '%s' is not a repo command." % name, file=sys.stderr)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700161 sys.exit(1)
162
Shawn O. Pearce752371d2011-10-11 15:23:37 -0700163 cmd.manifest = self.manifest
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700164 self._PrintCommandHelp(cmd)
165
166 else:
167 self._PrintCommandHelp(self)