blob: 8004071140364399637181e3c383204669148020 [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
19from color import Coloring
Mike Frysinger64477332023-08-21 21:20:32 -040020from command import MirrorSafeCommand
21from command import PagedCommand
Jason Chang1a3612f2023-08-08 14:12:53 -070022from error import RepoExitError
Mike Frysinger64477332023-08-21 21:20:32 -040023from subcmds import all_commands
24from wrapper import Wrapper
Jason Chang1a3612f2023-08-08 14:12:53 -070025
26
27class InvalidHelpCommand(RepoExitError):
28 """Invalid command passed into help."""
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070029
David Pursehouse819827a2020-02-12 15:20:19 +090030
Shawn O. Pearcec95583b2009-03-03 17:47:06 -080031class Help(PagedCommand, MirrorSafeCommand):
Gavin Makea2e3302023-03-11 06:46:20 +000032 COMMON = False
33 helpSummary = "Display detailed help on a command"
34 helpUsage = """
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070035%prog [--all|command]
36"""
Gavin Makea2e3302023-03-11 06:46:20 +000037 helpDescription = """
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070038Displays detailed usage information about a command.
39"""
40
Gavin Makea2e3302023-03-11 06:46:20 +000041 def _PrintCommands(self, commandNames):
42 """Helper to display |commandNames| summaries."""
43 maxlen = 0
44 for name in commandNames:
45 maxlen = max(maxlen, len(name))
46 fmt = " %%-%ds %%s" % maxlen
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070047
Gavin Makea2e3302023-03-11 06:46:20 +000048 for name in commandNames:
49 command = all_commands[name]()
50 try:
51 summary = command.helpSummary.strip()
52 except AttributeError:
53 summary = ""
54 print(fmt % (name, summary))
Mike Frysinger0b304c02019-12-02 16:49:13 -050055
Gavin Makea2e3302023-03-11 06:46:20 +000056 def _PrintAllCommands(self):
57 print("usage: repo COMMAND [ARGS]")
58 self.PrintAllCommandsBody()
Mike Frysinger56345c32021-07-26 23:46:32 -040059
Gavin Makea2e3302023-03-11 06:46:20 +000060 def PrintAllCommandsBody(self):
61 print("The complete list of recognized repo commands is:")
62 commandNames = list(sorted(all_commands))
63 self._PrintCommands(commandNames)
64 print(
65 "See 'repo help <command>' for more information on a "
66 "specific command."
67 )
68 print("Bug reports:", Wrapper().BUG_URL)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070069
Gavin Makea2e3302023-03-11 06:46:20 +000070 def _PrintCommonCommands(self):
71 print("usage: repo COMMAND [ARGS]")
72 self.PrintCommonCommandsBody()
Mike Frysinger56345c32021-07-26 23:46:32 -040073
Gavin Makea2e3302023-03-11 06:46:20 +000074 def PrintCommonCommandsBody(self):
75 print("The most commonly used repo commands are:")
Dan Willemsen9ff2ece2015-08-31 15:45:06 -070076
Gavin Makea2e3302023-03-11 06:46:20 +000077 commandNames = list(
78 sorted(
Jason Chang8914b1f2023-05-26 12:44:50 -070079 name for name, command in all_commands.items() if command.COMMON
Gavin Makea2e3302023-03-11 06:46:20 +000080 )
81 )
82 self._PrintCommands(commandNames)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070083
Gavin Makea2e3302023-03-11 06:46:20 +000084 print(
85 "See 'repo help <command>' for more information on a specific "
86 "command.\nSee 'repo help --all' for a complete list of recognized "
87 "commands."
88 )
89 print("Bug reports:", Wrapper().BUG_URL)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070090
Gavin Makea2e3302023-03-11 06:46:20 +000091 def _PrintCommandHelp(self, cmd, header_prefix=""):
92 class _Out(Coloring):
93 def __init__(self, gc):
94 Coloring.__init__(self, gc, "help")
95 self.heading = self.printer("heading", attr="bold")
96 self._first = True
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070097
Gavin Makea2e3302023-03-11 06:46:20 +000098 def _PrintSection(self, heading, bodyAttr):
99 try:
100 body = getattr(cmd, bodyAttr)
101 except AttributeError:
102 return
103 if body == "" or body is None:
104 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700105
Gavin Makea2e3302023-03-11 06:46:20 +0000106 if not self._first:
107 self.nl()
108 self._first = False
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700109
Gavin Makea2e3302023-03-11 06:46:20 +0000110 self.heading("%s%s", header_prefix, heading)
111 self.nl()
112 self.nl()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700113
Gavin Makea2e3302023-03-11 06:46:20 +0000114 me = "repo %s" % cmd.NAME
115 body = body.strip()
116 body = body.replace("%prog", me)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700117
Gavin Makea2e3302023-03-11 06:46:20 +0000118 # Extract the title, but skip any trailing {#anchors}.
119 asciidoc_hdr = re.compile(r"^\n?#+ ([^{]+)(\{#.+\})?$")
120 for para in body.split("\n\n"):
121 if para.startswith(" "):
122 self.write("%s", para)
123 self.nl()
124 self.nl()
125 continue
Shawn O. Pearce43c3d9e2009-03-04 14:26:50 -0800126
Gavin Makea2e3302023-03-11 06:46:20 +0000127 m = asciidoc_hdr.match(para)
128 if m:
129 self.heading("%s%s", header_prefix, m.group(1))
130 self.nl()
131 self.nl()
132 continue
Shawn O. Pearce43c3d9e2009-03-04 14:26:50 -0800133
Gavin Makea2e3302023-03-11 06:46:20 +0000134 lines = textwrap.wrap(
135 para.replace(" ", " "),
136 width=80,
137 break_long_words=False,
138 break_on_hyphens=False,
139 )
140 for line in lines:
141 self.write("%s", line)
142 self.nl()
143 self.nl()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700144
Gavin Makea2e3302023-03-11 06:46:20 +0000145 out = _Out(self.client.globalConfig)
146 out._PrintSection("Summary", "helpSummary")
147 cmd.OptionParser.print_help()
148 out._PrintSection("Description", "helpDescription")
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700149
Gavin Makea2e3302023-03-11 06:46:20 +0000150 def _PrintAllCommandHelp(self):
151 for name in sorted(all_commands):
152 cmd = all_commands[name](manifest=self.manifest)
Jason R. Coombsb32ccbb2023-09-29 11:04:49 -0400153 self._PrintCommandHelp(cmd, header_prefix=f"[{name}] ")
Mike Frysinger898f4e62019-07-31 18:17:44 -0400154
Gavin Makea2e3302023-03-11 06:46:20 +0000155 def _Options(self, p):
156 p.add_option(
157 "-a",
158 "--all",
159 dest="show_all",
160 action="store_true",
161 help="show the complete list of commands",
162 )
163 p.add_option(
164 "--help-all",
165 dest="show_all_help",
166 action="store_true",
167 help="show the --help of all commands",
168 )
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700169
Gavin Makea2e3302023-03-11 06:46:20 +0000170 def Execute(self, opt, args):
171 if len(args) == 0:
172 if opt.show_all_help:
173 self._PrintAllCommandHelp()
174 elif opt.show_all:
175 self._PrintAllCommands()
176 else:
177 self._PrintCommonCommands()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700178
Gavin Makea2e3302023-03-11 06:46:20 +0000179 elif len(args) == 1:
180 name = args[0]
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700181
Gavin Makea2e3302023-03-11 06:46:20 +0000182 try:
183 cmd = all_commands[name](manifest=self.manifest)
184 except KeyError:
185 print(
186 "repo: '%s' is not a repo command." % name, file=sys.stderr
187 )
Jason Chang1a3612f2023-08-08 14:12:53 -0700188 raise InvalidHelpCommand(name)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700189
Gavin Makea2e3302023-03-11 06:46:20 +0000190 self._PrintCommandHelp(cmd)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700191
Gavin Makea2e3302023-03-11 06:46:20 +0000192 else:
193 self._PrintCommandHelp(self)