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:') |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 37 | commandNames = list(sorted(self.commands)) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 38 | |
| 39 | maxlen = 0 |
| 40 | for name in commandNames: |
| 41 | maxlen = max(maxlen, len(name)) |
| 42 | fmt = ' %%-%ds %%s' % maxlen |
| 43 | |
| 44 | for name in commandNames: |
| 45 | command = self.commands[name] |
| 46 | try: |
| 47 | summary = command.helpSummary.strip() |
| 48 | except AttributeError: |
| 49 | summary = '' |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 50 | print(fmt % (name, summary)) |
David Pursehouse | 2f9e7e4 | 2013-03-05 17:26:46 +0900 | [diff] [blame] | 51 | print("See 'repo help <command>' for more information on a " |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 52 | 'specific command.') |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 53 | |
| 54 | def _PrintCommonCommands(self): |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 55 | print('usage: repo COMMAND [ARGS]') |
| 56 | print('The most commonly used repo commands are:') |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 57 | commandNames = list(sorted([name |
| 58 | for name, command in self.commands.items() |
| 59 | if command.common])) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 60 | |
| 61 | maxlen = 0 |
| 62 | for name in commandNames: |
| 63 | maxlen = max(maxlen, len(name)) |
| 64 | fmt = ' %%-%ds %%s' % maxlen |
| 65 | |
| 66 | for name in commandNames: |
| 67 | command = self.commands[name] |
| 68 | try: |
| 69 | summary = command.helpSummary.strip() |
| 70 | except AttributeError: |
| 71 | summary = '' |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 72 | print(fmt % (name, summary)) |
| 73 | print( |
| 74 | "See 'repo help <command>' for more information on a specific command.\n" |
| 75 | "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] | 76 | |
| 77 | def _PrintCommandHelp(self, cmd): |
| 78 | class _Out(Coloring): |
| 79 | def __init__(self, gc): |
| 80 | Coloring.__init__(self, gc, 'help') |
| 81 | self.heading = self.printer('heading', attr='bold') |
| 82 | |
| 83 | self.wrap = AbstractFormatter(DumbWriter()) |
| 84 | |
| 85 | def _PrintSection(self, heading, bodyAttr): |
| 86 | try: |
| 87 | body = getattr(cmd, bodyAttr) |
| 88 | except AttributeError: |
| 89 | return |
Shawn O. Pearce | c7c57e3 | 2009-06-03 17:43:16 -0700 | [diff] [blame] | 90 | if body == '' or body is None: |
| 91 | return |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 92 | |
| 93 | self.nl() |
| 94 | |
| 95 | self.heading('%s', heading) |
| 96 | self.nl() |
| 97 | |
| 98 | self.heading('%s', ''.ljust(len(heading), '-')) |
| 99 | self.nl() |
| 100 | |
| 101 | me = 'repo %s' % cmd.NAME |
| 102 | body = body.strip() |
| 103 | body = body.replace('%prog', me) |
| 104 | |
Shawn O. Pearce | 938d608 | 2009-04-21 08:01:17 -0700 | [diff] [blame] | 105 | 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] | 106 | for para in body.split("\n\n"): |
| 107 | if para.startswith(' '): |
| 108 | self.write('%s', para) |
| 109 | self.nl() |
| 110 | self.nl() |
Shawn O. Pearce | 43c3d9e | 2009-03-04 14:26:50 -0800 | [diff] [blame] | 111 | continue |
| 112 | |
| 113 | m = asciidoc_hdr.match(para) |
| 114 | if m: |
Shawn O. Pearce | 938d608 | 2009-04-21 08:01:17 -0700 | [diff] [blame] | 115 | title = m.group(1) |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 116 | section_type = m.group(2) |
| 117 | if section_type[0] in ('=', '-'): |
Shawn O. Pearce | 938d608 | 2009-04-21 08:01:17 -0700 | [diff] [blame] | 118 | p = self.heading |
| 119 | else: |
| 120 | def _p(fmt, *args): |
| 121 | self.write(' ') |
| 122 | self.heading(fmt, *args) |
| 123 | p = _p |
| 124 | |
| 125 | p('%s', title) |
Shawn O. Pearce | 43c3d9e | 2009-03-04 14:26:50 -0800 | [diff] [blame] | 126 | self.nl() |
David Pursehouse | 8f62fb7 | 2012-11-14 12:09:38 +0900 | [diff] [blame] | 127 | p('%s', ''.ljust(len(title), section_type[0])) |
Shawn O. Pearce | 43c3d9e | 2009-03-04 14:26:50 -0800 | [diff] [blame] | 128 | self.nl() |
| 129 | continue |
| 130 | |
| 131 | self.wrap.add_flowing_data(para) |
| 132 | self.wrap.end_paragraph(1) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 133 | self.wrap.end_paragraph(0) |
| 134 | |
| 135 | out = _Out(self.manifest.globalConfig) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 136 | out._PrintSection('Summary', 'helpSummary') |
Shawn O. Pearce | 5da554f | 2009-04-18 11:44:00 -0700 | [diff] [blame] | 137 | cmd.OptionParser.print_help() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 138 | out._PrintSection('Description', 'helpDescription') |
| 139 | |
| 140 | def _Options(self, p): |
| 141 | p.add_option('-a', '--all', |
| 142 | dest='show_all', action='store_true', |
| 143 | help='show the complete list of commands') |
| 144 | |
| 145 | def Execute(self, opt, args): |
| 146 | if len(args) == 0: |
| 147 | if opt.show_all: |
| 148 | self._PrintAllCommands() |
| 149 | else: |
| 150 | self._PrintCommonCommands() |
| 151 | |
| 152 | elif len(args) == 1: |
| 153 | name = args[0] |
| 154 | |
| 155 | try: |
| 156 | cmd = self.commands[name] |
| 157 | except KeyError: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 158 | 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] | 159 | sys.exit(1) |
| 160 | |
Shawn O. Pearce | 752371d | 2011-10-11 15:23:37 -0700 | [diff] [blame] | 161 | cmd.manifest = self.manifest |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 162 | self._PrintCommandHelp(cmd) |
| 163 | |
| 164 | else: |
| 165 | self._PrintCommandHelp(self) |