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 |
| 17 | from formatter import AbstractFormatter, DumbWriter |
| 18 | |
Mike Frysinger | d3639c5 | 2020-02-25 15:12:37 -0500 | [diff] [blame] | 19 | from subcmds import all_commands |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 20 | from color import Coloring |
Dan Willemsen | 7936064 | 2015-08-31 15:45:06 -0700 | [diff] [blame] | 21 | from command import PagedCommand, MirrorSafeCommand, GitcAvailableCommand, GitcClientCommand |
Dan Willemsen | 9ff2ece | 2015-08-31 15:45:06 -0700 | [diff] [blame] | 22 | import gitc_utils |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 23 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 24 | |
Shawn O. Pearce | c95583b | 2009-03-03 17:47:06 -0800 | [diff] [blame] | 25 | class Help(PagedCommand, MirrorSafeCommand): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 26 | common = False |
| 27 | helpSummary = "Display detailed help on a command" |
| 28 | helpUsage = """ |
| 29 | %prog [--all|command] |
| 30 | """ |
| 31 | helpDescription = """ |
| 32 | Displays detailed usage information about a command. |
| 33 | """ |
| 34 | |
Mike Frysinger | 0b304c0 | 2019-12-02 16:49:13 -0500 | [diff] [blame] | 35 | def _PrintCommands(self, commandNames): |
| 36 | """Helper to display |commandNames| summaries.""" |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 37 | maxlen = 0 |
| 38 | for name in commandNames: |
| 39 | maxlen = max(maxlen, len(name)) |
| 40 | fmt = ' %%-%ds %%s' % maxlen |
| 41 | |
| 42 | for name in commandNames: |
Mike Frysinger | bb93046 | 2020-02-25 15:18:31 -0500 | [diff] [blame] | 43 | command = all_commands[name]() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 44 | try: |
| 45 | summary = command.helpSummary.strip() |
| 46 | except AttributeError: |
| 47 | summary = '' |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 48 | print(fmt % (name, summary)) |
Mike Frysinger | 0b304c0 | 2019-12-02 16:49:13 -0500 | [diff] [blame] | 49 | |
| 50 | def _PrintAllCommands(self): |
| 51 | print('usage: repo COMMAND [ARGS]') |
| 52 | print('The complete list of recognized repo commands are:') |
Mike Frysinger | d3639c5 | 2020-02-25 15:12:37 -0500 | [diff] [blame] | 53 | commandNames = list(sorted(all_commands)) |
Mike Frysinger | 0b304c0 | 2019-12-02 16:49:13 -0500 | [diff] [blame] | 54 | self._PrintCommands(commandNames) |
David Pursehouse | 2f9e7e4 | 2013-03-05 17:26:46 +0900 | [diff] [blame] | 55 | print("See 'repo help <command>' for more information on a " |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 56 | 'specific command.') |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 57 | |
| 58 | def _PrintCommonCommands(self): |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 59 | print('usage: repo COMMAND [ARGS]') |
| 60 | print('The most commonly used repo commands are:') |
Dan Willemsen | 9ff2ece | 2015-08-31 15:45:06 -0700 | [diff] [blame] | 61 | |
| 62 | def gitc_supported(cmd): |
Dan Willemsen | 7936064 | 2015-08-31 15:45:06 -0700 | [diff] [blame] | 63 | if not isinstance(cmd, GitcAvailableCommand) and not isinstance(cmd, GitcClientCommand): |
Dan Willemsen | 9ff2ece | 2015-08-31 15:45:06 -0700 | [diff] [blame] | 64 | return True |
Mike Frysinger | 8c1e9cb | 2020-09-06 14:53:18 -0400 | [diff] [blame] | 65 | if self.client.isGitcClient: |
Dan Willemsen | 7936064 | 2015-08-31 15:45:06 -0700 | [diff] [blame] | 66 | return True |
| 67 | if isinstance(cmd, GitcClientCommand): |
| 68 | return False |
Dan Willemsen | 9ff2ece | 2015-08-31 15:45:06 -0700 | [diff] [blame] | 69 | if gitc_utils.get_gitc_manifest_dir(): |
| 70 | return True |
| 71 | return False |
| 72 | |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 73 | commandNames = list(sorted([name |
Mike Frysinger | d3639c5 | 2020-02-25 15:12:37 -0500 | [diff] [blame] | 74 | for name, command in all_commands.items() |
David Pursehouse | abdf750 | 2020-02-12 14:58:39 +0900 | [diff] [blame] | 75 | if command.common and gitc_supported(command)])) |
Mike Frysinger | 0b304c0 | 2019-12-02 16:49:13 -0500 | [diff] [blame] | 76 | self._PrintCommands(commandNames) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 77 | |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 78 | print( |
David Pursehouse | abdf750 | 2020-02-12 14:58:39 +0900 | [diff] [blame] | 79 | "See 'repo help <command>' for more information on a specific command.\n" |
| 80 | "See 'repo help --all' for a complete list of recognized commands.") |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 81 | |
Mike Frysinger | 898f4e6 | 2019-07-31 18:17:44 -0400 | [diff] [blame] | 82 | def _PrintCommandHelp(self, cmd, header_prefix=''): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 83 | class _Out(Coloring): |
| 84 | def __init__(self, gc): |
| 85 | Coloring.__init__(self, gc, 'help') |
| 86 | self.heading = self.printer('heading', attr='bold') |
| 87 | |
| 88 | self.wrap = AbstractFormatter(DumbWriter()) |
| 89 | |
| 90 | def _PrintSection(self, heading, bodyAttr): |
| 91 | try: |
| 92 | body = getattr(cmd, bodyAttr) |
| 93 | except AttributeError: |
| 94 | return |
Shawn O. Pearce | c7c57e3 | 2009-06-03 17:43:16 -0700 | [diff] [blame] | 95 | if body == '' or body is None: |
| 96 | return |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 97 | |
| 98 | self.nl() |
| 99 | |
Mike Frysinger | 898f4e6 | 2019-07-31 18:17:44 -0400 | [diff] [blame] | 100 | self.heading('%s%s', header_prefix, heading) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 101 | self.nl() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 102 | self.nl() |
| 103 | |
| 104 | me = 'repo %s' % cmd.NAME |
| 105 | body = body.strip() |
| 106 | body = body.replace('%prog', me) |
| 107 | |
Mike Frysinger | b8f7bb0 | 2018-10-10 01:05:11 -0400 | [diff] [blame] | 108 | asciidoc_hdr = re.compile(r'^\n?#+ (.+)$') |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 109 | for para in body.split("\n\n"): |
| 110 | if para.startswith(' '): |
| 111 | self.write('%s', para) |
| 112 | self.nl() |
| 113 | self.nl() |
Shawn O. Pearce | 43c3d9e | 2009-03-04 14:26:50 -0800 | [diff] [blame] | 114 | continue |
| 115 | |
| 116 | m = asciidoc_hdr.match(para) |
| 117 | if m: |
Mike Frysinger | 898f4e6 | 2019-07-31 18:17:44 -0400 | [diff] [blame] | 118 | self.heading('%s%s', header_prefix, m.group(1)) |
Shawn O. Pearce | 43c3d9e | 2009-03-04 14:26:50 -0800 | [diff] [blame] | 119 | self.nl() |
Shawn O. Pearce | 43c3d9e | 2009-03-04 14:26:50 -0800 | [diff] [blame] | 120 | self.nl() |
| 121 | continue |
| 122 | |
| 123 | self.wrap.add_flowing_data(para) |
| 124 | self.wrap.end_paragraph(1) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 125 | self.wrap.end_paragraph(0) |
| 126 | |
Mike Frysinger | 8c1e9cb | 2020-09-06 14:53:18 -0400 | [diff] [blame] | 127 | out = _Out(self.client.globalConfig) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 128 | out._PrintSection('Summary', 'helpSummary') |
Shawn O. Pearce | 5da554f | 2009-04-18 11:44:00 -0700 | [diff] [blame] | 129 | cmd.OptionParser.print_help() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 130 | out._PrintSection('Description', 'helpDescription') |
| 131 | |
Mike Frysinger | 898f4e6 | 2019-07-31 18:17:44 -0400 | [diff] [blame] | 132 | def _PrintAllCommandHelp(self): |
Mike Frysinger | d3639c5 | 2020-02-25 15:12:37 -0500 | [diff] [blame] | 133 | for name in sorted(all_commands): |
Mike Frysinger | bb93046 | 2020-02-25 15:18:31 -0500 | [diff] [blame] | 134 | cmd = all_commands[name]() |
Mike Frysinger | 898f4e6 | 2019-07-31 18:17:44 -0400 | [diff] [blame] | 135 | cmd.manifest = self.manifest |
| 136 | self._PrintCommandHelp(cmd, header_prefix='[%s] ' % (name,)) |
| 137 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 138 | def _Options(self, p): |
| 139 | p.add_option('-a', '--all', |
| 140 | dest='show_all', action='store_true', |
| 141 | help='show the complete list of commands') |
Mike Frysinger | 898f4e6 | 2019-07-31 18:17:44 -0400 | [diff] [blame] | 142 | p.add_option('--help-all', |
| 143 | dest='show_all_help', action='store_true', |
| 144 | help='show the --help of all commands') |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 145 | |
| 146 | def Execute(self, opt, args): |
| 147 | if len(args) == 0: |
Mike Frysinger | 898f4e6 | 2019-07-31 18:17:44 -0400 | [diff] [blame] | 148 | if opt.show_all_help: |
| 149 | self._PrintAllCommandHelp() |
| 150 | elif opt.show_all: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 151 | self._PrintAllCommands() |
| 152 | else: |
| 153 | self._PrintCommonCommands() |
| 154 | |
| 155 | elif len(args) == 1: |
| 156 | name = args[0] |
| 157 | |
| 158 | try: |
Mike Frysinger | bb93046 | 2020-02-25 15:18:31 -0500 | [diff] [blame] | 159 | cmd = all_commands[name]() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 160 | except KeyError: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 161 | print("repo: '%s' is not a repo command." % name, file=sys.stderr) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 162 | sys.exit(1) |
| 163 | |
Shawn O. Pearce | 752371d | 2011-10-11 15:23:37 -0700 | [diff] [blame] | 164 | cmd.manifest = self.manifest |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 165 | self._PrintCommandHelp(cmd) |
| 166 | |
| 167 | else: |
| 168 | self._PrintCommandHelp(self) |