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 | 43c3d9e | 2009-03-04 14:26:50 -0800 | [diff] [blame] | 110 | asciidoc_hdr = re.compile(r'^\n?([^\n]{1,})\n(={2,}|-{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: |
| 120 | self.heading('%s', m.group(1)) |
| 121 | self.nl() |
| 122 | self.heading('%s', ''.ljust(len(m.group(1)),'-')) |
| 123 | self.nl() |
| 124 | continue |
| 125 | |
| 126 | self.wrap.add_flowing_data(para) |
| 127 | self.wrap.end_paragraph(1) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 128 | self.wrap.end_paragraph(0) |
| 129 | |
| 130 | out = _Out(self.manifest.globalConfig) |
| 131 | cmd.OptionParser.print_help() |
| 132 | out._PrintSection('Summary', 'helpSummary') |
| 133 | out._PrintSection('Description', 'helpDescription') |
| 134 | |
| 135 | def _Options(self, p): |
| 136 | p.add_option('-a', '--all', |
| 137 | dest='show_all', action='store_true', |
| 138 | help='show the complete list of commands') |
| 139 | |
| 140 | def Execute(self, opt, args): |
| 141 | if len(args) == 0: |
| 142 | if opt.show_all: |
| 143 | self._PrintAllCommands() |
| 144 | else: |
| 145 | self._PrintCommonCommands() |
| 146 | |
| 147 | elif len(args) == 1: |
| 148 | name = args[0] |
| 149 | |
| 150 | try: |
| 151 | cmd = self.commands[name] |
| 152 | except KeyError: |
| 153 | print >>sys.stderr, "repo: '%s' is not a repo command." % name |
| 154 | sys.exit(1) |
| 155 | |
| 156 | self._PrintCommandHelp(cmd) |
| 157 | |
| 158 | else: |
| 159 | self._PrintCommandHelp(self) |