Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hide global options for subcommands unless --verbose #5746

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions news/4433.feature
@@ -0,0 +1,2 @@
Global options are not shown for subcommands unless explicitly requested with
`--verbose`.
32 changes: 30 additions & 2 deletions src/pip/_internal/cli/base_command.py
Expand Up @@ -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(
Expand Down Expand Up @@ -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 <command>' and '<command> --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)

Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/pip/_internal/cli/cmdoptions.py
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/pip/_internal/commands/help.py
Expand Up @@ -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