help.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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, MirrorSafeCommand
  19. class Help(PagedCommand, MirrorSafeCommand):
  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. See 'repo help --all' for a complete list of recognized commands.
  72. """
  73. def _PrintCommandHelp(self, cmd):
  74. class _Out(Coloring):
  75. def __init__(self, gc):
  76. Coloring.__init__(self, gc, 'help')
  77. self.heading = self.printer('heading', attr='bold')
  78. self.wrap = AbstractFormatter(DumbWriter())
  79. def _PrintSection(self, heading, bodyAttr):
  80. try:
  81. body = getattr(cmd, bodyAttr)
  82. except AttributeError:
  83. return
  84. self.nl()
  85. self.heading('%s', heading)
  86. self.nl()
  87. self.heading('%s', ''.ljust(len(heading), '-'))
  88. self.nl()
  89. me = 'repo %s' % cmd.NAME
  90. body = body.strip()
  91. body = body.replace('%prog', me)
  92. for para in body.split("\n\n"):
  93. if para.startswith(' '):
  94. self.write('%s', para)
  95. self.nl()
  96. self.nl()
  97. else:
  98. self.wrap.add_flowing_data(para)
  99. self.wrap.end_paragraph(1)
  100. self.wrap.end_paragraph(0)
  101. out = _Out(self.manifest.globalConfig)
  102. cmd.OptionParser.print_help()
  103. out._PrintSection('Summary', 'helpSummary')
  104. out._PrintSection('Description', 'helpDescription')
  105. def _Options(self, p):
  106. p.add_option('-a', '--all',
  107. dest='show_all', action='store_true',
  108. help='show the complete list of commands')
  109. def Execute(self, opt, args):
  110. if len(args) == 0:
  111. if opt.show_all:
  112. self._PrintAllCommands()
  113. else:
  114. self._PrintCommonCommands()
  115. elif len(args) == 1:
  116. name = args[0]
  117. try:
  118. cmd = self.commands[name]
  119. except KeyError:
  120. print >>sys.stderr, "repo: '%s' is not a repo command." % name
  121. sys.exit(1)
  122. self._PrintCommandHelp(cmd)
  123. else:
  124. self._PrintCommandHelp(self)