The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1 | # 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. Pearce | 43c3d9e | 2009-03-04 14:26:50 -0800 | [diff] [blame] | 15 | import re |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 16 | import sys |
Mike Frysinger | 0888a08 | 2021-04-13 20:22:01 -0400 | [diff] [blame] | 17 | import textwrap |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 18 | |
| 19 | from color import Coloring |
Mike Frysinger | 6447733 | 2023-08-21 21:20:32 -0400 | [diff] [blame] | 20 | from command import MirrorSafeCommand |
| 21 | from command import PagedCommand |
Jason Chang | 1a3612f | 2023-08-08 14:12:53 -0700 | [diff] [blame] | 22 | from error import RepoExitError |
Mike Frysinger | 6447733 | 2023-08-21 21:20:32 -0400 | [diff] [blame] | 23 | from subcmds import all_commands |
| 24 | from wrapper import Wrapper |
Jason Chang | 1a3612f | 2023-08-08 14:12:53 -0700 | [diff] [blame] | 25 | |
| 26 | |
| 27 | class InvalidHelpCommand(RepoExitError): |
| 28 | """Invalid command passed into help.""" |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 29 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 30 | |
Shawn O. Pearce | c95583b | 2009-03-03 17:47:06 -0800 | [diff] [blame] | 31 | class Help(PagedCommand, MirrorSafeCommand): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 32 | COMMON = False |
| 33 | helpSummary = "Display detailed help on a command" |
| 34 | helpUsage = """ |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 35 | %prog [--all|command] |
| 36 | """ |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 37 | helpDescription = """ |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 38 | Displays detailed usage information about a command. |
| 39 | """ |
| 40 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 41 | 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 Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 47 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 48 | 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 Frysinger | 0b304c0 | 2019-12-02 16:49:13 -0500 | [diff] [blame] | 55 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 56 | def _PrintAllCommands(self): |
| 57 | print("usage: repo COMMAND [ARGS]") |
| 58 | self.PrintAllCommandsBody() |
Mike Frysinger | 56345c3 | 2021-07-26 23:46:32 -0400 | [diff] [blame] | 59 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 60 | 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 Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 69 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 70 | def _PrintCommonCommands(self): |
| 71 | print("usage: repo COMMAND [ARGS]") |
| 72 | self.PrintCommonCommandsBody() |
Mike Frysinger | 56345c3 | 2021-07-26 23:46:32 -0400 | [diff] [blame] | 73 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 74 | def PrintCommonCommandsBody(self): |
| 75 | print("The most commonly used repo commands are:") |
Dan Willemsen | 9ff2ece | 2015-08-31 15:45:06 -0700 | [diff] [blame] | 76 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 77 | commandNames = list( |
| 78 | sorted( |
Jason Chang | 8914b1f | 2023-05-26 12:44:50 -0700 | [diff] [blame] | 79 | name for name, command in all_commands.items() if command.COMMON |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 80 | ) |
| 81 | ) |
| 82 | self._PrintCommands(commandNames) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 83 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 84 | 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 Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 90 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 91 | 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 Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 97 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 98 | 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 Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 105 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 106 | if not self._first: |
| 107 | self.nl() |
| 108 | self._first = False |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 109 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 110 | self.heading("%s%s", header_prefix, heading) |
| 111 | self.nl() |
| 112 | self.nl() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 113 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 114 | me = "repo %s" % cmd.NAME |
| 115 | body = body.strip() |
| 116 | body = body.replace("%prog", me) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 117 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 118 | # 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. Pearce | 43c3d9e | 2009-03-04 14:26:50 -0800 | [diff] [blame] | 126 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 127 | 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. Pearce | 43c3d9e | 2009-03-04 14:26:50 -0800 | [diff] [blame] | 133 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 134 | 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 Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 144 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 145 | out = _Out(self.client.globalConfig) |
| 146 | out._PrintSection("Summary", "helpSummary") |
| 147 | cmd.OptionParser.print_help() |
| 148 | out._PrintSection("Description", "helpDescription") |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 149 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 150 | def _PrintAllCommandHelp(self): |
| 151 | for name in sorted(all_commands): |
| 152 | cmd = all_commands[name](manifest=self.manifest) |
Jason R. Coombs | b32ccbb | 2023-09-29 11:04:49 -0400 | [diff] [blame] | 153 | self._PrintCommandHelp(cmd, header_prefix=f"[{name}] ") |
Mike Frysinger | 898f4e6 | 2019-07-31 18:17:44 -0400 | [diff] [blame] | 154 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 155 | 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 Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 169 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 170 | 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 Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 178 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 179 | elif len(args) == 1: |
| 180 | name = args[0] |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 181 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 182 | 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 Chang | 1a3612f | 2023-08-08 14:12:53 -0700 | [diff] [blame] | 188 | raise InvalidHelpCommand(name) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 189 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 190 | self._PrintCommandHelp(cmd) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 191 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 192 | else: |
| 193 | self._PrintCommandHelp(self) |