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 | |
| 16 | import sys |
| 17 | from formatter import AbstractFormatter, DumbWriter |
| 18 | |
| 19 | from color import Coloring |
| 20 | from command import PagedCommand |
| 21 | |
| 22 | class Help(PagedCommand): |
| 23 | common = False |
| 24 | helpSummary = "Display detailed help on a command" |
| 25 | helpUsage = """ |
| 26 | %prog [--all|command] |
| 27 | """ |
| 28 | helpDescription = """ |
| 29 | Displays detailed usage information about a command. |
| 30 | """ |
| 31 | |
| 32 | def _PrintAllCommands(self): |
| 33 | print 'usage: repo COMMAND [ARGS]' |
| 34 | print """ |
| 35 | The complete list of recognized repo commands are: |
| 36 | """ |
| 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 = '' |
| 51 | print fmt % (name, summary) |
| 52 | print """ |
| 53 | See 'repo help <command>' for more information on a specific command. |
| 54 | """ |
| 55 | |
| 56 | def _PrintCommonCommands(self): |
| 57 | print 'usage: repo COMMAND [ARGS]' |
| 58 | print """ |
| 59 | The most commonly used repo commands are: |
| 60 | """ |
| 61 | commandNames = [name |
| 62 | for name in self.commands.keys() |
| 63 | if self.commands[name].common] |
| 64 | commandNames.sort() |
| 65 | |
| 66 | maxlen = 0 |
| 67 | for name in commandNames: |
| 68 | maxlen = max(maxlen, len(name)) |
| 69 | fmt = ' %%-%ds %%s' % maxlen |
| 70 | |
| 71 | for name in commandNames: |
| 72 | command = self.commands[name] |
| 73 | try: |
| 74 | summary = command.helpSummary.strip() |
| 75 | except AttributeError: |
| 76 | summary = '' |
| 77 | print fmt % (name, summary) |
| 78 | print """ |
| 79 | See 'repo help <command>' for more information on a specific command. |
| 80 | """ |
| 81 | |
| 82 | def _PrintCommandHelp(self, cmd): |
| 83 | class _Out(Coloring): |
| 84 | def __init__(self, gc): |
| 85 | Coloring.__init__(self, gc, 'help') |
| 86 | self.heading = self.printer('heading', attr='bold') |
| 87 | |
| 88 | self.wrap = AbstractFormatter(DumbWriter()) |
| 89 | |
| 90 | def _PrintSection(self, heading, bodyAttr): |
| 91 | try: |
| 92 | body = getattr(cmd, bodyAttr) |
| 93 | except AttributeError: |
| 94 | return |
| 95 | |
| 96 | self.nl() |
| 97 | |
| 98 | self.heading('%s', heading) |
| 99 | self.nl() |
| 100 | |
| 101 | self.heading('%s', ''.ljust(len(heading), '-')) |
| 102 | self.nl() |
| 103 | |
| 104 | me = 'repo %s' % cmd.NAME |
| 105 | body = body.strip() |
| 106 | body = body.replace('%prog', me) |
| 107 | |
| 108 | for para in body.split("\n\n"): |
| 109 | if para.startswith(' '): |
| 110 | self.write('%s', para) |
| 111 | self.nl() |
| 112 | self.nl() |
| 113 | else: |
| 114 | self.wrap.add_flowing_data(para) |
| 115 | self.wrap.end_paragraph(1) |
| 116 | self.wrap.end_paragraph(0) |
| 117 | |
| 118 | out = _Out(self.manifest.globalConfig) |
| 119 | cmd.OptionParser.print_help() |
| 120 | out._PrintSection('Summary', 'helpSummary') |
| 121 | out._PrintSection('Description', 'helpDescription') |
| 122 | |
| 123 | def _Options(self, p): |
| 124 | p.add_option('-a', '--all', |
| 125 | dest='show_all', action='store_true', |
| 126 | help='show the complete list of commands') |
| 127 | |
| 128 | def Execute(self, opt, args): |
| 129 | if len(args) == 0: |
| 130 | if opt.show_all: |
| 131 | self._PrintAllCommands() |
| 132 | else: |
| 133 | self._PrintCommonCommands() |
| 134 | |
| 135 | elif len(args) == 1: |
| 136 | name = args[0] |
| 137 | |
| 138 | try: |
| 139 | cmd = self.commands[name] |
| 140 | except KeyError: |
| 141 | print >>sys.stderr, "repo: '%s' is not a repo command." % name |
| 142 | sys.exit(1) |
| 143 | |
| 144 | self._PrintCommandHelp(cmd) |
| 145 | |
| 146 | else: |
| 147 | self._PrintCommandHelp(self) |