diff options
author | Mike Frysinger <vapier@google.com> | 2021-07-26 23:46:32 -0400 |
---|---|---|
committer | Mike Frysinger <vapier@google.com> | 2021-07-28 05:38:34 +0000 |
commit | 56345c345bdfdd71399d17ccd0ffe8f39bf720eb (patch) | |
tree | efea7101c53e3f8eaea34639a54d8caedd8aa45a /main.py | |
parent | a024bd33b808489acc909036b63697a819cc6ce7 (diff) | |
download | git-repo-56345c345bdfdd71399d17ccd0ffe8f39bf720eb.tar.gz |
repo: refactor help output handling
Currently we have the behavior:
* `repo`: Equivalent to `repo help` -- only shows common subcommands
(with short description), and then exits 0.
* `repo --help`: Shows repo's core options, lists all commands (no
specific info), and then exits 0.
The first case is not behaving well:
* If you run `repo` without a specific subcommand, that's an error,
so we should be exiting 1 instead.
* Showing only subcommands and no actual option summary makes it seem
like repo itself doesn't take any options. This confuses users.
Let's rework things a bit. Now we have the behavior:
* `repo`: Shows repo's core options, lists all commands (no specific
info), and then exits 1.
* `repo --help`: Shows repo's core options, shows common subcommands
(with short description), and then exits 0.
* `repo --help-all`: Shows repo's core options, shows all subcommands
(with short description), and then exits 0.
Basically we swap the behavior of `repo` and `repo --help`, and fix
the exit status when the subcommand is missing.
The addition of --help-all is mostly for the man pages. We were
relying on `repo help --all` to generate the repo(1) man page, but
that too omitted the core repo options. Now the man page includes
all the core repo options and provides a summary of all commands.
Change-Id: I1f99b99d5b8af2591f96a078d0647a3d76d6b0fc
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/312908
Reviewed-by: Xin Li <delphij@google.com>
Tested-by: Mike Frysinger <vapier@google.com>
Diffstat (limited to 'main.py')
-rwxr-xr-x | main.py | 30 |
1 files changed, 23 insertions, 7 deletions
@@ -95,6 +95,8 @@ global_options = optparse.OptionParser( | |||
95 | add_help_option=False) | 95 | add_help_option=False) |
96 | global_options.add_option('-h', '--help', action='store_true', | 96 | global_options.add_option('-h', '--help', action='store_true', |
97 | help='show this help message and exit') | 97 | help='show this help message and exit') |
98 | global_options.add_option('--help-all', action='store_true', | ||
99 | help='show this help message with all subcommands and exit') | ||
98 | global_options.add_option('-p', '--paginate', | 100 | global_options.add_option('-p', '--paginate', |
99 | dest='pager', action='store_true', | 101 | dest='pager', action='store_true', |
100 | help='display command output in the pager') | 102 | help='display command output in the pager') |
@@ -128,6 +130,23 @@ class _Repo(object): | |||
128 | self.repodir = repodir | 130 | self.repodir = repodir |
129 | self.commands = all_commands | 131 | self.commands = all_commands |
130 | 132 | ||
133 | def _PrintHelp(self, short: bool = False, all_commands: bool = False): | ||
134 | """Show --help screen.""" | ||
135 | global_options.print_help() | ||
136 | print() | ||
137 | if short: | ||
138 | commands = ' '.join(sorted(self.commands)) | ||
139 | wrapped_commands = textwrap.wrap(commands, width=77) | ||
140 | print('Available commands:\n %s' % ('\n '.join(wrapped_commands),)) | ||
141 | print('\nRun `repo help <command>` for command-specific details.') | ||
142 | print('Bug reports:', Wrapper().BUG_URL) | ||
143 | else: | ||
144 | cmd = self.commands['help']() | ||
145 | if all_commands: | ||
146 | cmd.PrintAllCommandsBody() | ||
147 | else: | ||
148 | cmd.PrintCommonCommandsBody() | ||
149 | |||
131 | def _ParseArgs(self, argv): | 150 | def _ParseArgs(self, argv): |
132 | """Parse the main `repo` command line options.""" | 151 | """Parse the main `repo` command line options.""" |
133 | for i, arg in enumerate(argv): | 152 | for i, arg in enumerate(argv): |
@@ -177,19 +196,16 @@ class _Repo(object): | |||
177 | SetTrace() | 196 | SetTrace() |
178 | 197 | ||
179 | # Handle options that terminate quickly first. | 198 | # Handle options that terminate quickly first. |
180 | if gopts.help: | 199 | if gopts.help or gopts.help_all: |
181 | global_options.print_help() | 200 | self._PrintHelp(short=False, all_commands=gopts.help_all) |
182 | commands = ' '.join(sorted(self.commands)) | ||
183 | wrapped_commands = textwrap.wrap(commands, width=77) | ||
184 | print('\nAvailable commands:\n %s' % ('\n '.join(wrapped_commands),)) | ||
185 | print('\nRun `repo help <command>` for command-specific details.') | ||
186 | return 0 | 201 | return 0 |
187 | elif gopts.show_version: | 202 | elif gopts.show_version: |
188 | # Always allow global --version regardless of subcommand validity. | 203 | # Always allow global --version regardless of subcommand validity. |
189 | name = 'version' | 204 | name = 'version' |
190 | elif not name: | 205 | elif not name: |
191 | # No subcommand specified, so show the help/subcommand. | 206 | # No subcommand specified, so show the help/subcommand. |
192 | name = 'help' | 207 | self._PrintHelp(short=True) |
208 | return 1 | ||
193 | 209 | ||
194 | SetDefaultColoring(gopts.color) | 210 | SetDefaultColoring(gopts.color) |
195 | 211 | ||