blob: 6a767e6fd7840496bc2f6e6506fdeae1f1c0c128 [file] [log] [blame]
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001# Copyright (C) 2008 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
Shawn O. Pearce43c3d9e2009-03-04 14:26:50 -080015import re
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070016import sys
Mike Frysinger0888a082021-04-13 20:22:01 -040017import textwrap
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070018
Mike Frysingerd3639c52020-02-25 15:12:37 -050019from subcmds import all_commands
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070020from color import Coloring
Dan Willemsen79360642015-08-31 15:45:06 -070021from command import PagedCommand, MirrorSafeCommand, GitcAvailableCommand, GitcClientCommand
Dan Willemsen9ff2ece2015-08-31 15:45:06 -070022import gitc_utils
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070023
David Pursehouse819827a2020-02-12 15:20:19 +090024
Shawn O. Pearcec95583b2009-03-03 17:47:06 -080025class Help(PagedCommand, MirrorSafeCommand):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070026 common = False
27 helpSummary = "Display detailed help on a command"
28 helpUsage = """
29%prog [--all|command]
30"""
31 helpDescription = """
32Displays detailed usage information about a command.
33"""
34
Mike Frysinger0b304c02019-12-02 16:49:13 -050035 def _PrintCommands(self, commandNames):
36 """Helper to display |commandNames| summaries."""
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070037 maxlen = 0
38 for name in commandNames:
39 maxlen = max(maxlen, len(name))
40 fmt = ' %%-%ds %%s' % maxlen
41
42 for name in commandNames:
Mike Frysingerbb930462020-02-25 15:18:31 -050043 command = all_commands[name]()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070044 try:
45 summary = command.helpSummary.strip()
46 except AttributeError:
47 summary = ''
Sarah Owenscecd1d82012-11-01 22:59:27 -070048 print(fmt % (name, summary))
Mike Frysinger0b304c02019-12-02 16:49:13 -050049
50 def _PrintAllCommands(self):
51 print('usage: repo COMMAND [ARGS]')
52 print('The complete list of recognized repo commands are:')
Mike Frysingerd3639c52020-02-25 15:12:37 -050053 commandNames = list(sorted(all_commands))
Mike Frysinger0b304c02019-12-02 16:49:13 -050054 self._PrintCommands(commandNames)
David Pursehouse2f9e7e42013-03-05 17:26:46 +090055 print("See 'repo help <command>' for more information on a "
Sarah Owenscecd1d82012-11-01 22:59:27 -070056 'specific command.')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070057
58 def _PrintCommonCommands(self):
Sarah Owenscecd1d82012-11-01 22:59:27 -070059 print('usage: repo COMMAND [ARGS]')
60 print('The most commonly used repo commands are:')
Dan Willemsen9ff2ece2015-08-31 15:45:06 -070061
62 def gitc_supported(cmd):
Dan Willemsen79360642015-08-31 15:45:06 -070063 if not isinstance(cmd, GitcAvailableCommand) and not isinstance(cmd, GitcClientCommand):
Dan Willemsen9ff2ece2015-08-31 15:45:06 -070064 return True
Mike Frysinger8c1e9cb2020-09-06 14:53:18 -040065 if self.client.isGitcClient:
Dan Willemsen79360642015-08-31 15:45:06 -070066 return True
67 if isinstance(cmd, GitcClientCommand):
68 return False
Dan Willemsen9ff2ece2015-08-31 15:45:06 -070069 if gitc_utils.get_gitc_manifest_dir():
70 return True
71 return False
72
Chirayu Desai217ea7d2013-03-01 19:14:38 +053073 commandNames = list(sorted([name
Mike Frysingerd3639c52020-02-25 15:12:37 -050074 for name, command in all_commands.items()
David Pursehouseabdf7502020-02-12 14:58:39 +090075 if command.common and gitc_supported(command)]))
Mike Frysinger0b304c02019-12-02 16:49:13 -050076 self._PrintCommands(commandNames)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070077
Sarah Owenscecd1d82012-11-01 22:59:27 -070078 print(
David Pursehouseabdf7502020-02-12 14:58:39 +090079 "See 'repo help <command>' for more information on a specific command.\n"
80 "See 'repo help --all' for a complete list of recognized commands.")
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070081
Mike Frysinger898f4e62019-07-31 18:17:44 -040082 def _PrintCommandHelp(self, cmd, header_prefix=''):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070083 class _Out(Coloring):
84 def __init__(self, gc):
85 Coloring.__init__(self, gc, 'help')
86 self.heading = self.printer('heading', attr='bold')
Mike Frysinger0888a082021-04-13 20:22:01 -040087 self._first = True
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070088
89 def _PrintSection(self, heading, bodyAttr):
90 try:
91 body = getattr(cmd, bodyAttr)
92 except AttributeError:
93 return
Shawn O. Pearcec7c57e32009-06-03 17:43:16 -070094 if body == '' or body is None:
95 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070096
Mike Frysinger0888a082021-04-13 20:22:01 -040097 if not self._first:
98 self.nl()
99 self._first = False
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700100
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 Frysinger0888a082021-04-13 20:22:01 -0400109 # Extract the title, but skip any trailing {#anchors}.
110 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
Mike Frysinger0888a082021-04-13 20:22:01 -0400125 lines = textwrap.wrap(para.replace(' ', ' '), width=80,
126 break_long_words=False, break_on_hyphens=False)
127 for line in lines:
128 self.write('%s', line)
129 self.nl()
130 self.nl()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700131
Mike Frysinger8c1e9cb2020-09-06 14:53:18 -0400132 out = _Out(self.client.globalConfig)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700133 out._PrintSection('Summary', 'helpSummary')
Shawn O. Pearce5da554f2009-04-18 11:44:00 -0700134 cmd.OptionParser.print_help()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700135 out._PrintSection('Description', 'helpDescription')
136
Mike Frysinger898f4e62019-07-31 18:17:44 -0400137 def _PrintAllCommandHelp(self):
Mike Frysingerd3639c52020-02-25 15:12:37 -0500138 for name in sorted(all_commands):
Mike Frysingerbb930462020-02-25 15:18:31 -0500139 cmd = all_commands[name]()
Mike Frysinger898f4e62019-07-31 18:17:44 -0400140 cmd.manifest = self.manifest
141 self._PrintCommandHelp(cmd, header_prefix='[%s] ' % (name,))
142
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700143 def _Options(self, p):
144 p.add_option('-a', '--all',
145 dest='show_all', action='store_true',
146 help='show the complete list of commands')
Mike Frysinger898f4e62019-07-31 18:17:44 -0400147 p.add_option('--help-all',
148 dest='show_all_help', action='store_true',
149 help='show the --help of all commands')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700150
151 def Execute(self, opt, args):
152 if len(args) == 0:
Mike Frysinger898f4e62019-07-31 18:17:44 -0400153 if opt.show_all_help:
154 self._PrintAllCommandHelp()
155 elif opt.show_all:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700156 self._PrintAllCommands()
157 else:
158 self._PrintCommonCommands()
159
160 elif len(args) == 1:
161 name = args[0]
162
163 try:
Mike Frysingerbb930462020-02-25 15:18:31 -0500164 cmd = all_commands[name]()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700165 except KeyError:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700166 print("repo: '%s' is not a repo command." % name, file=sys.stderr)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700167 sys.exit(1)
168
Shawn O. Pearce752371d2011-10-11 15:23:37 -0700169 cmd.manifest = self.manifest
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700170 self._PrintCommandHelp(cmd)
171
172 else:
173 self._PrintCommandHelp(self)