blob: 1ad391dbe5187cc28512c9822024dad4d4da4f14 [file] [log] [blame]
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001# Copyright (C) 2008 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
Shawn O. Pearce43c3d9e2009-03-04 14:26:50 -080015import re
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070016import sys
Mike Frysinger0888a082021-04-13 20:22:01 -040017import textwrap
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070018
Mike Frysingerd3639c52020-02-25 15:12:37 -050019from subcmds import all_commands
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070020from color import Coloring
Dan Willemsen79360642015-08-31 15:45:06 -070021from command import PagedCommand, MirrorSafeCommand, GitcAvailableCommand, GitcClientCommand
Dan Willemsen9ff2ece2015-08-31 15:45:06 -070022import gitc_utils
Mike Frysingera1cd7702021-04-20 23:38:04 -040023from wrapper import Wrapper
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070024
David Pursehouse819827a2020-02-12 15:20:19 +090025
Shawn O. Pearcec95583b2009-03-03 17:47:06 -080026class Help(PagedCommand, MirrorSafeCommand):
Mike Frysinger4f210542021-06-14 16:05:19 -040027 COMMON = False
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070028 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
Mike Frysinger0b304c02019-12-02 16:49:13 -050036 def _PrintCommands(self, commandNames):
37 """Helper to display |commandNames| summaries."""
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070038 maxlen = 0
39 for name in commandNames:
40 maxlen = max(maxlen, len(name))
41 fmt = ' %%-%ds %%s' % maxlen
42
43 for name in commandNames:
Mike Frysingerbb930462020-02-25 15:18:31 -050044 command = all_commands[name]()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070045 try:
46 summary = command.helpSummary.strip()
47 except AttributeError:
48 summary = ''
Sarah Owenscecd1d82012-11-01 22:59:27 -070049 print(fmt % (name, summary))
Mike Frysinger0b304c02019-12-02 16:49:13 -050050
51 def _PrintAllCommands(self):
52 print('usage: repo COMMAND [ARGS]')
Mike Frysinger56345c32021-07-26 23:46:32 -040053 self.PrintAllCommandsBody()
54
55 def PrintAllCommandsBody(self):
Mike Frysinger3001d6a2021-11-12 01:39:21 -050056 print('The complete list of recognized repo commands is:')
Mike Frysingerd3639c52020-02-25 15:12:37 -050057 commandNames = list(sorted(all_commands))
Mike Frysinger0b304c02019-12-02 16:49:13 -050058 self._PrintCommands(commandNames)
David Pursehouse2f9e7e42013-03-05 17:26:46 +090059 print("See 'repo help <command>' for more information on a "
Sarah Owenscecd1d82012-11-01 22:59:27 -070060 'specific command.')
Mike Frysinger56345c32021-07-26 23:46:32 -040061 print('Bug reports:', Wrapper().BUG_URL)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070062
63 def _PrintCommonCommands(self):
Sarah Owenscecd1d82012-11-01 22:59:27 -070064 print('usage: repo COMMAND [ARGS]')
Mike Frysinger56345c32021-07-26 23:46:32 -040065 self.PrintCommonCommandsBody()
66
67 def PrintCommonCommandsBody(self):
Sarah Owenscecd1d82012-11-01 22:59:27 -070068 print('The most commonly used repo commands are:')
Dan Willemsen9ff2ece2015-08-31 15:45:06 -070069
70 def gitc_supported(cmd):
Dan Willemsen79360642015-08-31 15:45:06 -070071 if not isinstance(cmd, GitcAvailableCommand) and not isinstance(cmd, GitcClientCommand):
Dan Willemsen9ff2ece2015-08-31 15:45:06 -070072 return True
Mike Frysinger8c1e9cb2020-09-06 14:53:18 -040073 if self.client.isGitcClient:
Dan Willemsen79360642015-08-31 15:45:06 -070074 return True
75 if isinstance(cmd, GitcClientCommand):
76 return False
Dan Willemsen9ff2ece2015-08-31 15:45:06 -070077 if gitc_utils.get_gitc_manifest_dir():
78 return True
79 return False
80
Chirayu Desai217ea7d2013-03-01 19:14:38 +053081 commandNames = list(sorted([name
Mike Frysingerd3639c52020-02-25 15:12:37 -050082 for name, command in all_commands.items()
Mike Frysinger4f210542021-06-14 16:05:19 -040083 if command.COMMON and gitc_supported(command)]))
Mike Frysinger0b304c02019-12-02 16:49:13 -050084 self._PrintCommands(commandNames)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070085
Sarah Owenscecd1d82012-11-01 22:59:27 -070086 print(
David Pursehouseabdf7502020-02-12 14:58:39 +090087 "See 'repo help <command>' for more information on a specific command.\n"
88 "See 'repo help --all' for a complete list of recognized commands.")
Mike Frysingera1cd7702021-04-20 23:38:04 -040089 print('Bug reports:', Wrapper().BUG_URL)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070090
Mike Frysinger898f4e62019-07-31 18:17:44 -040091 def _PrintCommandHelp(self, cmd, header_prefix=''):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070092 class _Out(Coloring):
93 def __init__(self, gc):
94 Coloring.__init__(self, gc, 'help')
95 self.heading = self.printer('heading', attr='bold')
Mike Frysinger0888a082021-04-13 20:22:01 -040096 self._first = True
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070097
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
Mike Frysinger0888a082021-04-13 20:22:01 -0400106 if not self._first:
107 self.nl()
108 self._first = False
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700109
Mike Frysinger898f4e62019-07-31 18:17:44 -0400110 self.heading('%s%s', header_prefix, heading)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700111 self.nl()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700112 self.nl()
113
114 me = 'repo %s' % cmd.NAME
115 body = body.strip()
116 body = body.replace('%prog', me)
117
Mike Frysinger0888a082021-04-13 20:22:01 -0400118 # Extract the title, but skip any trailing {#anchors}.
119 asciidoc_hdr = re.compile(r'^\n?#+ ([^{]+)(\{#.+\})?$')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700120 for para in body.split("\n\n"):
121 if para.startswith(' '):
122 self.write('%s', para)
123 self.nl()
124 self.nl()
Shawn O. Pearce43c3d9e2009-03-04 14:26:50 -0800125 continue
126
127 m = asciidoc_hdr.match(para)
128 if m:
Mike Frysinger898f4e62019-07-31 18:17:44 -0400129 self.heading('%s%s', header_prefix, m.group(1))
Shawn O. Pearce43c3d9e2009-03-04 14:26:50 -0800130 self.nl()
Shawn O. Pearce43c3d9e2009-03-04 14:26:50 -0800131 self.nl()
132 continue
133
Mike Frysinger0888a082021-04-13 20:22:01 -0400134 lines = textwrap.wrap(para.replace(' ', ' '), width=80,
135 break_long_words=False, break_on_hyphens=False)
136 for line in lines:
137 self.write('%s', line)
138 self.nl()
139 self.nl()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700140
Mike Frysinger8c1e9cb2020-09-06 14:53:18 -0400141 out = _Out(self.client.globalConfig)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700142 out._PrintSection('Summary', 'helpSummary')
Shawn O. Pearce5da554f2009-04-18 11:44:00 -0700143 cmd.OptionParser.print_help()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700144 out._PrintSection('Description', 'helpDescription')
145
Mike Frysinger898f4e62019-07-31 18:17:44 -0400146 def _PrintAllCommandHelp(self):
Mike Frysingerd3639c52020-02-25 15:12:37 -0500147 for name in sorted(all_commands):
Mike Frysingerd58d0dd2021-06-14 16:17:27 -0400148 cmd = all_commands[name](manifest=self.manifest)
Mike Frysinger898f4e62019-07-31 18:17:44 -0400149 self._PrintCommandHelp(cmd, header_prefix='[%s] ' % (name,))
150
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700151 def _Options(self, p):
152 p.add_option('-a', '--all',
153 dest='show_all', action='store_true',
154 help='show the complete list of commands')
Mike Frysinger898f4e62019-07-31 18:17:44 -0400155 p.add_option('--help-all',
156 dest='show_all_help', action='store_true',
157 help='show the --help of all commands')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700158
159 def Execute(self, opt, args):
160 if len(args) == 0:
Mike Frysinger898f4e62019-07-31 18:17:44 -0400161 if opt.show_all_help:
162 self._PrintAllCommandHelp()
163 elif opt.show_all:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700164 self._PrintAllCommands()
165 else:
166 self._PrintCommonCommands()
167
168 elif len(args) == 1:
169 name = args[0]
170
171 try:
Mike Frysingerd58d0dd2021-06-14 16:17:27 -0400172 cmd = all_commands[name](manifest=self.manifest)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700173 except KeyError:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700174 print("repo: '%s' is not a repo command." % name, file=sys.stderr)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700175 sys.exit(1)
176
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700177 self._PrintCommandHelp(cmd)
178
179 else:
180 self._PrintCommandHelp(self)