blob: 7893050238e935392fb4857315d96bef589203a5 [file] [log] [blame]
Mike Frysingerf6013762019-06-13 02:30:51 -04001# -*- coding:utf-8 -*-
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002#
3# Copyright (C) 2008 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
Sarah Owenscecd1d82012-11-01 22:59:27 -070017from __future__ import print_function
Shawn O. Pearce43c3d9e2009-03-04 14:26:50 -080018import re
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070019import sys
20from formatter import AbstractFormatter, DumbWriter
21
22from color import Coloring
Dan Willemsen79360642015-08-31 15:45:06 -070023from command import PagedCommand, MirrorSafeCommand, GitcAvailableCommand, GitcClientCommand
Dan Willemsen9ff2ece2015-08-31 15:45:06 -070024import gitc_utils
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070025
Shawn O. Pearcec95583b2009-03-03 17:47:06 -080026class Help(PagedCommand, MirrorSafeCommand):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070027 common = False
28 helpSummary = "Display detailed help on a command"
29 helpUsage = """
30%prog [--all|command]
31"""
32 helpDescription = """
33Displays detailed usage information about a command.
34"""
35
Mike Frysinger0b304c02019-12-02 16:49:13 -050036 def _PrintCommands(self, commandNames):
37 """Helper to display |commandNames| summaries."""
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070038 maxlen = 0
39 for name in commandNames:
40 maxlen = max(maxlen, len(name))
41 fmt = ' %%-%ds %%s' % maxlen
42
43 for name in commandNames:
44 command = self.commands[name]
45 try:
46 summary = command.helpSummary.strip()
47 except AttributeError:
48 summary = ''
Sarah Owenscecd1d82012-11-01 22:59:27 -070049 print(fmt % (name, summary))
Mike Frysinger0b304c02019-12-02 16:49:13 -050050
51 def _PrintAllCommands(self):
52 print('usage: repo COMMAND [ARGS]')
53 print('The complete list of recognized repo commands are:')
54 commandNames = list(sorted(self.commands))
55 self._PrintCommands(commandNames)
David Pursehouse2f9e7e42013-03-05 17:26:46 +090056 print("See 'repo help <command>' for more information on a "
Sarah Owenscecd1d82012-11-01 22:59:27 -070057 'specific command.')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070058
59 def _PrintCommonCommands(self):
Sarah Owenscecd1d82012-11-01 22:59:27 -070060 print('usage: repo COMMAND [ARGS]')
61 print('The most commonly used repo commands are:')
Dan Willemsen9ff2ece2015-08-31 15:45:06 -070062
63 def gitc_supported(cmd):
Dan Willemsen79360642015-08-31 15:45:06 -070064 if not isinstance(cmd, GitcAvailableCommand) and not isinstance(cmd, GitcClientCommand):
Dan Willemsen9ff2ece2015-08-31 15:45:06 -070065 return True
Dan Willemsen79360642015-08-31 15:45:06 -070066 if self.manifest.isGitcClient:
67 return True
68 if isinstance(cmd, GitcClientCommand):
69 return False
Dan Willemsen9ff2ece2015-08-31 15:45:06 -070070 if gitc_utils.get_gitc_manifest_dir():
71 return True
72 return False
73
Chirayu Desai217ea7d2013-03-01 19:14:38 +053074 commandNames = list(sorted([name
75 for name, command in self.commands.items()
Dan Willemsen9ff2ece2015-08-31 15:45:06 -070076 if command.common and gitc_supported(command)]))
Mike Frysinger0b304c02019-12-02 16:49:13 -050077 self._PrintCommands(commandNames)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070078
Sarah Owenscecd1d82012-11-01 22:59:27 -070079 print(
80"See 'repo help <command>' for more information on a specific command.\n"
81"See 'repo help --all' for a complete list of recognized commands.")
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070082
Mike Frysinger898f4e62019-07-31 18:17:44 -040083 def _PrintCommandHelp(self, cmd, header_prefix=''):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070084 class _Out(Coloring):
85 def __init__(self, gc):
86 Coloring.__init__(self, gc, 'help')
87 self.heading = self.printer('heading', attr='bold')
88
89 self.wrap = AbstractFormatter(DumbWriter())
90
91 def _PrintSection(self, heading, bodyAttr):
92 try:
93 body = getattr(cmd, bodyAttr)
94 except AttributeError:
95 return
Shawn O. Pearcec7c57e32009-06-03 17:43:16 -070096 if body == '' or body is None:
97 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070098
99 self.nl()
100
Mike Frysinger898f4e62019-07-31 18:17:44 -0400101 self.heading('%s%s', header_prefix, heading)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700102 self.nl()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700103 self.nl()
104
105 me = 'repo %s' % cmd.NAME
106 body = body.strip()
107 body = body.replace('%prog', me)
108
Mike Frysingerb8f7bb02018-10-10 01:05:11 -0400109 asciidoc_hdr = re.compile(r'^\n?#+ (.+)$')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700110 for para in body.split("\n\n"):
111 if para.startswith(' '):
112 self.write('%s', para)
113 self.nl()
114 self.nl()
Shawn O. Pearce43c3d9e2009-03-04 14:26:50 -0800115 continue
116
117 m = asciidoc_hdr.match(para)
118 if m:
Mike Frysinger898f4e62019-07-31 18:17:44 -0400119 self.heading('%s%s', header_prefix, m.group(1))
Shawn O. Pearce43c3d9e2009-03-04 14:26:50 -0800120 self.nl()
Shawn O. Pearce43c3d9e2009-03-04 14:26:50 -0800121 self.nl()
122 continue
123
124 self.wrap.add_flowing_data(para)
125 self.wrap.end_paragraph(1)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700126 self.wrap.end_paragraph(0)
127
128 out = _Out(self.manifest.globalConfig)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700129 out._PrintSection('Summary', 'helpSummary')
Shawn O. Pearce5da554f2009-04-18 11:44:00 -0700130 cmd.OptionParser.print_help()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700131 out._PrintSection('Description', 'helpDescription')
132
Mike Frysinger898f4e62019-07-31 18:17:44 -0400133 def _PrintAllCommandHelp(self):
134 for name in sorted(self.commands):
135 cmd = self.commands[name]
136 cmd.manifest = self.manifest
137 self._PrintCommandHelp(cmd, header_prefix='[%s] ' % (name,))
138
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700139 def _Options(self, p):
140 p.add_option('-a', '--all',
141 dest='show_all', action='store_true',
142 help='show the complete list of commands')
Mike Frysinger898f4e62019-07-31 18:17:44 -0400143 p.add_option('--help-all',
144 dest='show_all_help', action='store_true',
145 help='show the --help of all commands')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700146
147 def Execute(self, opt, args):
148 if len(args) == 0:
Mike Frysinger898f4e62019-07-31 18:17:44 -0400149 if opt.show_all_help:
150 self._PrintAllCommandHelp()
151 elif opt.show_all:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700152 self._PrintAllCommands()
153 else:
154 self._PrintCommonCommands()
155
156 elif len(args) == 1:
157 name = args[0]
158
159 try:
160 cmd = self.commands[name]
161 except KeyError:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700162 print("repo: '%s' is not a repo command." % name, file=sys.stderr)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700163 sys.exit(1)
164
Shawn O. Pearce752371d2011-10-11 15:23:37 -0700165 cmd.manifest = self.manifest
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700166 self._PrintCommandHelp(cmd)
167
168 else:
169 self._PrintCommandHelp(self)