help.py 4.7 KB

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