blob: 90c12443fc0d43e0e76b379de49b468699d61bf9 [file] [log] [blame]
Mike Frysingerf6013762019-06-13 02:30:51 -04001# -*- coding:utf-8 -*-
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002#
3# Copyright (C) 2008 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
Sarah Owenscecd1d82012-11-01 22:59:27 -070017from __future__ import print_function
Shawn O. Pearce43c3d9e2009-03-04 14:26:50 -080018import re
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070019import sys
20from formatter import AbstractFormatter, DumbWriter
21
22from color import Coloring
Dan Willemsen79360642015-08-31 15:45:06 -070023from command import PagedCommand, MirrorSafeCommand, GitcAvailableCommand, GitcClientCommand
Dan Willemsen9ff2ece2015-08-31 15:45:06 -070024import gitc_utils
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070025
Shawn O. Pearcec95583b2009-03-03 17:47:06 -080026class Help(PagedCommand, MirrorSafeCommand):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070027 common = False
28 helpSummary = "Display detailed help on a command"
29 helpUsage = """
30%prog [--all|command]
31"""
32 helpDescription = """
33Displays detailed usage information about a command.
34"""
35
36 def _PrintAllCommands(self):
Sarah Owenscecd1d82012-11-01 22:59:27 -070037 print('usage: repo COMMAND [ARGS]')
38 print('The complete list of recognized repo commands are:')
Chirayu Desai217ea7d2013-03-01 19:14:38 +053039 commandNames = list(sorted(self.commands))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070040
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 = ''
Sarah Owenscecd1d82012-11-01 22:59:27 -070052 print(fmt % (name, summary))
David Pursehouse2f9e7e42013-03-05 17:26:46 +090053 print("See 'repo help <command>' for more information on a "
Sarah Owenscecd1d82012-11-01 22:59:27 -070054 'specific command.')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070055
56 def _PrintCommonCommands(self):
Sarah Owenscecd1d82012-11-01 22:59:27 -070057 print('usage: repo COMMAND [ARGS]')
58 print('The most commonly used repo commands are:')
Dan Willemsen9ff2ece2015-08-31 15:45:06 -070059
60 def gitc_supported(cmd):
Dan Willemsen79360642015-08-31 15:45:06 -070061 if not isinstance(cmd, GitcAvailableCommand) and not isinstance(cmd, GitcClientCommand):
62 return True
63 if self.manifest.isGitcClient:
Dan Willemsen9ff2ece2015-08-31 15:45:06 -070064 return True
Dan Willemsen79360642015-08-31 15:45:06 -070065 if isinstance(cmd, GitcClientCommand):
66 return False
Dan Willemsen9ff2ece2015-08-31 15:45:06 -070067 if gitc_utils.get_gitc_manifest_dir():
68 return True
69 return False
70
Chirayu Desai217ea7d2013-03-01 19:14:38 +053071 commandNames = list(sorted([name
72 for name, command in self.commands.items()
Dan Willemsen9ff2ece2015-08-31 15:45:06 -070073 if command.common and gitc_supported(command)]))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070074
75 maxlen = 0
76 for name in commandNames:
77 maxlen = max(maxlen, len(name))
78 fmt = ' %%-%ds %%s' % maxlen
79
80 for name in commandNames:
81 command = self.commands[name]
82 try:
83 summary = command.helpSummary.strip()
84 except AttributeError:
85 summary = ''
Sarah Owenscecd1d82012-11-01 22:59:27 -070086 print(fmt % (name, summary))
87 print(
88"See 'repo help <command>' for more information on a specific command.\n"
89"See 'repo help --all' for a complete list of recognized commands.")
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070090
91 def _PrintCommandHelp(self, cmd):
92 class _Out(Coloring):
93 def __init__(self, gc):
94 Coloring.__init__(self, gc, 'help')
95 self.heading = self.printer('heading', attr='bold')
96
97 self.wrap = AbstractFormatter(DumbWriter())
98
99 def _PrintSection(self, heading, bodyAttr):
100 try:
101 body = getattr(cmd, bodyAttr)
102 except AttributeError:
103 return
Shawn O. Pearcec7c57e32009-06-03 17:43:16 -0700104 if body == '' or body is None:
105 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700106
107 self.nl()
108
109 self.heading('%s', heading)
110 self.nl()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700111 self.nl()
112
113 me = 'repo %s' % cmd.NAME
114 body = body.strip()
115 body = body.replace('%prog', me)
116
Mike Frysingerb8f7bb02018-10-10 01:05:11 -0400117 asciidoc_hdr = re.compile(r'^\n?#+ (.+)$')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700118 for para in body.split("\n\n"):
119 if para.startswith(' '):
120 self.write('%s', para)
121 self.nl()
122 self.nl()
Shawn O. Pearce43c3d9e2009-03-04 14:26:50 -0800123 continue
124
125 m = asciidoc_hdr.match(para)
126 if m:
Mike Frysingerb8f7bb02018-10-10 01:05:11 -0400127 self.heading(m.group(1))
Shawn O. Pearce43c3d9e2009-03-04 14:26:50 -0800128 self.nl()
Shawn O. Pearce43c3d9e2009-03-04 14:26:50 -0800129 self.nl()
130 continue
131
132 self.wrap.add_flowing_data(para)
133 self.wrap.end_paragraph(1)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700134 self.wrap.end_paragraph(0)
135
136 out = _Out(self.manifest.globalConfig)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700137 out._PrintSection('Summary', 'helpSummary')
Shawn O. Pearce5da554f2009-04-18 11:44:00 -0700138 cmd.OptionParser.print_help()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700139 out._PrintSection('Description', 'helpDescription')
140
141 def _Options(self, p):
142 p.add_option('-a', '--all',
143 dest='show_all', action='store_true',
144 help='show the complete list of commands')
145
146 def Execute(self, opt, args):
147 if len(args) == 0:
148 if opt.show_all:
149 self._PrintAllCommands()
150 else:
151 self._PrintCommonCommands()
152
153 elif len(args) == 1:
154 name = args[0]
155
156 try:
157 cmd = self.commands[name]
158 except KeyError:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700159 print("repo: '%s' is not a repo command." % name, file=sys.stderr)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700160 sys.exit(1)
161
Shawn O. Pearce752371d2011-10-11 15:23:37 -0700162 cmd.manifest = self.manifest
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700163 self._PrintCommandHelp(cmd)
164
165 else:
166 self._PrintCommandHelp(self)