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 |
| 97 | |
| 98 | self.nl() |
| 99 | |
| 100 | self.heading('%s', heading) |
| 101 | self.nl() |
| 102 | |
| 103 | self.heading('%s', ''.ljust(len(heading), '-')) |
| 104 | self.nl() |
| 105 | |
| 106 | me = 'repo %s' % cmd.NAME |
| 107 | body = body.strip() |
| 108 | body = body.replace('%prog', me) |
| 109 | |
Shawn O. Pearce | 938d608 | 2009-04-21 08:01:17 -0700 | [diff] [blame] | 110 | 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] | 111 | for para in body.split("\n\n"): |
| 112 | if para.startswith(' '): |
| 113 | self.write('%s', para) |
| 114 | self.nl() |
| 115 | self.nl() |
Shawn O. Pearce | 43c3d9e | 2009-03-04 14:26:50 -0800 | [diff] [blame] | 116 | continue |
| 117 | |
| 118 | m = asciidoc_hdr.match(para) |
| 119 | if m: |
Shawn O. Pearce | 938d608 | 2009-04-21 08:01:17 -0700 | [diff] [blame] | 120 | title = m.group(1) |
| 121 | type = m.group(2) |
| 122 | if type[0] in ('=', '-'): |
| 123 | p = self.heading |
| 124 | else: |
| 125 | def _p(fmt, *args): |
| 126 | self.write(' ') |
| 127 | self.heading(fmt, *args) |
| 128 | p = _p |
| 129 | |
| 130 | p('%s', title) |
Shawn O. Pearce | 43c3d9e | 2009-03-04 14:26:50 -0800 | [diff] [blame] | 131 | self.nl() |
Shawn O. Pearce | 938d608 | 2009-04-21 08:01:17 -0700 | [diff] [blame] | 132 | p('%s', ''.ljust(len(title),type[0])) |
Shawn O. Pearce | 43c3d9e | 2009-03-04 14:26:50 -0800 | [diff] [blame] | 133 | self.nl() |
| 134 | continue |
| 135 | |
| 136 | self.wrap.add_flowing_data(para) |
| 137 | self.wrap.end_paragraph(1) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 138 | self.wrap.end_paragraph(0) |
| 139 | |
| 140 | out = _Out(self.manifest.globalConfig) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 141 | out._PrintSection('Summary', 'helpSummary') |
Shawn O. Pearce | 5da554f | 2009-04-18 11:44:00 -0700 | [diff] [blame] | 142 | cmd.OptionParser.print_help() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 143 | out._PrintSection('Description', 'helpDescription') |
| 144 | |
| 145 | def _Options(self, p): |
| 146 | p.add_option('-a', '--all', |
| 147 | dest='show_all', action='store_true', |
| 148 | help='show the complete list of commands') |
| 149 | |
| 150 | def Execute(self, opt, args): |
| 151 | if len(args) == 0: |
| 152 | if opt.show_all: |
| 153 | self._PrintAllCommands() |
| 154 | else: |
| 155 | self._PrintCommonCommands() |
| 156 | |
| 157 | elif len(args) == 1: |
| 158 | name = args[0] |
| 159 | |
| 160 | try: |
| 161 | cmd = self.commands[name] |
| 162 | except KeyError: |
| 163 | print >>sys.stderr, "repo: '%s' is not a repo command." % name |
| 164 | sys.exit(1) |
| 165 | |
| 166 | self._PrintCommandHelp(cmd) |
| 167 | |
| 168 | else: |
| 169 | self._PrintCommandHelp(self) |