help.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. # Copyright (C) 2008 The Android Open Source Project
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import re
  15. import sys
  16. from formatter import AbstractFormatter, DumbWriter
  17. from subcmds import all_commands
  18. from color import Coloring
  19. from command import PagedCommand, MirrorSafeCommand, GitcAvailableCommand, GitcClientCommand
  20. import gitc_utils
  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 _PrintCommands(self, commandNames):
  31. """Helper to display |commandNames| summaries."""
  32. maxlen = 0
  33. for name in commandNames:
  34. maxlen = max(maxlen, len(name))
  35. fmt = ' %%-%ds %%s' % maxlen
  36. for name in commandNames:
  37. command = all_commands[name]()
  38. try:
  39. summary = command.helpSummary.strip()
  40. except AttributeError:
  41. summary = ''
  42. print(fmt % (name, summary))
  43. def _PrintAllCommands(self):
  44. print('usage: repo COMMAND [ARGS]')
  45. print('The complete list of recognized repo commands are:')
  46. commandNames = list(sorted(all_commands))
  47. self._PrintCommands(commandNames)
  48. print("See 'repo help <command>' for more information on a "
  49. 'specific command.')
  50. def _PrintCommonCommands(self):
  51. print('usage: repo COMMAND [ARGS]')
  52. print('The most commonly used repo commands are:')
  53. def gitc_supported(cmd):
  54. if not isinstance(cmd, GitcAvailableCommand) and not isinstance(cmd, GitcClientCommand):
  55. return True
  56. if self.client.isGitcClient:
  57. return True
  58. if isinstance(cmd, GitcClientCommand):
  59. return False
  60. if gitc_utils.get_gitc_manifest_dir():
  61. return True
  62. return False
  63. commandNames = list(sorted([name
  64. for name, command in all_commands.items()
  65. if command.common and gitc_supported(command)]))
  66. self._PrintCommands(commandNames)
  67. print(
  68. "See 'repo help <command>' for more information on a specific command.\n"
  69. "See 'repo help --all' for a complete list of recognized commands.")
  70. def _PrintCommandHelp(self, cmd, header_prefix=''):
  71. class _Out(Coloring):
  72. def __init__(self, gc):
  73. Coloring.__init__(self, gc, 'help')
  74. self.heading = self.printer('heading', attr='bold')
  75. self.wrap = AbstractFormatter(DumbWriter())
  76. def _PrintSection(self, heading, bodyAttr):
  77. try:
  78. body = getattr(cmd, bodyAttr)
  79. except AttributeError:
  80. return
  81. if body == '' or body is None:
  82. return
  83. self.nl()
  84. self.heading('%s%s', header_prefix, heading)
  85. self.nl()
  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?#+ (.+)$')
  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. self.heading('%s%s', header_prefix, m.group(1))
  100. self.nl()
  101. self.nl()
  102. continue
  103. self.wrap.add_flowing_data(para)
  104. self.wrap.end_paragraph(1)
  105. self.wrap.end_paragraph(0)
  106. out = _Out(self.client.globalConfig)
  107. out._PrintSection('Summary', 'helpSummary')
  108. cmd.OptionParser.print_help()
  109. out._PrintSection('Description', 'helpDescription')
  110. def _PrintAllCommandHelp(self):
  111. for name in sorted(all_commands):
  112. cmd = all_commands[name]()
  113. cmd.manifest = self.manifest
  114. self._PrintCommandHelp(cmd, header_prefix='[%s] ' % (name,))
  115. def _Options(self, p):
  116. p.add_option('-a', '--all',
  117. dest='show_all', action='store_true',
  118. help='show the complete list of commands')
  119. p.add_option('--help-all',
  120. dest='show_all_help', action='store_true',
  121. help='show the --help of all commands')
  122. def Execute(self, opt, args):
  123. if len(args) == 0:
  124. if opt.show_all_help:
  125. self._PrintAllCommandHelp()
  126. elif opt.show_all:
  127. self._PrintAllCommands()
  128. else:
  129. self._PrintCommonCommands()
  130. elif len(args) == 1:
  131. name = args[0]
  132. try:
  133. cmd = all_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)