help.py 4.7 KB

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