blob: 5e24ed0b6b56a5693f502567dcadddcc1f5abf18 [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
Mike Frysingerd3639c52020-02-25 15:12:37 -050022from subcmds import all_commands
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070023from color import Coloring
Dan Willemsen79360642015-08-31 15:45:06 -070024from command import PagedCommand, MirrorSafeCommand, GitcAvailableCommand, GitcClientCommand
Dan Willemsen9ff2ece2015-08-31 15:45:06 -070025import gitc_utils
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070026
David Pursehouse819827a2020-02-12 15:20:19 +090027
Shawn O. Pearcec95583b2009-03-03 17:47:06 -080028class Help(PagedCommand, MirrorSafeCommand):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070029 common = False
30 helpSummary = "Display detailed help on a command"
31 helpUsage = """
32%prog [--all|command]
33"""
34 helpDescription = """
35Displays detailed usage information about a command.
36"""
37
Mike Frysinger0b304c02019-12-02 16:49:13 -050038 def _PrintCommands(self, commandNames):
39 """Helper to display |commandNames| summaries."""
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070040 maxlen = 0
41 for name in commandNames:
42 maxlen = max(maxlen, len(name))
43 fmt = ' %%-%ds %%s' % maxlen
44
45 for name in commandNames:
Mike Frysingerd3639c52020-02-25 15:12:37 -050046 command = all_commands[name]
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070047 try:
48 summary = command.helpSummary.strip()
49 except AttributeError:
50 summary = ''
Sarah Owenscecd1d82012-11-01 22:59:27 -070051 print(fmt % (name, summary))
Mike Frysinger0b304c02019-12-02 16:49:13 -050052
53 def _PrintAllCommands(self):
54 print('usage: repo COMMAND [ARGS]')
55 print('The complete list of recognized repo commands are:')
Mike Frysingerd3639c52020-02-25 15:12:37 -050056 commandNames = list(sorted(all_commands))
Mike Frysinger0b304c02019-12-02 16:49:13 -050057 self._PrintCommands(commandNames)
David Pursehouse2f9e7e42013-03-05 17:26:46 +090058 print("See 'repo help <command>' for more information on a "
Sarah Owenscecd1d82012-11-01 22:59:27 -070059 'specific command.')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070060
61 def _PrintCommonCommands(self):
Sarah Owenscecd1d82012-11-01 22:59:27 -070062 print('usage: repo COMMAND [ARGS]')
63 print('The most commonly used repo commands are:')
Dan Willemsen9ff2ece2015-08-31 15:45:06 -070064
65 def gitc_supported(cmd):
Dan Willemsen79360642015-08-31 15:45:06 -070066 if not isinstance(cmd, GitcAvailableCommand) and not isinstance(cmd, GitcClientCommand):
Dan Willemsen9ff2ece2015-08-31 15:45:06 -070067 return True
Dan Willemsen79360642015-08-31 15:45:06 -070068 if self.manifest.isGitcClient:
69 return True
70 if isinstance(cmd, GitcClientCommand):
71 return False
Dan Willemsen9ff2ece2015-08-31 15:45:06 -070072 if gitc_utils.get_gitc_manifest_dir():
73 return True
74 return False
75
Chirayu Desai217ea7d2013-03-01 19:14:38 +053076 commandNames = list(sorted([name
Mike Frysingerd3639c52020-02-25 15:12:37 -050077 for name, command in all_commands.items()
David Pursehouseabdf7502020-02-12 14:58:39 +090078 if command.common and gitc_supported(command)]))
Mike Frysinger0b304c02019-12-02 16:49:13 -050079 self._PrintCommands(commandNames)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070080
Sarah Owenscecd1d82012-11-01 22:59:27 -070081 print(
David Pursehouseabdf7502020-02-12 14:58:39 +090082 "See 'repo help <command>' for more information on a specific command.\n"
83 "See 'repo help --all' for a complete list of recognized commands.")
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070084
Mike Frysinger898f4e62019-07-31 18:17:44 -040085 def _PrintCommandHelp(self, cmd, header_prefix=''):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070086 class _Out(Coloring):
87 def __init__(self, gc):
88 Coloring.__init__(self, gc, 'help')
89 self.heading = self.printer('heading', attr='bold')
90
91 self.wrap = AbstractFormatter(DumbWriter())
92
93 def _PrintSection(self, heading, bodyAttr):
94 try:
95 body = getattr(cmd, bodyAttr)
96 except AttributeError:
97 return
Shawn O. Pearcec7c57e32009-06-03 17:43:16 -070098 if body == '' or body is None:
99 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700100
101 self.nl()
102
Mike Frysinger898f4e62019-07-31 18:17:44 -0400103 self.heading('%s%s', header_prefix, heading)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700104 self.nl()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700105 self.nl()
106
107 me = 'repo %s' % cmd.NAME
108 body = body.strip()
109 body = body.replace('%prog', me)
110
Mike Frysingerb8f7bb02018-10-10 01:05:11 -0400111 asciidoc_hdr = re.compile(r'^\n?#+ (.+)$')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700112 for para in body.split("\n\n"):
113 if para.startswith(' '):
114 self.write('%s', para)
115 self.nl()
116 self.nl()
Shawn O. Pearce43c3d9e2009-03-04 14:26:50 -0800117 continue
118
119 m = asciidoc_hdr.match(para)
120 if m:
Mike Frysinger898f4e62019-07-31 18:17:44 -0400121 self.heading('%s%s', header_prefix, m.group(1))
Shawn O. Pearce43c3d9e2009-03-04 14:26:50 -0800122 self.nl()
Shawn O. Pearce43c3d9e2009-03-04 14:26:50 -0800123 self.nl()
124 continue
125
126 self.wrap.add_flowing_data(para)
127 self.wrap.end_paragraph(1)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700128 self.wrap.end_paragraph(0)
129
130 out = _Out(self.manifest.globalConfig)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700131 out._PrintSection('Summary', 'helpSummary')
Shawn O. Pearce5da554f2009-04-18 11:44:00 -0700132 cmd.OptionParser.print_help()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700133 out._PrintSection('Description', 'helpDescription')
134
Mike Frysinger898f4e62019-07-31 18:17:44 -0400135 def _PrintAllCommandHelp(self):
Mike Frysingerd3639c52020-02-25 15:12:37 -0500136 for name in sorted(all_commands):
137 cmd = all_commands[name]
Mike Frysinger898f4e62019-07-31 18:17:44 -0400138 cmd.manifest = self.manifest
139 self._PrintCommandHelp(cmd, header_prefix='[%s] ' % (name,))
140
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700141 def _Options(self, p):
142 p.add_option('-a', '--all',
143 dest='show_all', action='store_true',
144 help='show the complete list of commands')
Mike Frysinger898f4e62019-07-31 18:17:44 -0400145 p.add_option('--help-all',
146 dest='show_all_help', action='store_true',
147 help='show the --help of all commands')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700148
149 def Execute(self, opt, args):
150 if len(args) == 0:
Mike Frysinger898f4e62019-07-31 18:17:44 -0400151 if opt.show_all_help:
152 self._PrintAllCommandHelp()
153 elif opt.show_all:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700154 self._PrintAllCommands()
155 else:
156 self._PrintCommonCommands()
157
158 elif len(args) == 1:
159 name = args[0]
160
161 try:
Mike Frysingerd3639c52020-02-25 15:12:37 -0500162 cmd = all_commands[name]
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700163 except KeyError:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700164 print("repo: '%s' is not a repo command." % name, file=sys.stderr)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700165 sys.exit(1)
166
Shawn O. Pearce752371d2011-10-11 15:23:37 -0700167 cmd.manifest = self.manifest
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700168 self._PrintCommandHelp(cmd)
169
170 else:
171 self._PrintCommandHelp(self)