blob: ae5b8f0821e3bb634e2fd4c0ad40ee8f2942b69c [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
Dan Willemsen9ff2ece2015-08-31 15:45:06 -070022from command import PagedCommand, MirrorSafeCommand, RequiresGitcCommand
23import gitc_utils
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070024
Shawn O. Pearcec95583b2009-03-03 17:47:06 -080025class Help(PagedCommand, MirrorSafeCommand):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070026 common = False
27 helpSummary = "Display detailed help on a command"
28 helpUsage = """
29%prog [--all|command]
30"""
31 helpDescription = """
32Displays detailed usage information about a command.
33"""
34
35 def _PrintAllCommands(self):
Sarah Owenscecd1d82012-11-01 22:59:27 -070036 print('usage: repo COMMAND [ARGS]')
37 print('The complete list of recognized repo commands are:')
Chirayu Desai217ea7d2013-03-01 19:14:38 +053038 commandNames = list(sorted(self.commands))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070039
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))
David Pursehouse2f9e7e42013-03-05 17:26:46 +090052 print("See 'repo help <command>' for more information on a "
Sarah Owenscecd1d82012-11-01 22:59:27 -070053 '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:')
Dan Willemsen9ff2ece2015-08-31 15:45:06 -070058
59 def gitc_supported(cmd):
60 if not isinstance(cmd, RequiresGitcCommand):
61 return True
62 if gitc_utils.get_gitc_manifest_dir():
63 return True
64 return False
65
Chirayu Desai217ea7d2013-03-01 19:14:38 +053066 commandNames = list(sorted([name
67 for name, command in self.commands.items()
Dan Willemsen9ff2ece2015-08-31 15:45:06 -070068 if command.common and gitc_supported(command)]))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070069
70 maxlen = 0
71 for name in commandNames:
72 maxlen = max(maxlen, len(name))
73 fmt = ' %%-%ds %%s' % maxlen
74
75 for name in commandNames:
76 command = self.commands[name]
77 try:
78 summary = command.helpSummary.strip()
79 except AttributeError:
80 summary = ''
Sarah Owenscecd1d82012-11-01 22:59:27 -070081 print(fmt % (name, summary))
82 print(
83"See 'repo help <command>' for more information on a specific command.\n"
84"See 'repo help --all' for a complete list of recognized commands.")
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070085
86 def _PrintCommandHelp(self, cmd):
87 class _Out(Coloring):
88 def __init__(self, gc):
89 Coloring.__init__(self, gc, 'help')
90 self.heading = self.printer('heading', attr='bold')
91
92 self.wrap = AbstractFormatter(DumbWriter())
93
94 def _PrintSection(self, heading, bodyAttr):
95 try:
96 body = getattr(cmd, bodyAttr)
97 except AttributeError:
98 return
Shawn O. Pearcec7c57e32009-06-03 17:43:16 -070099 if body == '' or body is None:
100 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700101
102 self.nl()
103
104 self.heading('%s', heading)
105 self.nl()
106
107 self.heading('%s', ''.ljust(len(heading), '-'))
108 self.nl()
109
110 me = 'repo %s' % cmd.NAME
111 body = body.strip()
112 body = body.replace('%prog', me)
113
Shawn O. Pearce938d6082009-04-21 08:01:17 -0700114 asciidoc_hdr = re.compile(r'^\n?([^\n]{1,})\n([=~-]{2,})$')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700115 for para in body.split("\n\n"):
116 if para.startswith(' '):
117 self.write('%s', para)
118 self.nl()
119 self.nl()
Shawn O. Pearce43c3d9e2009-03-04 14:26:50 -0800120 continue
121
122 m = asciidoc_hdr.match(para)
123 if m:
Shawn O. Pearce938d6082009-04-21 08:01:17 -0700124 title = m.group(1)
David Pursehouse8a68ff92012-09-24 12:15:13 +0900125 section_type = m.group(2)
126 if section_type[0] in ('=', '-'):
Shawn O. Pearce938d6082009-04-21 08:01:17 -0700127 p = self.heading
128 else:
129 def _p(fmt, *args):
130 self.write(' ')
131 self.heading(fmt, *args)
132 p = _p
133
134 p('%s', title)
Shawn O. Pearce43c3d9e2009-03-04 14:26:50 -0800135 self.nl()
David Pursehouse8f62fb72012-11-14 12:09:38 +0900136 p('%s', ''.ljust(len(title), section_type[0]))
Shawn O. Pearce43c3d9e2009-03-04 14:26:50 -0800137 self.nl()
138 continue
139
140 self.wrap.add_flowing_data(para)
141 self.wrap.end_paragraph(1)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700142 self.wrap.end_paragraph(0)
143
144 out = _Out(self.manifest.globalConfig)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700145 out._PrintSection('Summary', 'helpSummary')
Shawn O. Pearce5da554f2009-04-18 11:44:00 -0700146 cmd.OptionParser.print_help()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700147 out._PrintSection('Description', 'helpDescription')
148
149 def _Options(self, p):
150 p.add_option('-a', '--all',
151 dest='show_all', action='store_true',
152 help='show the complete list of commands')
153
154 def Execute(self, opt, args):
155 if len(args) == 0:
156 if opt.show_all:
157 self._PrintAllCommands()
158 else:
159 self._PrintCommonCommands()
160
161 elif len(args) == 1:
162 name = args[0]
163
164 try:
165 cmd = self.commands[name]
166 except KeyError:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700167 print("repo: '%s' is not a repo command." % name, file=sys.stderr)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700168 sys.exit(1)
169
Shawn O. Pearce752371d2011-10-11 15:23:37 -0700170 cmd.manifest = self.manifest
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700171 self._PrintCommandHelp(cmd)
172
173 else:
174 self._PrintCommandHelp(self)