blob: 9bb4c8c7dded8d7a21c1c08ba6949227bdaaa6d2 [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 Willemsen79360642015-08-31 15:45:06 -070022from command import PagedCommand, MirrorSafeCommand, GitcAvailableCommand, GitcClientCommand
Dan Willemsen9ff2ece2015-08-31 15:45:06 -070023import 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):
Dan Willemsen79360642015-08-31 15:45:06 -070060 if not isinstance(cmd, GitcAvailableCommand) and not isinstance(cmd, GitcClientCommand):
61 return True
62 if self.manifest.isGitcClient:
Dan Willemsen9ff2ece2015-08-31 15:45:06 -070063 return True
Dan Willemsen79360642015-08-31 15:45:06 -070064 if isinstance(cmd, GitcClientCommand):
65 return False
Dan Willemsen9ff2ece2015-08-31 15:45:06 -070066 if gitc_utils.get_gitc_manifest_dir():
67 return True
68 return False
69
Chirayu Desai217ea7d2013-03-01 19:14:38 +053070 commandNames = list(sorted([name
71 for name, command in self.commands.items()
Dan Willemsen9ff2ece2015-08-31 15:45:06 -070072 if command.common and gitc_supported(command)]))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070073
74 maxlen = 0
75 for name in commandNames:
76 maxlen = max(maxlen, len(name))
77 fmt = ' %%-%ds %%s' % maxlen
78
79 for name in commandNames:
80 command = self.commands[name]
81 try:
82 summary = command.helpSummary.strip()
83 except AttributeError:
84 summary = ''
Sarah Owenscecd1d82012-11-01 22:59:27 -070085 print(fmt % (name, summary))
86 print(
87"See 'repo help <command>' for more information on a specific command.\n"
88"See 'repo help --all' for a complete list of recognized commands.")
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070089
90 def _PrintCommandHelp(self, cmd):
91 class _Out(Coloring):
92 def __init__(self, gc):
93 Coloring.__init__(self, gc, 'help')
94 self.heading = self.printer('heading', attr='bold')
95
96 self.wrap = AbstractFormatter(DumbWriter())
97
98 def _PrintSection(self, heading, bodyAttr):
99 try:
100 body = getattr(cmd, bodyAttr)
101 except AttributeError:
102 return
Shawn O. Pearcec7c57e32009-06-03 17:43:16 -0700103 if body == '' or body is None:
104 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700105
106 self.nl()
107
108 self.heading('%s', heading)
109 self.nl()
110
111 self.heading('%s', ''.ljust(len(heading), '-'))
112 self.nl()
113
114 me = 'repo %s' % cmd.NAME
115 body = body.strip()
116 body = body.replace('%prog', me)
117
Shawn O. Pearce938d6082009-04-21 08:01:17 -0700118 asciidoc_hdr = re.compile(r'^\n?([^\n]{1,})\n([=~-]{2,})$')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700119 for para in body.split("\n\n"):
120 if para.startswith(' '):
121 self.write('%s', para)
122 self.nl()
123 self.nl()
Shawn O. Pearce43c3d9e2009-03-04 14:26:50 -0800124 continue
125
126 m = asciidoc_hdr.match(para)
127 if m:
Shawn O. Pearce938d6082009-04-21 08:01:17 -0700128 title = m.group(1)
David Pursehouse8a68ff92012-09-24 12:15:13 +0900129 section_type = m.group(2)
130 if section_type[0] in ('=', '-'):
Shawn O. Pearce938d6082009-04-21 08:01:17 -0700131 p = self.heading
132 else:
133 def _p(fmt, *args):
134 self.write(' ')
135 self.heading(fmt, *args)
136 p = _p
137
138 p('%s', title)
Shawn O. Pearce43c3d9e2009-03-04 14:26:50 -0800139 self.nl()
David Pursehouse8f62fb72012-11-14 12:09:38 +0900140 p('%s', ''.ljust(len(title), section_type[0]))
Shawn O. Pearce43c3d9e2009-03-04 14:26:50 -0800141 self.nl()
142 continue
143
144 self.wrap.add_flowing_data(para)
145 self.wrap.end_paragraph(1)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700146 self.wrap.end_paragraph(0)
147
148 out = _Out(self.manifest.globalConfig)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700149 out._PrintSection('Summary', 'helpSummary')
Shawn O. Pearce5da554f2009-04-18 11:44:00 -0700150 cmd.OptionParser.print_help()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700151 out._PrintSection('Description', 'helpDescription')
152
153 def _Options(self, p):
154 p.add_option('-a', '--all',
155 dest='show_all', action='store_true',
156 help='show the complete list of commands')
157
158 def Execute(self, opt, args):
159 if len(args) == 0:
160 if opt.show_all:
161 self._PrintAllCommands()
162 else:
163 self._PrintCommonCommands()
164
165 elif len(args) == 1:
166 name = args[0]
167
168 try:
169 cmd = self.commands[name]
170 except KeyError:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700171 print("repo: '%s' is not a repo command." % name, file=sys.stderr)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700172 sys.exit(1)
173
Shawn O. Pearce752371d2011-10-11 15:23:37 -0700174 cmd.manifest = self.manifest
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700175 self._PrintCommandHelp(cmd)
176
177 else:
178 self._PrintCommandHelp(self)