blob: 36b3a7ae1e5aa3e2b500b812378a0a4a34bbfb15 [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
David Pursehouse819827a2020-02-12 15:20:19 +090026
Shawn O. Pearcec95583b2009-03-03 17:47:06 -080027class Help(PagedCommand, MirrorSafeCommand):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070028 common = False
29 helpSummary = "Display detailed help on a command"
30 helpUsage = """
31%prog [--all|command]
32"""
33 helpDescription = """
34Displays detailed usage information about a command.
35"""
36
Mike Frysinger0b304c02019-12-02 16:49:13 -050037 def _PrintCommands(self, commandNames):
38 """Helper to display |commandNames| summaries."""
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070039 maxlen = 0
40 for name in commandNames:
41 maxlen = max(maxlen, len(name))
42 fmt = ' %%-%ds %%s' % maxlen
43
44 for name in commandNames:
45 command = self.commands[name]
46 try:
47 summary = command.helpSummary.strip()
48 except AttributeError:
49 summary = ''
Sarah Owenscecd1d82012-11-01 22:59:27 -070050 print(fmt % (name, summary))
Mike Frysinger0b304c02019-12-02 16:49:13 -050051
52 def _PrintAllCommands(self):
53 print('usage: repo COMMAND [ARGS]')
54 print('The complete list of recognized repo commands are:')
55 commandNames = list(sorted(self.commands))
56 self._PrintCommands(commandNames)
David Pursehouse2f9e7e42013-03-05 17:26:46 +090057 print("See 'repo help <command>' for more information on a "
Sarah Owenscecd1d82012-11-01 22:59:27 -070058 'specific command.')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070059
60 def _PrintCommonCommands(self):
Sarah Owenscecd1d82012-11-01 22:59:27 -070061 print('usage: repo COMMAND [ARGS]')
62 print('The most commonly used repo commands are:')
Dan Willemsen9ff2ece2015-08-31 15:45:06 -070063
64 def gitc_supported(cmd):
Dan Willemsen79360642015-08-31 15:45:06 -070065 if not isinstance(cmd, GitcAvailableCommand) and not isinstance(cmd, GitcClientCommand):
Dan Willemsen9ff2ece2015-08-31 15:45:06 -070066 return True
Dan Willemsen79360642015-08-31 15:45:06 -070067 if self.manifest.isGitcClient:
68 return True
69 if isinstance(cmd, GitcClientCommand):
70 return False
Dan Willemsen9ff2ece2015-08-31 15:45:06 -070071 if gitc_utils.get_gitc_manifest_dir():
72 return True
73 return False
74
Chirayu Desai217ea7d2013-03-01 19:14:38 +053075 commandNames = list(sorted([name
David Pursehouseabdf7502020-02-12 14:58:39 +090076 for name, command in self.commands.items()
77 if command.common and gitc_supported(command)]))
Mike Frysinger0b304c02019-12-02 16:49:13 -050078 self._PrintCommands(commandNames)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070079
Sarah Owenscecd1d82012-11-01 22:59:27 -070080 print(
David Pursehouseabdf7502020-02-12 14:58:39 +090081 "See 'repo help <command>' for more information on a specific command.\n"
82 "See 'repo help --all' for a complete list of recognized commands.")
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070083
Mike Frysinger898f4e62019-07-31 18:17:44 -040084 def _PrintCommandHelp(self, cmd, header_prefix=''):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070085 class _Out(Coloring):
86 def __init__(self, gc):
87 Coloring.__init__(self, gc, 'help')
88 self.heading = self.printer('heading', attr='bold')
89
90 self.wrap = AbstractFormatter(DumbWriter())
91
92 def _PrintSection(self, heading, bodyAttr):
93 try:
94 body = getattr(cmd, bodyAttr)
95 except AttributeError:
96 return
Shawn O. Pearcec7c57e32009-06-03 17:43:16 -070097 if body == '' or body is None:
98 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070099
100 self.nl()
101
Mike Frysinger898f4e62019-07-31 18:17:44 -0400102 self.heading('%s%s', header_prefix, heading)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700103 self.nl()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700104 self.nl()
105
106 me = 'repo %s' % cmd.NAME
107 body = body.strip()
108 body = body.replace('%prog', me)
109
Mike Frysingerb8f7bb02018-10-10 01:05:11 -0400110 asciidoc_hdr = re.compile(r'^\n?#+ (.+)$')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700111 for para in body.split("\n\n"):
112 if para.startswith(' '):
113 self.write('%s', para)
114 self.nl()
115 self.nl()
Shawn O. Pearce43c3d9e2009-03-04 14:26:50 -0800116 continue
117
118 m = asciidoc_hdr.match(para)
119 if m:
Mike Frysinger898f4e62019-07-31 18:17:44 -0400120 self.heading('%s%s', header_prefix, m.group(1))
Shawn O. Pearce43c3d9e2009-03-04 14:26:50 -0800121 self.nl()
Shawn O. Pearce43c3d9e2009-03-04 14:26:50 -0800122 self.nl()
123 continue
124
125 self.wrap.add_flowing_data(para)
126 self.wrap.end_paragraph(1)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700127 self.wrap.end_paragraph(0)
128
129 out = _Out(self.manifest.globalConfig)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700130 out._PrintSection('Summary', 'helpSummary')
Shawn O. Pearce5da554f2009-04-18 11:44:00 -0700131 cmd.OptionParser.print_help()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700132 out._PrintSection('Description', 'helpDescription')
133
Mike Frysinger898f4e62019-07-31 18:17:44 -0400134 def _PrintAllCommandHelp(self):
135 for name in sorted(self.commands):
136 cmd = self.commands[name]
137 cmd.manifest = self.manifest
138 self._PrintCommandHelp(cmd, header_prefix='[%s] ' % (name,))
139
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700140 def _Options(self, p):
141 p.add_option('-a', '--all',
142 dest='show_all', action='store_true',
143 help='show the complete list of commands')
Mike Frysinger898f4e62019-07-31 18:17:44 -0400144 p.add_option('--help-all',
145 dest='show_all_help', action='store_true',
146 help='show the --help of all commands')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700147
148 def Execute(self, opt, args):
149 if len(args) == 0:
Mike Frysinger898f4e62019-07-31 18:17:44 -0400150 if opt.show_all_help:
151 self._PrintAllCommandHelp()
152 elif opt.show_all:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700153 self._PrintAllCommands()
154 else:
155 self._PrintCommonCommands()
156
157 elif len(args) == 1:
158 name = args[0]
159
160 try:
161 cmd = self.commands[name]
162 except KeyError:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700163 print("repo: '%s' is not a repo command." % name, file=sys.stderr)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700164 sys.exit(1)
165
Shawn O. Pearce752371d2011-10-11 15:23:37 -0700166 cmd.manifest = self.manifest
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700167 self._PrintCommandHelp(cmd)
168
169 else:
170 self._PrintCommandHelp(self)