diff --git a/news/4433.feature b/news/4433.feature new file mode 100644 index 00000000000..29b16a168f2 --- /dev/null +++ b/news/4433.feature @@ -0,0 +1,2 @@ +Global options are not shown for subcommands unless explicitly requested with +`--verbose`. \ No newline at end of file diff --git a/src/pip/_internal/cli/base_command.py b/src/pip/_internal/cli/base_command.py index dac4b053d09..d8296723fbc 100644 --- a/src/pip/_internal/cli/base_command.py +++ b/src/pip/_internal/cli/base_command.py @@ -63,11 +63,11 @@ def __init__(self, isolated=False): self.cmd_opts = optparse.OptionGroup(self.parser, optgroup_name) # Add the general options - gen_opts = cmdoptions.make_option_group( + self.gen_opts = cmdoptions.make_option_group( cmdoptions.general_group, self.parser, ) - self.parser.add_option_group(gen_opts) + self.parser.add_option_group(self.gen_opts) def _build_session(self, options, retries=None, timeout=None): session = PipSession( @@ -109,6 +109,30 @@ def parse_args(self, args): # factored out for testability return self.parser.parse_args(args) + def print_help(self, verbose=False): + """ + Process 'help ' and ' --help' to hide global + options unless --verbose flag is specified. + + 'pip --help' is handled separately by pip.__init__.parseopt() + """ + if verbose: + self.parser.print_help() + else: + # remove General Options group and restore after print + saveepy = self.parser.epilog + saveogr = self.parser.option_groups # it is a list + self.parser.epilog = "" + if len(saveogr) > 1: + # need newline for commands with own options + self.parser.epilog = "\n" + self.parser.epilog += "Add '-v' flag to show general "\ + "options.\n" + self.parser.option_groups.remove(self.gen_opts) + self.parser.print_help() + self.parser.epilog = saveepy + self.parser.option_groups = saveogr + def main(self, args): options, args = self.parse_args(args) @@ -121,6 +145,10 @@ def main(self, args): user_log_file=options.log, ) + if options.help: + self.print_help(options.verbose) + sys.exit(0) + # TODO: Try to get these passing down from the command? # without resorting to os.environ to hold these. # This also affects isolated builds and it should. diff --git a/src/pip/_internal/cli/cmdoptions.py b/src/pip/_internal/cli/cmdoptions.py index 3033cd4b5e6..64216fd9806 100644 --- a/src/pip/_internal/cli/cmdoptions.py +++ b/src/pip/_internal/cli/cmdoptions.py @@ -106,7 +106,7 @@ def check_dist_restriction(options, check_target=False): Option, '-h', '--help', dest='help', - action='help', + action='store_true', help='Show help.', ) # type: Any diff --git a/src/pip/_internal/commands/help.py b/src/pip/_internal/commands/help.py index 49a81cbb074..347c5f82f3b 100644 --- a/src/pip/_internal/commands/help.py +++ b/src/pip/_internal/commands/help.py @@ -32,6 +32,6 @@ def run(self, options, args): raise CommandError(' - '.join(msg)) command = commands_dict[cmd_name]() - command.parser.print_help() + command.print_help(options.verbose) return SUCCESS