The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1 | # |
| 2 | # Copyright (C) 2008 The Android Open Source Project |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 16 | from __future__ import print_function |
Shawn O. Pearce | 43c3d9e | 2009-03-04 14:26:50 -0800 | [diff] [blame] | 17 | import re |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 18 | import sys |
| 19 | from formatter import AbstractFormatter, DumbWriter |
| 20 | |
| 21 | from color import Coloring |
Shawn O. Pearce | c95583b | 2009-03-03 17:47:06 -0800 | [diff] [blame] | 22 | from command import PagedCommand, MirrorSafeCommand |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 23 | |
Shawn O. Pearce | c95583b | 2009-03-03 17:47:06 -0800 | [diff] [blame] | 24 | class Help(PagedCommand, MirrorSafeCommand): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 25 | common = False |
| 26 | helpSummary = "Display detailed help on a command" |
| 27 | helpUsage = """ |
| 28 | %prog [--all|command] |
| 29 | """ |
| 30 | helpDescription = """ |
| 31 | Displays detailed usage information about a command. |
| 32 | """ |
| 33 | |
| 34 | def _PrintAllCommands(self): |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 35 | print('usage: repo COMMAND [ARGS]') |
| 36 | print('The complete list of recognized repo commands are:') |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 37 | commandNames = self.commands.keys() |
| 38 | commandNames.sort() |
| 39 | |
| 40 | maxlen = 0 |
| 41 | for name in commandNames: |
| 42 | maxlen = max(maxlen, len(name)) |
| 43 | fmt = ' %%-%ds %%s' % maxlen |
| 44 | |
| 45 | for name in commandNames: |
| 46 | command = self.commands[name] |
| 47 | try: |
| 48 | summary = command.helpSummary.strip() |
| 49 | except AttributeError: |
| 50 | summary = '' |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 51 | print(fmt % (name, summary)) |
| 52 | print("See 'repo help <command>' for more information on a" |
| 53 | 'specific command.') |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 54 | |
| 55 | def _PrintCommonCommands(self): |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 56 | print('usage: repo COMMAND [ARGS]') |
| 57 | print('The most commonly used repo commands are:') |
| 58 | commandNames = [name |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 59 | for name in self.commands.keys() |
| 60 | if self.commands[name].common] |
| 61 | commandNames.sort() |
| 62 | |
| 63 | maxlen = 0 |
| 64 | for name in commandNames: |
| 65 | maxlen = max(maxlen, len(name)) |
| 66 | fmt = ' %%-%ds %%s' % maxlen |
| 67 | |
| 68 | for name in commandNames: |
| 69 | command = self.commands[name] |
| 70 | try: |
| 71 | summary = command.helpSummary.strip() |
| 72 | except AttributeError: |
| 73 | summary = '' |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 74 | print(fmt % (name, summary)) |
| 75 | print( |
| 76 | "See 'repo help <command>' for more information on a specific command.\n" |
| 77 | "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] | 78 | |
| 79 | def _PrintCommandHelp(self, cmd): |
| 80 | class _Out(Coloring): |
| 81 | def __init__(self, gc): |
| 82 | Coloring.__init__(self, gc, 'help') |
| 83 | self.heading = self.printer('heading', attr='bold') |
| 84 | |
| 85 | self.wrap = AbstractFormatter(DumbWriter()) |
| 86 | |
| 87 | def _PrintSection(self, heading, bodyAttr): |
| 88 | try: |
| 89 | body = getattr(cmd, bodyAttr) |
| 90 | except AttributeError: |
| 91 | return |
Shawn O. Pearce | c7c57e3 | 2009-06-03 17:43:16 -0700 | [diff] [blame] | 92 | if body == '' or body is None: |
| 93 | return |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 94 | |
| 95 | self.nl() |
| 96 | |
| 97 | self.heading('%s', heading) |
| 98 | self.nl() |
| 99 | |
| 100 | self.heading('%s', ''.ljust(len(heading), '-')) |
| 101 | self.nl() |
| 102 | |
| 103 | me = 'repo %s' % cmd.NAME |
| 104 | body = body.strip() |
| 105 | body = body.replace('%prog', me) |
| 106 | |
Shawn O. Pearce | 938d608 | 2009-04-21 08:01:17 -0700 | [diff] [blame] | 107 | asciidoc_hdr = re.compile(r'^\n?([^\n]{1,})\n([=~-]{2,})$') |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 108 | for para in body.split("\n\n"): |
| 109 | if para.startswith(' '): |
| 110 | self.write('%s', para) |
| 111 | self.nl() |
| 112 | self.nl() |
Shawn O. Pearce | 43c3d9e | 2009-03-04 14:26:50 -0800 | [diff] [blame] | 113 | continue |
| 114 | |
| 115 | m = asciidoc_hdr.match(para) |
| 116 | if m: |
Shawn O. Pearce | 938d608 | 2009-04-21 08:01:17 -0700 | [diff] [blame] | 117 | title = m.group(1) |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 118 | section_type = m.group(2) |
| 119 | if section_type[0] in ('=', '-'): |
Shawn O. Pearce | 938d608 | 2009-04-21 08:01:17 -0700 | [diff] [blame] | 120 | p = self.heading |
| 121 | else: |
| 122 | def _p(fmt, *args): |
| 123 | self.write(' ') |
| 124 | self.heading(fmt, *args) |
| 125 | p = _p |
| 126 | |
| 127 | p('%s', title) |
Shawn O. Pearce | 43c3d9e | 2009-03-04 14:26:50 -0800 | [diff] [blame] | 128 | self.nl() |
David Pursehouse | 8f62fb7 | 2012-11-14 12:09:38 +0900 | [diff] [blame] | 129 | p('%s', ''.ljust(len(title), section_type[0])) |
Shawn O. Pearce | 43c3d9e | 2009-03-04 14:26:50 -0800 | [diff] [blame] | 130 | self.nl() |
| 131 | continue |
| 132 | |
| 133 | self.wrap.add_flowing_data(para) |
| 134 | self.wrap.end_paragraph(1) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 135 | self.wrap.end_paragraph(0) |
| 136 | |
| 137 | out = _Out(self.manifest.globalConfig) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 138 | out._PrintSection('Summary', 'helpSummary') |
Shawn O. Pearce | 5da554f | 2009-04-18 11:44:00 -0700 | [diff] [blame] | 139 | cmd.OptionParser.print_help() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 140 | out._PrintSection('Description', 'helpDescription') |
| 141 | |
| 142 | def _Options(self, p): |
| 143 | p.add_option('-a', '--all', |
| 144 | dest='show_all', action='store_true', |
| 145 | help='show the complete list of commands') |
| 146 | |
| 147 | def Execute(self, opt, args): |
| 148 | if len(args) == 0: |
| 149 | if opt.show_all: |
| 150 | self._PrintAllCommands() |
| 151 | else: |
| 152 | self._PrintCommonCommands() |
| 153 | |
| 154 | elif len(args) == 1: |
| 155 | name = args[0] |
| 156 | |
| 157 | try: |
| 158 | cmd = self.commands[name] |
| 159 | except KeyError: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 160 | 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] | 161 | sys.exit(1) |
| 162 | |
Shawn O. Pearce | 752371d | 2011-10-11 15:23:37 -0700 | [diff] [blame] | 163 | cmd.manifest = self.manifest |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 164 | self._PrintCommandHelp(cmd) |
| 165 | |
| 166 | else: |
| 167 | self._PrintCommandHelp(self) |