help.py 4.7 KB

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