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