help.py 3.9 KB

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